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

发布于 2024-12-11 04:08:51 字数 2532 浏览 0 评论 0原文

我试图在 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;
}

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

I'm trying to insert a row in a sqlite database's table, but when I call the insertOrThrow() method (Class Sqlitedatabase) i get this exception:
"Application did not close the cursor or database object that was opened here"

I don't understand why:

here's the code for the main class:

         ........
         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);

These are the main methods for DbAdapter Class:

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;
}

Anyone has an idea for what the problem could be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲歌长辞 2024-12-18 04:08:51

您应该在末尾添加 mDbHelper.close()

You should add mDbHelper.close() at end.

风蛊 2024-12-18 04:08:51

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

you have to add a mDbHelper.close(); when you are sure that I do not need more a mDbHelper.
I have solved same issue by doing this. I hope is working for u.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文