Search code examples
androiddatabasesqlitedatabase-locking

SQLite Database Locked in DialogFragment onClick event


People say that it may be caused when you don't close cursor or database. But I have something strange. I try to open database on OK button click in a dialogfragment window (resultClass) in MainActivity in the beginning of app.

The method that calls DialogFragment window

@SuppressLint("NewApi")
    public void vasya(View v){
    DialogFragment dlg_rec=new resultClass();
     dlg_rec.show(getFragmentManager(), "dlg_rec");}

in my DialogFragment class

public Score_DS dsoop=new Score_DS(getActivity());

..
case R.id.button1:

        dsoop.open();// Here comes the crash
           ...
            dsoop.close();

Score_DS is my DataSource class to operate with database.

public class Score_DS {
    ContentValues cv= new ContentValues();
    DB dbHelper;
    SQLiteDatabase db;
     public Score_DS(Context context) {
            dbHelper = new DB(context);
          }
    public void open() throws SQLException {
        if (dbHelper != null) {
            db = dbHelper.getWritableDatabase();
        }
    }
...
      (here I have a couple of methods that I've never called yet)
}

DB.java is this

package com.wordpress.jimmaru.tutorial.SimpleGame;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class DB extends SQLiteOpenHelper implements BaseColumns{

    private static final String DATABASE_NAME = "mygame.db";
    private static final int DATABASE_VERSION = 1;
    private static DB sInstance;
    ContentValues cv;
     public DB(Context context) {

         super(context, DATABASE_NAME, null, DATABASE_VERSION);}



    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE Records(_id INTEGER PRIMARY KEY,   Player_Name VARCHAR(50), Player_Score VARCHAR(50));");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
        // TODO Auto-generated method stub

    }

}

So, again. I call DialogFragment window in the very beginning of my app by clicking on a button. When I click on OK button in my dialogfragment window, the app crashes and shows that Database is locked. I closed everything (db.close). I have no cursors opened. in MainActivity I have another database related method.

Score_DS dseed=new Score_DS(this);
...
public void clearall(View v)
    {
         dseed.open();
         dseed.execution("DELETE FROM Records");
          dseed.close();
    }

And it works just fine. What can be wrong?


Solution

  • It's not "database is locked" but rather NullPointerException in getDatabaseLocked(). It is caused because you passed a null Context to SQLiteOpenHelper here:

    public Score_DS dsoop=new Score_DS(getActivity());
    

    When the fragment object is instantiated, it's not yet attached to an activity and getActivity() returns null. Postpone the Score_DS instantiation to e.g. onAttach() or later in the fragment lifecycle.