Reputation: 2079
I am getting following error even I have closed all instances as soon as I finish calling them:
close() was never explicitly called on database '/data/data/com.click4tab.fragmentvogella/databases/NewOrderDB'
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
I was not getting this error until I defined this method.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(MainActivity.this, "Button one pressed",
Toast.LENGTH_LONG).show();
// write data on server
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
try {
mDbHelper.writeUnwrittenNetOrder();
mDbHelper.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
And the definition to writeUnwrittenNetOrder:
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
StringBuffer strbuf = new StringBuffer();
params.add(new BasicNameValuePair("tag", "add_NetOrderID"));
for (mCur.moveToFirst(); !mCur.isAfterLast(); mCur.moveToNext()) {
String sql3 = "INSERT into NetOrderID (StoreID, Date, SalesManID, NetOrderID) VALUES ("
+ mCur.getString(0)
+ ", "
+ mCur.getString(1)
+ ", "
+ mCur.getString(2) + ", " + mCur.getString(3) + ");";
if((mCur.isLast())){
strbuf.append(sql3);
}
else {
strbuf.append(sql3 + "#");
}
// String sql3 =
// "INSERT into NetOrderID (StoreID, SalesManID, NetOrderID) VALUES (1,1,80)";
// sqlQueries.add(sql3);
Log.e("sql", strbuf.toString());
}
params.add(new BasicNameValuePair("query", strbuf.toString()));
Log.e("param","param added");
new SyncWithServer();
// send params
SyncWithServer.setParams(params);
Log.e("param","param set");
// return arraylist
}
Where should I close the mDbHelper to avoid this error, well this error is coming up in Logcat while the database is getting successfully written and the Application is not crashing.
Upvotes: 0
Views: 645
Reputation: 2994
Alex makes a good point.
And also (but I don't think that's what's causing the error right now): I think it is better to take the mDbHelper.close()
statement out of the try/catch, or put it in a finally
clause. Because currently, when writeUnwrittenNetOrder
throws an exception, mDbHelper.close()
will never be called. Something comparable will yields for when you're adding the cursor close method as Alex suggested: this should also happen regardless of exceptions that occur between open and close.
Upvotes: 0
Reputation: 11400
The error message says you did not close database or cursor. Did you close all of them as well (mCur
object)?
Upvotes: 1