0

I have try this code on DatabaseHelper.java

Line Number 34

public DatabaseHelper(Context context) throws IOException {
    super(context, DB_NAME, null, 1);
    this.mycontext = context;
    boolean dbexist = checkdatabase();
    if (dbexist) {
        // System.out.println("Database exists");
        opendatabase();
    } else {
        // System.out.println("Database doesn't exist");
        createdatabase();
    }
}

Line number 64

private boolean checkdatabase() {
    // SQLiteDatabase checkdb = null;
    boolean checkdb = false;
    try {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        checkdb = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE) != null;
        checkdb = dbfile.exists();
    } catch (SQLiteException e) {
        System.out.println("Database doesn't exist");
    }

    return checkdb;
}

Line number 44

On create method menu.java

try {
        db = new DatabaseHelper(this);
    } catch (IOException e2) {

        e2.printStackTrace();
    }

    try {
        db.createdatabase();
    } catch (IOException e) {

        e.printStackTrace();
    }
     db.getReadableDatabase();

    db.opendatabase();
    Random rand = new Random();
    randomJokId = rand.nextInt(603 - 1) + 1;
    cur = db.jokOfTheDay(randomJokId);
    cur.moveToFirst();
    String fullJok = cur.getString(cur.getColumnIndex("body"));
    String Jok = "";
    Jok = fullJok.substring(0, 55);
    jok_of_the_day.setText(Jok + "...  Read more");

    Log.d(TAG, "" + randomJokId);
}

Getting this error message

Another important things, I have this error message occurs three time.

01-19 13:49:41.284: ERROR/Database(10932): close() was never explicitly called on database '/data/data/com.horror.android/databases/jokesdatabase.sql' 
01-19 13:49:41.284: ERROR/Database(10932): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-19 13:49:41.284: ERROR/Database(10932):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
01-19 13:49:41.284: ERROR/Database(10932):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
01-19 13:49:41.284: ERROR/Database(10932):     at com.horror.android.DatabaseHelper.checkdatabase(DatabaseHelper.java:64)
01-19 13:49:41.284: ERROR/Database(10932):     at com.horror.android.DatabaseHelper.createdatabase(DatabaseHelper.java:45)
01-19 13:49:41.284: ERROR/Database(10932):     at com.horror.android.UltimateJokesMenu.onCreate(UltimateJokesMenu.java:51)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-19 13:49:41.284: ERROR/Database(10932):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 13:49:41.284: ERROR/Database(10932):     at android.os.Looper.loop(Looper.java:130)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-19 13:49:41.284: ERROR/Database(10932):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 13:49:41.284: ERROR/Database(10932):     at java.lang.reflect.Method.invoke(Method.java:507)
01-19 13:49:41.284: ERROR/Database(10932):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-19 13:49:41.284: ERROR/Database(10932):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 13:49:41.284: ERROR/Database(10932):     at dalvik.system.NativeStart.main(Native Method)
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
  • [Found answer here][1]: "Make DBManager a singleton." [1]: http://stackoverflow.com/questions/7424585/close-was-never-explicitly-called-on-database#comment-9002897 – Ivan May 04 '12 at 14:39

5 Answers5

1

When your data base use became over then better approach to use SQLiteDatabase.close().Close cursor also cursor.close();

For cursor use startmanagincursor(cursor).just after assigning it some value

And remember sometimes if you donot close database then it can create severe problem

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
0

Closing in onPause is just enough as it precedes onStop on the activities lifecycle. Using startManagingCursor besides being deprecated it may carry you problems of handling cases of updating view. I see two issues to assess:

  • Closing db. Is enough to follow the answer given here Android error - close() was never explicitly called on database, that is:

    Literally understand database is not normally closed, the actual because repeated instantiation your database, or connect you have been set up, and you and try to open another connection there will be no exception. So the solution is, make sure that you open only a connection.

    In small words: each time you open a db connection close it. This can be implemented be assuring that there is only one connection, you can for example use the method isOpen or assure that each time you open it is closed.

  • Closing cursors, as I said before using startManagingCursor is not good idea. Instead you should use the support library and using the CursorLoader approach, it involves using a ContentProvider but if you think is an overkill you may read about CursorLoader usage without ContentProvider

Community
  • 1
  • 1
Andres Felipe
  • 625
  • 7
  • 24
0

Try calling SQLiteDatabase.close() at the end of your try in the checkDatabase method.

Jivings
  • 22,834
  • 6
  • 60
  • 101
0

use db.close(); to close your Database and cur.close(); to close your cursor after you end working with them.

a fair player
  • 11,530
  • 9
  • 46
  • 48
0

Close your database on the onStop() method and use startmanagingcursor() with all your cursors to handle its lifecycle automatically.

Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47