Using SQLiteOpenhelper inside another class - Does not call OnCreate()

Friday, May 2, 2014

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;ls

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;
}






http://ift.tt/1nSmsss