-1

I have a problem with database connection in my Android application. Strange thing is that everything was working fine. One fine morning, I started getting this problem. And with the existing well running applications as well.

I believe it is a kind of environment issue, but cant track it. Need your help guys. I have checked existing posts in SOF. Tried all suggested, nothing helping.

My DB connection program :

  package com.intw.jokes;

  import java.util.ArrayList;
  import java.util.List;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteException;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.util.Log;

  public class DatabaseUtil extends SQLiteOpenHelper{

public SQLiteDatabase DB;
public String DBPath;
public static String DBName = "JOKES";
public static final int version = '1';
public static Context currentContext;;


public DatabaseUtil(Context context) {
    super(context, DBName, null, version);
    currentContext = context;
    DBPath = "/data/data/" + context.getPackageName() + "/databases/";

}

public SQLiteDatabase getDatabaseObject(){
    return this.getWritableDatabase();
}

public void closeDBConnection(SQLiteDatabase DB){
    if(null != DB)
        DB.close();
}


@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void createDatabase() {
    boolean dbExists = checkDbExists();
    //dbExists = false;
    if (dbExists) {
        // do nothing
    } else {
        DB = currentContext.openOrCreateDatabase(DBName, 0, null);

        DB.close();
    }
}




private boolean checkDbExists() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DBPath + DBName;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {

    }
    boolean test = checkDB != null ? true : false; 
    if (checkDB != null) {
        checkDB.close();
    }
    return test;
}

}

and my log :

    03-21 10:13:56.844: E/Trace(780): error opening trace file: No such file or directory (2)
    03-21 10:13:59.794: E/SQLiteLog(780): (14) cannot open file at line 30174 of [00bb9c9ce4]
    03-21 10:13:59.794: E/SQLiteLog(780): (14) os_unix.c:30174: (2) open(/data/data/com.intw.jokes/databases/JOKES) - 
    03-21 10:13:59.834: E/SQLiteDatabase(780): Failed to open database '/data/data/com.intw.jokes/databases/JOKES'.
    03-21 10:13:59.834: E/SQLiteDatabase(780): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at com.intw.jokes.DatabaseUtil.checkDbExists(DatabaseUtil.java:111)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at com.intw.jokes.DatabaseUtil.createDatabase(DatabaseUtil.java:49)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at com.intw.jokes.AllJokesActivity.configureDatabase(AllJokesActivity.java:201)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at com.intw.jokes.AllJokesActivity.onCreate(AllJokesActivity.java:80)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.app.Activity.performCreate(Activity.java:5008)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.os.Handler.dispatchMessage(Handler.java:99)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.os.Looper.loop(Looper.java:137)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at android.app.ActivityThread.main(ActivityThread.java:4745)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at java.lang.reflect.Method.invoke(Method.java:511)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    03-21 10:13:59.834: E/SQLiteDatabase(780):  at dalvik.system.NativeStart.main(Native Method)
    03-21 10:14:00.534: D/dalvikvm(780): GC_CONCURRENT freed 304K, 9% free 7416K/8135K, paused 16ms+27ms, total 130ms
    03-21 10:14:00.534: D/dalvikvm(780): WAIT_FOR_CONCURRENT_GC blocked 11ms
    03-21 10:14:01.894: D/gralloc_goldfish(780): Emulator without GPU emulation detected.
Bhabani Shankar
  • 1,267
  • 4
  • 22
  • 41
  • Why are you using `SQLiteOpenHelper` without implementing `onCreate`/`onUpdate`? – CL. Mar 21 '16 at 08:08

1 Answers1

1

There is another way you can check database if exists. As answered by Andre Rocha, Check the database like here inside checkDbExists() No need to be so complex as you are currently being now,

private boolean checkDbExists() {    
    File file= currentContext.getDatabasePath(DBName);
    return file.exists();
}
Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • Thanks for the response. I am not getting the error any more. But my insert statements are not working. this.DB = getWritableDatabase(); if (count <= 0) { this.DB.execSQL("INSERT INTO " + champTableName + " Values ('" + statsDTO.getLevelNumber() + "','" + statsDTO.getTime() + "');"); } this.DB.close(); – Bhabani Shankar Mar 21 '16 at 19:37
  • The problem is that it was a working code. Not only this application, I have 50 add applications having the same issue. It could be something with environment. But not sure what – Bhabani Shankar Mar 22 '16 at 03:26
  • That is ok bro.. but, what about this one ? Is it solved or still some issue left – Shree Krishna Mar 22 '16 at 11:37
  • 1
    I am trying to reconfigure my Android. Let's see. – Bhabani Shankar Mar 25 '16 at 03:12