Java – Android SQLite, onCreate() is not called

Android SQLite, onCreate() is not called… here is a solution to the problem.

Android SQLite, onCreate() is not called

I’ve been working hard to create a SQLite database in my Android app.
I’ve seen a lot of tutorials, as well as a lot of existing issues about Stack Overflow and other sites.

This 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 can’t get SQLite onCreate to appear, and can’t find the database anywhere in file explorer or on the device itself, either a simulated device or a real device.

Thanks a lot for any help and apologies for the format of the code!

Solution

I recommend using the following in an activity (for now): –

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 review the logs. You should see output from modules_table and sqlite_sequence (the latter is because you’ve already written autoincrements.)

sqlite_master is a system table that stores system information, such as table and index names, or schemas.

Append – Access the database file

On devices that are not rooted, every application data (data/data) is protected, so you will not be able to see the database file.

On the emulator, it depends on the emulator. I believe later versions of Android Studio now allow access, e.g. :-

enter image description here

  • Note that the above is Android 10.1 Pie (API 28), so the database has write-ahead logging (WAL), so the -shm and -wal files are also present.

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

    • As you can see, cache directory, then I suggest that for some reason (probably a failure) the database does not exist, but you do have access to However, when checked through the virtual appliance file explorer, the only subfolder in my package is cached.
    • Perhaps, trying to rerun on the device (be careful to reselect the device in Device Explorer as it won’t refresh) could be another reason why you don’t see the database.

Related Problems and Solutions