Reputation: 296
There is a class inside AbstractDbAdapter
which extends SQLiteOpenHelper
. When I call it, it calls the default constructor but does not enter the onCreate()
function. I created DB successfully manually so I am sure about all SQL queries. Why isn't it calling the onCreate?? Is it something to do with class inside a class??
public abstract class AbstractDbAdapter {
public static DatabaseHelper mDbHelper = null;
public static SQLiteDatabase mDb = null;
private static int mReferenceCount = 0;
public static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context,DATABASE_TABLE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
mDb.execSQL(SCREATE_SOME_TABLE);
//"Database creation successful"
}
catch(Exception ex) {
//"Database creation failed"
}
// "Database creation successful"
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
mDb.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_NAME);
this.onCreate(mDb);
}
}
/**
*
*
* @return this
* @throws SQLException
* if the database could be neither opened or created
*/
public AbstractDbAdapter open() throws SQLException {
if( mReferenceCount == 0 ) {
if( mDbHelper == null )
mDbHelper = new DatabaseHelper(//getting main activity context);
if( mDb == null || mDb.isOpen() == false )
mDb = mDbHelper.getWritableDatabase();
}
mReferenceCount++;
return this;
}
Upvotes: 0
Views: 606
Reputation: 1
onCreate
/ onUpgrade
is called when you call getWritableDatabase
/getReadableDatabase
Upvotes: 0
Reputation: 18592
inside your onCreate() and onUpgrade() you are using the wrong database reference. You should be using the database reference that these method receive as their parameters which in your case is db but you are using mDb which is wrong
Upvotes: 0
Reputation: 152867
onCreate()
is called when you call getWritableDatabase()
or getReadableDatabase()
on the helper and the database file does not exist. If the file is already there and the version number is the requested one, no callback such as onCreate()
is invoked.
For what it's worth, it's also a bad idea to catch exceptions in onCreate()
. If the method returns successfully (doesn't throw), the framework thinks the database was created successfully.
Upvotes: 3