Search code examples
androidsqliteandroid-sqlite

Android create sqlite database


I am having trouble with a simple file to create a sqlite db. I am logging some messages that I was expecting to show in the logs if the database is created or upgraded but they are not showing.

Here is the code:

package com.example.karim.myandoidapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class HomeActivity extends AppCompatActivity {

    String msg = "My android app log : ";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e(msg, "onCreate() event loading activity_home view");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Log.e(msg, "onCreate() event loading activity_home view");
        Context context = myandoidapp.getAppContext();
        MySQLiteHelper MySQLiteHelper = new MySQLiteHelper(context);
    }

    private class MySQLiteHelper extends SQLiteOpenHelper {

        // Database Version
        private static final int DATABASE_VERSION = 1;
        // Database Name
        private static final String DATABASE_NAME = "mydbnamesss";

        public MySQLiteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.e("DEBUG: ", "db created at " + db.getPath());
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.e("DEBUG: ", "db upgraded ");
        }

    }

}

Here is the output that I have in my logs:

06-14 15:02:29.122 6644-6644/com.example.karim.myandoidapp E/My android app log :: Opening HomeActivity

    --------- beginning of system
06-14 15:02:29.141 6644-6644/com.example.karim.myandoidapp E/My android app log :: onCreate() event loading activity_home view
06-14 15:02:29.203 6644-6673/com.example.karim.myandoidapp D/EGL_emulation: eglMakeCurrent: 0xa22050c0: ver 3 0 (tinfo 0xa2203360)
06-14 15:02:29.209 6644-6673/com.example.karim.myandoidapp D/EGL_emulation: eglMakeCurrent: 0xa22050c0: ver 3 0 (tinfo 0xa2203360)
06-14 15:02:29.216 6644-6673/com.example.karim.myandoidapp D/EGL_emulation: eglMakeCurrent: 0xa22050c0: ver 3 0 (tinfo 0xa2203360)
06-14 15:02:29.218 6644-6673/com.example.karim.myandoidapp D/OpenGLRenderer: endAllActiveAnimators on 0x8eeae180 (RippleDrawable) with handle 0xa2203ac0

I am adding here how I am getting the context in case it is relevant:

package com.example.karim.myandoidapp;

import android.content.Context; import android.app.Application;

public class myandoidapp extends Application {

    private static Context context;

    public void onCreate() {
        super.onCreate();
        myandoidapp.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return myandoidapp.context;
    }

}

Thanks


Solution

  • The onCreate method will only be called when an attempt is made to actually get/open the database. The simple fix would be to change :-

        public MySQLiteHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    

    to

        public MySQLiteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.getWritableDatabase(); //<<<< this.getReadableDatabase could also be used.  
        }
    

    If you want to always have a closed db, it could be better to use :-

        public MySQLiteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            SQLiteDatabase mydb = this.getWritableDatabase();
            mydb.close();
        }
    
    • There are many arguments as to whether or not you should keep opening/closing the database (personally I tend to keep it open)

    Then when you instantiate the Database Helper, using MySQLiteHelper MySQLiteHelper = new MySQLiteHelper(context);. An attempt is made to open the database.

    Note that the onCreate method is only called once for the lifetime of the database, so you would only get the log messages the first time the database is opened (you could delete the App's data or uninstall the App and this would then result in onCreate being called again).

    The onUpgrade method would only be called when the version number is increased and again not until an attempt is made to open/get the database.