I’m creating a new database in my Activity like this:
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate called");
super.onCreate(savedInstanceState);
Log.d(TAG, "create DatabaseOpenHelper");
DatabaseOpenHandler helper = new DatabaseOpenHandler(this);
Log.d(TAG, "get readable database access");
database = helper.getReadableDatabase();
Log.d(TAG, "create Cursor for database access");
Cursor data = database.query(DatabaseConstants.TABLE_NOTES, fields,
null, null, null, null, null);
Log.d(TAG, "create SimpleCursorAdapter for ListView");
dataSource = new SimpleCursorAdapter(this,
R.layout.conference_note_item, data, fields,
new int[] { R.id.note_text });
Log.d(TAG, "set ListAdapter");
setListAdapter(dataSource);
}
but my DatabaseOpenHandler doesn’t call the onCreate method.
public DatabaseOpenHandler(Context context) {
super(context, DatabaseConstants.DATABASE_NAME, null,
DatabaseConstants.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "onCreate called");
db.execSQL(DatabaseConstants.CREATE_TABLE_NOTES);
}
My SQL query:
public static final String CREATE_TABLE_NOTES = "CREATE TABLE "
+ TABLE_NOTES + " (" + NOTE_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_CONFERENCE
+ " INTEGER, " + NOTE_DATE + " INTEGER, " + NOTE_TEXT
+ " INTEGER);";
The
SQLiteOpenHelper.onCreate()lifecycle method is only called when the database doesn’t already exist. It gets executed the first time when there is no database present. Assuming there are no errors in your code, change the name of the database in your dbadapter and it would get executed.