我在数据库中执行一个简单的插入查询 但我得到了标题中所示的例外情况 求你帮帮我。
代码如下。
public void insertAutoAlbumDetails(String setId,String albumid, String Type,String albumName){
String query = "INSERT INTO "
+ ALBUM_DETAILS
+ "("
+ ALBUM_ID
+ ","
+ ALBUM_NAME
+ ","
+ ALBUM_TYPE
+ ","
+ USER_ID
+ ","
+ ALBUM_SETID
+ ") SELECT \""
+ albumid
+ "\",\""
+ albumName
+ "\",\""
+ Type
+ "\",\""
+ AppPreferences.getAppPreferences(context).getString(
AppConstants.USER_ID, "")
+ "\",\""
+ setId
+ "\" WHERE NOT EXISTS "
+ "("
+ "SELECT 1 FROM "
+ ALBUM_DETAILS
+ " WHERE "
+ ALBUM_ID
+ "=\""
+ albumid
+ "\" AND "
+ USER_ID
+ "=\""
+ AppPreferences.getAppPreferences(context).getString(
AppConstants.USER_ID, "") + "\")";
this.Query(query);//// i get exception on this line.
}
获得以下异常......
02-11 18:11:35.370: W/System.err(26820): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.app.quirl/databases/canvera.sqlite
02-11 18:11:35.370: W/System.err(26820): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
02-11 18:11:35.370: W/System.err(26820): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1648)
02-11 18:11:35.370: W/System.err(26820): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-11 18:11:35.370: W/System.err(26820): at com.app.quirl.db.GetDB.Query(GetDB.java:50)
02-11 18:11:35.370: W/System.err(26820): at com.app.quirl.db.AlbumDetailsDao.insertAutoAlbumDetails(AlbumDetailsDao.java:119)
02-11 18:11:35.370: W/System.err(26820): at com.app.quirl.GetAutoCountService.uploadImages(GetAutoCountService.java:108)
02-11 18:11:35.370: W/System.err(26820): at com.app.quirl.GetAutoCountService.access$0(GetAutoCountService.java:62)
02-11 18:11:35.370: W/System.err(26820): at com.app.quirl.GetAutoCountService$startUploadImage.doInBackground(GetAutoCountService.java:56)
02-11 18:11:35.370: W/System.err(26820): at com.app.quirl.GetAutoCountService$startUploadImage.doInBackground(GetAutoCountService.java:1)
02-11 18:11:35.370: W/System.err(26820): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-11 18:11:35.370: W/System.err(26820): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-11 18:11:35.370: W/System.err(26820): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-11 18:11:35.370: W/System.err(26820): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-11 18:11:35.370: W/System.err(26820): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-11 18:11:35.370: W/System.err(26820): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-11 18:11:35.370: W/System.err(26820): at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:0)
异常说:您正在尝试使用已关闭的连接来插入该值。在插入之前确保连接对数据库是开放的。
答案 1 :(得分:0)
我的问题最终出现在onCreate()
,我在执行db.close()
后呼叫db.execSQL(TABLE_CREATE)
。
onCreate
之后会自动调用 dbHelper.getReadableDatabase()
。
这导致它崩溃,因为它用一个已经关闭的对象从数据库中检索所有值。我从db.close()
删除onCreate
后,一切正常。
您可能会拨打dbHelper.getReadableDatabase()
,但这有用吗?