Android SQLite异常:由于未定义的语句而无法关闭

Jon*_*Jon 9 database sqlite android

我正在编写一个Android购物管理器应用程序,并在尝试关闭我的一个SQLite数据库表时遇到错误.

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();
Run Code Online (Sandbox Code Playgroud)

对于这个类的其他调用不会发生这种情况,所以我想知道是否因为这里有一个循环,有很多对数据库的调用.错误是SQLite Exception: unable to close due to unfinalised statements.

"ItemsDb"类的错误行在这里

    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();
    }
Run Code Online (Sandbox Code Playgroud)

显然,SQLite3有一个用于销毁以前的DB调用的finalize方法,但我不知道如何实现它,或者即使这里有必要.

对此的任何帮助都会很棒.

Jon*_*Jon 6

看来这个问题与光标有关,感谢Jivings.我在查询数据库后没有关闭游标,这意味着对数据库的某些引用无效.

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;
}
Run Code Online (Sandbox Code Playgroud)

称"c.close"似乎可以解决问题.