-2

I have the following class named DatabaseHandler

public class DatabaseHandler extends SQLiteOpenHelper {

    // Database Name
    private static final String DATABASE_NAME="contacts_provider";

    // Database Version
    private static final int DATABASE_VARSION=1;

    // Table Name
    private static final String TABLE_NAME="contacts";

    // Column Names
    private static final String COL_CONTACT_ID="contact_id";
    private static final String COL_NAME="name";
    private static final String COL_PHONE="phone";

    public DatabaseHandler(Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VARSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){}

    public void addContact(Contact contact){
        SQLiteDatabase db=this.getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put(COL_NAME,contact.getName());
        values.put(COL_PHONE,contact.getPhone());

        db.insert(TABLE_NAME,null,values);

        db.close();
    }

    public List<Contact> getAllContacts(){
        SQLiteDatabase db=getReadableDatabase();

        List<Contact> list=new ArrayList<>();

        String sql="SELECT * FROM "+TABLE_NAME;

        Cursor cursor=db.rawQuery(sql,null);

        if(cursor!=null){
            if(cursor.moveToFirst()){

                do{
                    int contact_id=Integer.parseInt(cursor.getString(0));
                    String name=cursor.getString(1);
                    String phone=cursor.getString(2);

                    Contact contact=new Contact(contact_id,name,phone);

                    list.add(contact);
                }while(cursor.moveToNext());

            }

            cursor.close();
        }

        db.close();

        return  list;
    }
}

As you can can observe, I have not created the contacts table in the onCreate() method.

Here are my observations,

  1. When I execute addContact(Contact contact) method the following things happen: a. SQLiteException gets produced. logcat shows, android.database.sqlite.SQLiteException: no such table: contacts (code 1): , while compiling: INSERT INTO contacts(phone,name) VALUES (?,?) b. The app doesn't get crashed on the emulator.

  2. When I execute getAllContacts() method the following things happen: a. RuntimeException gets produced. logcat shows, java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.sqlite2/com.example.user.sqlite2.SQLite.SQLiteExecution}: android.database.sqlite.SQLiteException: no such table: contacts (code 1): , while compiling: SELECT * FROM contacts b. The app crashes on the emulator

So my question is, What kind of SQL instructions produce what kind of Exceptions if the table doesn't exist in the SQLite database?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
shiv
  • 191
  • 2
  • 11

1 Answers1

-1

You must create table in public void onCreate(SQLiteDatabase db) before doing any insertions.

public void onCreate(SQLiteDatabase db) {
    db.execSQL(String.format("CREATE TABLE %s(%s ID INT PRIMARY KEY,
                                              %s TEXT,
                                              %s TEXT);",
                             TABLE_NAME,
                             COL_CONTACT_ID, 
                             COL_NAME, 
                             COL_PHONE);
}
IlyaGulya
  • 957
  • 6
  • 18