为什么我在SQLite中完成了一个未被停用或关闭错误的游标?

时间:2012-08-29 12:03:14

标签: android database sqlite exception cursor

我是Android的菜鸟,我遇到了令人沮丧的IllegalArgumentException。 logcat说完成一个尚未停用或关闭的游标。我已经阅读了有关stackoverflow的所有相关问题,但仍然无法弄清楚发生了什么。非常感谢任何帮助。

带来错误的代码:

    public void add(){
    boolean didItWork = true;
    try {
        //String name = sqlName.getText().toString();
        //String hotness = sqlHotness.getText().toString();
        calculateaddentry();
        String coin = addcoinselection.getSelectedItem().toString();
        String quantity = sqlAddCoin.getText().toString();
        String ounces = entryounces;
        String value = denomination + newentryvalue;


        //PortfolioDatabase entry = new PortfolioDatabase(Portfolio.this);
        info.open();
        info.createEntry(coin, quantity, ounces, value);
        info.close();

        info.open();
        String data = info.getData();
        info.close();
        info.open();
        String ccoin = info.getCoin();
        info.close();
        info.open();
        String cqty = info.getQty();
        info.close();
        info.open();
        String counces = info.getOunces();
        info.close();
        info.open();
        String cvalue = info.getValue();
        info.close();

        currentcoin.setText(ccoin);
        currentqty.setText(cqty);
        currentounces.setText(counces);
        currentvalue.setText(cvalue);

    } catch (Exception e) {
        didItWork = false;
        String error = e.toString();
        Dialog d = new Dialog(this);
        d.setTitle("Dang it!");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
    } finally {
        if (didItWork) {
            /*Dialog d = new Dialog(this);
            d.setTitle("Heck Yea!");
            TextView tv = new TextView(this);
            tv.setText("Success");
            d.setContentView(tv);
            d.show();*/
        }
    }}

我的PortfolioDatabase java:

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "cointype_name";
public static final String KEY_QUANTITY = "cointype_quantity";
public static final String KEY_OUNCES = "cointype_ounces";
public static final String KEY_VALUE = "cointype_value";

private static final String DATABASE_NAME = "PortfolioDatabase";
private static final String DATABASE_TABLE = "cointypeTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_QUANTITY  + " TEXT NOT NULL, " +
                KEY_OUNCES + " TEXT NOT NULL, " +
                KEY_VALUE + " TEXT NOT NULL);"
        );  
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }       
}

public PortfolioDatabase(Context c){
    ourContext = c;
}

public PortfolioDatabase open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();      
    return this;
}
 public void close(){
     ourHelper.close();

 }

public long createEntry(String coin, String quantity, String ounces, String value) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, coin);
    cv.put(KEY_QUANTITY, quantity);
    cv.put(KEY_OUNCES, ounces);
    cv.put(KEY_VALUE, value);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iQuantity = c.getColumnIndex(KEY_QUANTITY);
    int iOunces = c.getColumnIndex(KEY_OUNCES);
    int iValue = c.getColumnIndex(KEY_VALUE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + /*c.getString(iRow) + " " +*/ c.getString(iName) + " " + c.getString(iQuantity) + " " + c.getString(iOunces) + " " + c.getString(iValue) + "\n";
    }
    c.close();

    return result;
}

public String getCoin() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iQuantity = c.getColumnIndex(KEY_QUANTITY);
    int iOunces = c.getColumnIndex(KEY_OUNCES);
    int iValue = c.getColumnIndex(KEY_VALUE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + /*c.getString(iRow) + " " +*/ c.getString(iName).substring(0, Math.min(18, c.getString(iName).length())) + "\n";
    }
    c.close();
    return result;
}

    public String getQty() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iQuantity = c.getColumnIndex(KEY_QUANTITY);
        int iOunces = c.getColumnIndex(KEY_OUNCES);
        int iValue = c.getColumnIndex(KEY_VALUE);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + /*c.getString(iRow) + " " +*/ c.getString(iQuantity) + "\n";
        }           
    c.close();
    return result;
}

    public String getOunces() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iQuantity = c.getColumnIndex(KEY_QUANTITY);
        int iOunces = c.getColumnIndex(KEY_OUNCES);
        int iValue = c.getColumnIndex(KEY_VALUE);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + /*c.getString(iRow) + " " +*/  c.getString(iOunces) + "\n";
        }
        c.close();
        return result;
    }

    public String getValue() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iQuantity = c.getColumnIndex(KEY_QUANTITY);
        int iOunces = c.getColumnIndex(KEY_OUNCES);
        int iValue = c.getColumnIndex(KEY_VALUE);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + /*c.getString(iRow) + " " +*/ c.getString(iValue) + "\n";
        }           
        c.close();
    return result;
}



