我想在sqlite數據庫的表中插入一行,但當我調用insertOrThrow()方法(類Sqlitedatabase)時,我得到此異常: 「應用程序沒有關閉遊標或在這裏打開的數據庫對象」android sqlite異常遊標或數據庫未關閉
我不明白爲什麼:
這裏的主類代碼:
........
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;
}
任何人都有什麼問題的想法?
關閉您的數據庫在活動的停止或銷燬方法。 mDbHelper.close() – user370305
只爲每個應用程序創建一個數據庫實例,參考:http://stackoverflow.com/questions/7444327/how-to-initialize-sqlite-database-once-from-a-helper-class-in -android/7446028#7446028 –
我該怎麼做?我創建了多少個數據庫實例? –