I am writing an android shopping manager application and have come across an error when trying to close one of my SQLite database tables.
ItemsDb idb = new ItemsDb(this);
idb.open();
ArrayList<String> itemNames = idb.getItemNames();
for(int i=0; i < itemNames.size(); i++){
String itemName = itemNames.get(i);
String itemID = idb.getItemID(itemName);
String itemName = idb.getItemNames().get(i);
String itemPrices = idb.getItemPrices().get(i);
String itemQuantity = idb.getItemQuantities().get(i);
String dateBought = idb.getDateBought(itemName);
String decayRate = idb.getDecayRate(itemName);
String decayType = idb.getDecayType(itemName);
String lastDecay = idb.getLastDecay(itemName);
String prevQuantity = idb.getPreviousQuantity(itemName);
}
idb.close();
This doesn't happen with other calls to this class so and I am wondering if it is because there is a loop with a lot of calls to the database here.
The error is SQLite Exception: unable to close due to unfinalised statements
.
The error line from the "ItemsDb" class is here
public ItemsDb(Context c){
ourContext = c;
}
public ItemsDb open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
Apparently SQLite3 has a finalize method for destroying previous DB calls but I am not sure how to implement this or even if it is necessary here.
Any help on this would be great.
It seems the problem was cursor related, thanks Jivings. I wasn't closing the cursor after I queried the database, which meant certain references to the database were invalid.
public String getName(String id) throws SQLException{
String[] columns = new String[]{ KEY_ItemID, KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ItemID + "='" + id + "'", null, null, null, null);
if(c != null){
c.moveToFirst();
String name = c.getString(1);
c.close();
return name;
}
return null;
}
Calling "c.close" seemed to do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With