Skip to content Skip to sidebar Skip to footer

Unable To Load Sqlite Database On First Run

I have created an app when I install and launch it for first time it closes automatically but when I open it after it works fine. If I clear data of the app it also closes for fir

Solution 1:

I believe that the database has in fact been copied and loaded (opened) successfully and rather that the issue is that the copied database does not have a table named Dictionary1 when attempting the following:-

Cursor cursor = sd.query("Dictionary1" ,null, null, null, null, null, null);

As per at com.elytelabs.myapplication.MainActivity.fetchData(MainActivity.java:142)

So you need to correct the table name to be according to the table in the copied database.

You may wish to make use of this Q&A to display database information.


The messages :-

11-2223:14:56.45513193-13193/com.elytelabs.myapplication E/SQLiteLog: (14) cannot open file at line 30052 of [b3bb660af9]
11-2223:14:56.45513193-13193/com.elytelabs.myapplication E/SQLiteLog: (14) os_unix.c:30052: (2) open(/data/data/com.elytelabs.myapplication/databases/dictionary.db) - 
11-2223:14:56.45513193-13193/com.elytelabs.myapplication E/SQLiteDatabase: Failed to open database '/data/data/com.elytelabs.myapplication/databases/dictionary.db'.
                                                                             android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

etc .........

result from when the checkDatabase method is invoked as the database hasn't at that time been found as it hasn't been copied over from the assets (as it should do).

The method uses the getReadableDatabase method to try to see if the database exists. This method always prints the stacktrace.

An alternative approach could be to replace :-

privatebooleancheckDataBase(){
     //  this.getReadableDatabase();SQLiteDatabasecheckDB=null;
        try{
            StringmyPath= DB_PATH ;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }catch(SQLiteException e){
            //database does't exist yet.
        }
        if(checkDB != null){

            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

with :-

privatebooleancheckDataBase(){
        Filedb=newFile(DB_PATH);
        if(db.exists()) returntrue;
        Filedir=newFile(db.getParent());
        if (!dir.exists()) {
            dir.mkdirs();
        }
        returnfalse;
    }

This attempts to see if the database file exists (and also creates an non-existing directories in the path e.g. in some circumstances the databases directory may not exist.)

If the above is used then your messages be more like :-

11-2223:14:56.52513193-13193/com.elytelabs.myapplication E/SQLiteLog: (1) no such table: Dictionary1
11-2223:14:56.52513193-13193/com.elytelabs.myapplication D/AndroidRuntime: Shutting down VM
11-2223:14:56.52513193-13193/com.elytelabs.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.elytelabs.myapplication, PID: 13193
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elytelabs.myapplication/com.elytelabs.myapplication.MainActivity}: android.database.sqlite.SQLiteException: no such table: Dictionary1 (code 1): , while compiling: SELECT * FROM Dictionary1
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5296)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
                                                                              Caused by: android.database.sqlite.SQLiteException: no such table: Dictionary1 (code 1): , while compiling: SELECT * FROM Dictionary1
                                                                                 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                 at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                 at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                 at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                 at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                                 at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                                 at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                                                                                 at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
                                                                                 at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
                                                                                 at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
                                                                                 at com.elytelabs.myapplication.MainActivity.fetchData(MainActivity.java:142)
                                                                                 at com.elytelabs.myapplication.MainActivity.onCreate(MainActivity.java:82)

Solution 2:

Try this code,

publicclassDatabaseHelperextendsSQLiteOpenHelper{

        privatestaticStringDB_NAME = "dictionary.db";
        privateSQLiteDatabase myDataBase;
        private final Context myContext;

        /**
         * Constructor
         * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
         */publicDatabaseHelper(Context context) {
            super(context, DB_NAME, null, 1);
            this.myContext = context;
        }

        @OverridepublicvoidonCreate(SQLiteDatabase db) {
          //write code create table.
        }

        @OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion{
        }

        // Add your public helper methods to access and get content from the database.// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy// to you to create adapters for your views.//add your public methods for insert, get, delete and update data in database.

}

Solution 3:

You can try like bellow like.. if you add any table just change the VERSION code. That means if add new table ,you have to change VERSION = 1 to VERSION = 2.

publicclassDatabaseHelperextendsSQLiteOpenHelper {

privatestaticfinalStringDBNAME="yourdbname.db";
privatestaticfinalintVERSION=1;

publicDatabaseHelper(Context context) {
    super(context, DBNAME, null, VERSION);
    // TODO Auto-generated constructor stub
}


@OverridepublicvoidonCreate(SQLiteDatabase db) {
    db.execSQL("create table tablename(tablename_id integer primary key autoincrement,name text)");

}

@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS tablename");
    onCreate(db);

  }

}

Post a Comment for "Unable To Load Sqlite Database On First Run"