Deepa MG Deepa MG - 25 days ago 8
Android Question

android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

Here is my SQLiteHeler.java class.

//tables name in database
public static final String TABLE_NAME="Favtable";

//common column for all table
public static final String KEY_ID="id";
//column in alumnitable
public static final String KEY_CID="Courseid";
public static final String KEY_FavStatus="Status";

public SQLiteHelper(Context context) {

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

//create tables
@Override
public void onCreate(SQLiteDatabase database){

String CREATE_NAME="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_CID+" VARCHAR, "+KEY_FavStatus+" VARCHAR)";
database.execSQL(CREATE_NAME);
database.close();

}
public void InsertData(int Courseid,int flag)
{
Cursor cursor = null;
SQLiteDatabase database = this.getReadableDatabase();
cursor.moveToFirst();
SQLiteQuery = "INSERT INTO FavTable (Courseid,Status) VALUES('" + Courseid + "', '" + flag + "');";
database.execSQL(SQLiteQuery);
database.close();
}
public void close() {
if (database != null) {
database.close();
}
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
database.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);

onCreate(database);
}


}

What i am doing wrong?It giving error as

_android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here_


And i called create and insert methods of SQLiteHelper into my Fragment class by creating Object.

This is my CourseFragment.java.here i am calling onCreate and Insert Method

alertDialogBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0,
int arg1) {
flag = 1;


/*s1="true";*/
Courseid = mCourse.getCourseid();

mCourse.setIsFavCourse(!mCourse.getIsFavCourse());
mCourse.save();

// Update listview
mCourses.get(position).setIsFavCourse(
mCourse.getIsFavCourse());
courseListAdapter.notifyDataSetChanged();

//Add courseid and status to database
favourite.onCreate(database);
favourite.InsertData(Courseid,flag);

// Start sync service for course
Intent i = new Intent(context, MDroidService.class);

i.putExtra("notifications", false);
i.putExtra("siteid",
session.getCurrentSiteId());
i.putExtra("courseid",
mCourse.getCourseid());

context.startService(i);

}
});

Answer

After copying and running your code and then modifying it. The primary/initial issue is due to you closing the database when you create it in the onCreate method of the helper (commented out in code below).

However, as per the comments there are other issues, some of which the following code resolves. The following code works (as far as resolving the issue that you currently have) :-

public class SQLiteHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Courses";

    //tables name in database
    public static final String TABLE_NAME="Favtable";

    //common column for all table
    public static final String KEY_ID="_id";
    //column in alumnitable
    public static final String KEY_CID="Courseid";
    public static final String KEY_FavStatus="Status";

    public SQLiteHelper(Context context) {

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

    //create tables
    @Override
    public void onCreate(SQLiteDatabase database){

        String CREATE_NAME="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_CID+" VARCHAR, "+KEY_FavStatus+" VARCHAR)";
        database.execSQL(CREATE_NAME);
        //database.close();

    }
    public void InsertData(int Courseid,int flag)
    {
        SQLiteDatabase database = this.getWritableDatabase();
        String SQLiteQuery = "INSERT INTO FavTable (Courseid,Status) VALUES('" + Courseid + "', '" + flag + "');";
        database.execSQL(SQLiteQuery);
    }


    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(database);
    }
}

Here's the code/calls I've used in the activity:-

    SQLiteHelper mydb = new SQLiteHelper(this);
    mydb.InsertData(0,10);
    mydb.close();

You should not have this line in your code (as posted after I answered) :-

favourite.onCreate(database);