public String getSingleValue(String aCoin) throws SQLException{
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE};     
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iQuantity = c.getColumnIndex(KEY_QUANTITY);
    int iOunces = c.getColumnIndex(KEY_OUNCES);
    int iValue = c.getColumnIndex(KEY_VALUE);

    if (c != null){
        c.moveToFirst();
        result= c.getString(0);
        return result;
    }
    c.close();
    return null;
}

public String getHotness(long l) throws SQLException{
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_VALUE };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
    if (c != null){
        c.moveToFirst();
        String hotness = c.getString(2);
        return hotness;
    }
            c.close();
    return null;
}

//public void updateEntry(String mCoin, String mQuantity, String mValue) throws SQLException{
    // TODO Auto-generated method stub
//  ContentValues cvUpdate = new ContentValues();
//  cvUpdate.put(KEY_NAME, mCoin);
//  cvUpdate.put(KEY_QUANTITY, mQuantity);
//  cvUpdate.put(KEY_VALUE, mValue);
//  ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + mCoin, null); 
//}

public void updateEntry(String mCoin, String mQuantity, String mOunces, String mValue) throws SQLException{
    // TODO Auto-generated method stub
    String[] whereArgs = new String[] { mCoin };
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_NAME, mCoin);
    cvUpdate.put(KEY_QUANTITY, mQuantity);
    cvUpdate.put(KEY_OUNCES, mOunces);
    cvUpdate.put(KEY_VALUE, mValue);
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=?", whereArgs);   
}

public void changeCurrency(String mCoin, String mValue) throws SQLException{
    // TODO Auto-generated method stub
    String[] whereArgs = new String[] { mCoin };
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_NAME, mCoin);      
    cvUpdate.put(KEY_VALUE, mValue);
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=?", whereArgs);   
}

//public void deleteEntry(String coin) throws SQLException{
    // TODO Auto-generated method stub
//  ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=" + coin, null);
//}

public void deleteEntry(String coin) throws SQLException{
    String[] whereArgs = new String[] { coin };
    ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=?", whereArgs);
}

public void removeAll()
{
    /*// db.delete(String tableName, String whereClause, String[] whereArgs);
    // If whereClause is null, it will delete all rows.
    SQLiteDatabase db = helper.getWritableDatabase(); // helper is object extends SQLiteOpenHelper
    db.delete(DatabaseHelper.TAB_USERS, null, null);
    db.delete(DatabaseHelper.TAB_USERS_GROUP, null, null);*/
    ourDatabase.delete(DATABASE_TABLE, null, null);

}

logcat的:

08-29 04:20:45.583: E/Cursor(1899): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.example.portfolio/databases/PortfolioDatabase, table = cointypeTable, query = SELECT _id, cointype_name, cointype_quantity, cointype_ounces, cointype_value FROM cointypeTable
08-29 04:20:45.583: E/Cursor(1899): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
08-29 04:20:45.583: E/Cursor(1899):     at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
08-29 04:20:45.583: E/Cursor(1899):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
08-29 04:20:45.583: E/Cursor(1899):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
08-29 04:20:45.583: E/Cursor(1899):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
08-29 04:20:45.583: E/Cursor(1899):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
08-29 04:20:45.583: E/Cursor(1899):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
08-29 04:20:45.583: E/Cursor(1899):     at com.example.portfolio.PortfolioDatabase.getData(PortfolioDatabase.java:79)
08-29 04:20:45.583: E/Cursor(1899):     at com.example.portfolio.Portfolio.fillcoinstoadd(Portfolio.java:1472)
08-29 04:20:45.583: E/Cursor(1899):     at com.example.portfolio.Portfolio$9.onClick(Portfolio.java:575)
08-29 04:20:45.583: E/Cursor(1899):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
08-29 04:20:45.583: E/Cursor(1899):     at android.os.Handler.dispatchMessage(Handler.java:99)     08-29 04:20:45.583: E/Cursor(1899):  at android.os.Looper.loop(Looper.java:123)
08-29 04:20:45.583: E/Cursor(1899):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-29 04:20:45.583: E/Cursor(1899):     at java.lang.reflect.Method.invokeNative(Native Method)
08-29 04:20:45.583: E/Cursor(1899):     at java.lang.reflect.Method.invoke(Method.java:521)
08-29 04:20:45.583: E/Cursor(1899):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-29 04:20:45.583: E/Cursor(1899):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-29 04:20:45.583: E/Cursor(1899):     at dalvik.system.NativeStart.main(Native Method)
08-29 04:20:48.413: W/KeyCharacterMap(1899): No keyboard for id 0

2 个答案:

答案 0 :(得分:0)

要克服此问题,请在返回结果之前关闭getData Method()中的光标。

c.close()
return result;

答案 1 :(得分:0)

getHotness()中的光标永远不会关闭。

作为一般说明,即使抛出异常,也应确保游标关闭。这意味着将close()调用放在finally块中,如下所示:

Cursor c = ...
try {
    // use the cursor
} finally {
    c.close();
}