android sqlite异常游标或数据库未关闭

时间:2011-10-19 09:41:55

标签: java android sqlite

我正在尝试在sqlite数据库的表中插入一行,但是当我调用insertOrThrow()方法(类Sqlitedatabase)时,我得到以下异常: “应用程序未关闭此处打开的游标或数据库对象”

我不明白为什么:

这是主类的代码:

         ........
         ContentValues values = new ContentValues();
         values.put("nome", info.getString("nome"));
         values.put("ingredienti", info.getJSONObject("ingredienti").toString());
         values.put("descrizione", info.getString("descrizione"));
         values.put("persone", info.getString("persone"));
         values.put("tempo", info.getString("tempo"));
         values.put("facilita", info.getString("facilita"));
         if(info.getString("url_foto")!="/images/ricettapredefinita.jpg")
            values.put("foto", true);
         else
            values.put("foto", false);
         values.put("categoria", info.getString("categoria"));
         values.put("zona", info.getString("regione"));
         values.put("ethnos", info.getString("etnica"));
         values.put("voto", info.getString("voto"));
        // Open database
        DbAdapter mDbHelper = new DbAdapter(Main.this);
        mDbHelper.open_w();
        long ritorno=mDbHelper.createRecipe(values);

这些是DbAdapter类的主要方法:

private static final String DATABASE_CREATE =
    "CREATE TABLE recipes (" +
    "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
    "nome VARCHAR(255) NOT NULL," +
    "ingredienti TEXT NOT NULL," +
    "descrizione TEXT " +
    "persone SMALLINT," +
    "tempo TINYINT(3)," +
    "facilita SMALLINT," +
    "foto BOOL," +
    "voto TINYINT(3)," +
    "categoria VARCHAR(255)," +
    "zona VARCHAR(255)," +
    "ethnos VARCHAR(255));"; 
public long createRecipe(ContentValues info) {
    return mDb.insertOrThrow(DATABASE_TABLE, null, info);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS notes");
        onCreate(db);
    }
}
public DbAdapter open_w() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

任何人都知道问题可能是什么?

2 个答案:

答案 0 :(得分:1)

您最后应添加mDbHelper.close()

答案 1 :(得分:1)

你必须添加一个mDbHelper.close();当你确定我不需要更多的mDbHelper时。 这样做我解决了同样的问题。我希望能为你工作。