0

I want to know the answer of this question because of, in one of my interview, interviewer asked to me this question.

When database Table is created..?

Option:-

  1. On application launch.
  2. when inserting data.

can any one explain me when the data base created?

I have always created table with specific row and column and inserted data, updating data for my application, but never think about this question.

AndroidMob
  • 101
  • 1
  • 11

1 Answers1

0

This seems like an odd question to me, the answer should be whenever you create one, no? Straight from the documentation:

public class DictionaryOpenHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DICTIONARY_TABLE_NAME = "dictionary";
    private static final String DICTIONARY_TABLE_CREATE =
                "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
                KEY_WORD + " TEXT, " +
                KEY_DEFINITION + " TEXT);";

    DictionaryOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DICTIONARY_TABLE_CREATE);
    }
}
Bryan
  • 14,756
  • 10
  • 70
  • 125
  • this answer is not cleared me, i want just know when table is created in db, on time of app launch or on time of when we inserting data? – AndroidMob Jun 23 '16 at 12:52
  • @AndroidMob Whenever *you* as a developer decide to call `database.execSQL()` on a SQL string containing the `CREATE TABLE` statement. – Bryan Jun 23 '16 at 12:58
  • it means whenever i will call to create the instance of DictionaryOpenHelper then all that tables will be created right ? – AndroidMob Jun 23 '16 at 13:03
  • If you are using a class that extends `SQLiteOpenHelper`, and calling `database.execSQL()` in the `onCreate()` method (as shown in my answer), the `onCreate()` method will not be called until you call either `getReadableDatabase` or `getWriteableDatabase` for the [first time](http://stackoverflow.com/a/15597908/5115932). But, it is your choice if you want to use `SQLiteOpenHelper` or not (though it is recommended). – Bryan Jun 23 '16 at 13:18