Skip to content Skip to sidebar Skip to footer

Close() The Cursor And Db When Use The Sqlite Database

In my application I use the sqlite Database,When I run the application Logcat display me close() was never explicitly called on database '/data/data/com.MAT.CanadaImmigrationApp/d

Solution 1:

After calling the:-

DB_Adapter.addToDB(Chapters,Question,CorrectAnswer,Answer1,Answer2,Answer3); 
**db.close();**

Solution 2:

There are two ways of closing the Cursor and Database object.

First Way Close both the object after using it.(i.e whenever the process get completed that relate to your cursor)

Second Way

Add it in OnStop() or onDestroy().

In OnStop() or onDestroy() Check whether the cursor and database object is null. If not then close it.

public void onStop(){
 if(db!=null)
   {db.close();}
 if(cur!=null)
  { cur.close();}
}

It Should work.

Solution 3:

use this method to create data base

my constructor

public DBClass(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		this.context = context;
		
		
		
	
		
	}

@Override
	public void onCreate(SQLiteDatabase db) {
		
		context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
		db.execSQL("CREATE TABLE IF NOT EXISTS prediction(country1 VARCHAR,Country2 VARCHAR,winner VARCHAR,pool VARCHAR);");
	}

then in your database accessing functions use like these

public void databaseInsertOperations(String c1, String c2, String c3) {
		db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(DBClass.COUNTRY_1, c1);
		cv.put(DBClass.COUNTRY_2, c2);
		cv.put(DBClass.WINNER, c3);
		db.insert(DBClass.TABLE, null, cv);
		db.close();
	}

	public ArrayList<String> databaseRetriveOperation() {

	
		db = this.getWritableDatabase();
		
		ArrayList<String> buffer = new ArrayList<String>();

cursor = db.rawQuery("select * from prediction", null);

		if (cursor.getCount() == 0) {
			System.out.println("Error No records found");

		} else {

			while (cursor.moveToNext()) {
				buffer.add(cursor.getString(0) + "/" + cursor.getString(1)
						+ "/" + cursor.getString(2));
			}
			
		}
		
		db.close();		cursor.close();
		return buffer;

	}

worked out for me when i got this error

Solution 4:

Try this scenario:

When ever you want to access with the database help function or fire any query then use to open Database.

After using that function or query, always close that database.

e.g:

db.open(); // function to open database for the use
db.YOUR_QUERY or ANY_FUNCTION
db.close();

Open and close of database is use to open databse in perticular mode like writable/readable and close it.

You can use below function in to databaseHelper class to Open and Close the database.

//---opens the database---public DBAdapter2 open() throws SQLException 
{
        db = DBHelper.getWritableDatabase();

    returnthis;
}

//---closes the database---    publicvoidclose() 
{
    DBHelper.close();
}

Hope it will help you.

Updated

change this:

    question_Set.add(c.getString(2).trim());
    Answers_Set.add(c.getString(3).trim());
    Option1.add(c.getString(4).trim());
    Option2.add(c.getString(5).trim());
    Option3.add(c.getString(6).trim());

with below:

        db.open();
    question_Set.add(c.getString(2).trim());
    db.close();
    db.open();
    Answers_Set.add(c.getString(3).trim());
    db.close();
    db.open();
    Option1.add(c.getString(4).trim());
    db.close();
    db.open();
    Option2.add(c.getString(5).trim());
    db.close();
    db.open();
    Option3.add(c.getString(6).trim());
    db.close();

Post a Comment for "Close() The Cursor And Db When Use The Sqlite Database"