Android SQLite, onCreate() not being called

Connor C :

I've been struggling to create a SQLite DB within my Android application. I've looked at numerous tutorials, and quite a few existing questions on stack overflow and other sites.

Here is my DatabaseHelper class

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

public SQLiteDatabase db;
public static final String DATABASE_NAME = "user.db";

//Module table
public static final String MODULE_TABLE = "modules_table";
public static final String MODULE_COL_1 = "ID";
public static final String MODULE_COL_2 = "CODE";
public static final String MODULE_COL_3 = "TITLE";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    Log.d("SQL", "SQLite dbhelper");
    db = getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    //db.execSQL("create table " + MODULE_TABLE + "(" + MODULE_COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + MODULE_COL_2 + " TEXT, " + MODULE_COL_3 + " TEXT " +")");
    db.execSQL("create table modules_table (ID INTEGER PRIMARY KEY 
AUTOINCREMENT, CODE TEXT, TITLE TEXT)");
    Log.d("SQL", "SQLite onCreate");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + MODULE_TABLE);
    onCreate(db);
}
}

I've managed to get SQLite dbhelper to appear in logcat, but cannot get SQLite onCreate to appear, and cannot find the db anywhere in the file explorer or the device itself, both emulated and real device.

Any help would be greatly appreciated, and apologies for the formatting of the code!

MikeT :

I'd suggest using the following (temporarily) in the activity :-

DatabaseHelper myDBHelper = new DatabaseHelper(this); //<<<<<<<<< you appear to already have the equivalent of this line (if so use whatever variable name you have given to the DatabaseHelper object)

Cursor csr = myDBHelper.getWritableDatabase().query("sqlite_master",null,null,null,null,null,null);
DatabaseUtils.dumpCursor(csr);
csr.close();

Run and then check the log. You should see output for your modules_table and also sqlite_sequence (the latter because you have coded autoincrement.

sqlite_master is a system table that stores system information, such as table and index names i.e. the schema.

Additional - access to the database file

On a device that isn't rooted each applications data (data/data) is protected so you won't be able to see the database file.

On an emulator, it depends upon the emulator. I believe later versions of Android studio do now allow access e.g. :-

enter image description here

  • Note the above is Android 10.1 Pie (API 28) and hence the database has Write-Ahead Logging (WAL) and thus the -shm and -wal files also exist.

  • The package is mjt.pvcheck. The full path is data/data/mjt.pvcheck/databases.

    • As you can see cache directory, then I'd suggest that for some reason, perhaps a failure, the database doesn't exist, but you do appear to have access as per however upon checking through the virtual device file explorer the only sub folder I have within my package is the cache.
    • Perhaps, try rerunning on the device (note in device explorer re-select the device as it doesn't refresh), which may be another reason why you didn't see the database.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=80641&siteId=1