Java – getReadableDatabase() does not create a new database after deleting an android database

getReadableDatabase() does not create a new database after deleting an android database… here is a solution to the problem.

getReadableDatabase() does not create a new database after deleting an android database

I deleted the android database by executing rm/data/data/app/databases/database. After deleting it, I tried to run the application again, but it throws an exception because the table hasn’t been created yet. The exception I get is E/AndroidRuntime:FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to create service wificontroller. WifiControllerService: android.database.sqlite.SQLiteException: There is no such table:

I

create the table in

public class DataStore extends SQLiteOpenHelper{

private static final String TAG = "Database";
private static final String DatabaseName = "Database";
private static final String NICKNAME_TABLE_NAME = "nicknames";
private static final String SCAN_TABLE_NAME = "scans";
private static final int Version = 2;

private static final String NICKNAME_TABLE_CREATE = "Query I cant show"

private static final String SCAN_TABLE_CREATE = "Query I cant show"

public DataStore(Context context) {
    super(context, DatabaseName , null, Version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    Log.d(TAG, "Creating database");

try {
        db.execSQL(NICKNAME_TABLE_CREATE);
        db.execSQL(SCAN_TABLE_CREATE);
    }
    catch (Exception e){
        Log.e(TAG, "Could not create database", e);
        Log.e(TAG, "Query 1: " + NICKNAME_TABLE_CREATE);
        Log.e(TAG, "Query 2: " + SCAN_TABLE_CREATE);
    }

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(TAG, "On Upgrade called");
}
public String[] getScanColumns(){
    return new String[]{"accessPoints", "clients", "status","timeOfStartScan", "packetsFound"};
}
public String getDatabaseName(){
    return DatabaseName;
}
public String getScanTableName(){
    return SCAN_TABLE_NAME;
}

The method of calling the database is

SQLiteDatabase database = dbHelper.getReadableDatabase();

Cursor mCursor = database.rawQuery("SELECT QUERY", null); THIS IS THE LINE THAT THROWS THE EXCEPTION

try {
    if (mCursor != null) {
        mCursor.moveToFirst();

Log.w(Tag, "Packets found on last scan = " + mCursor.getString(mCursor.getColumnIndex("packetsFound")));

Log.i(Tag, "Successfully loaded database scan");
     } else {
        Log.w(Tag, "Could not retrieve scan from database");
     }
 }
 catch (Exception e){
    Log.w(Tag, "Could not retrieve scan data from database probably because it doesn't exist");
}
finally {
        mCursor.close();
}

I have verified that neither it was called at create time nor when upgrading. I’m sorry I can’t post their proprietary queries, but I know they compile correctly because this has been working for a long time.

I have tried reinstalling the app and deleting all user data, but nothing works. Any ideas on why I suddenly can’t query my database anymore.

Solution

In case others encounter this problem as well. My initial failure was because the database didn’t know that the database had changed and I needed to increment the version, as mentioned above, but when my on create failed before trying to execute all the queries, I ran into a very similar issue. One of the queries was terrible, I failed the first time I executed it, but every time after that it didn’t retry calling Create. In short, if Oncreate fails once, then you have to completely remove the app from your phone and try again.

Related Problems and Solutions