我正在尝试从数据库中删除一行。当我第一次删除它时,该行被成功删除。尝试删除另一行后,出现错误,提示数据库未打开/已锁定。我已经发布了logcat和代码。
此外,如果我第一次删除一行,它将被删除;当我关闭该应用程序并再次将其重新打开并删除另一行时,它将起作用。仅当我在不关闭应用程序的情况下继续删除另一行时,才会出现错误。
这是日志:
Caused by: java.lang.IllegalStateException: The database '/data/user/0/com.basusingh.blingoo/databases/wordLayList' is not open.
at android.database.sqlite.SQLiteDatabase.throwIfNotOpenLocked(SQLiteDatabase.java:2943)
at android.database.sqlite.SQLiteDatabase.createSession(SQLiteDatabase.java:564)
at android.database.sqlite.-$$Lambda$RBWjWVyGrOTsQrLCYzJ_G8Uk25Q.get(Unknown Source:2)
at java.lang.ThreadLocal$SuppliedThreadLocal.initialValue(ThreadLocal.java:284)
at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:180)
at java.lang.ThreadLocal.get(ThreadLocal.java:170)
at android.database.sqlite.SQLiteDatabase.getThreadSession(SQLiteDatabase.java:558)
at android.database.sqlite.SQLiteProgram.getSession(SQLiteProgram.java:123)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeUpdateDelete(FrameworkSQLiteStatement.java:46)
at com.basusingh.blingoo.Database.WordLayList.WordLayListDao_Impl.DeleteWordLayList(WordLayListDao_Impl.java:94)
at com.basusingh.blingoo.Activity.WordLayListViewer$1doTask.doInBackground(WordLayListViewer.java:156)
at com.basusingh.blingoo.Activity.WordLayListViewer$1doTask.doInBackground(WordLayListViewer.java:148)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
E/ROOM: Cannot run invalidation tracker. Is the db closed?
android.database.sqlite.SQLiteException: no such table: room_table_modification_log (code 1 SQLITE_ERROR[1]): , while compiling: SELECT * FROM room_table_modification_log WHERE invalidated = 1;
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1229)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:703)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1865)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1840)
at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.query(FrameworkSQLiteDatabase.java:161)
at androidx.room.RoomDatabase.query(RoomDatabase.java:328)
at androidx.room.RoomDatabase.query(RoomDatabase.java:311)
at androidx.room.InvalidationTracker$1.checkUpdatedTable(InvalidationTracker.java:415)
at androidx.room.InvalidationTracker$1.run(InvalidationTracker.java:389)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
这是DAO代码:
@Dao
public interface WordLayListDao {
@Query("DELETE FROM WordLayListItems WHERE uid = :id")
void DeleteWordLayList(int id);
}
和其他班级
public class WordLayListDatabaseClient {
private Context mCtx;
private static WordLayListDatabaseClient mInstance;
private WordLayListAppDatabase appDatabase;
private WordLayListDatabaseClient(Context mCtx) {
this.mCtx = mCtx;
appDatabase = Room.databaseBuilder(mCtx, WordLayListAppDatabase.class, "wordLayList").build();
}
public static synchronized WordLayListDatabaseClient getInstance(Context mCtx) {
if (mInstance == null) {
mInstance = new WordLayListDatabaseClient(mCtx);
}
return mInstance;
}
public WordLayListAppDatabase getAppDatabase() {
return appDatabase;
}
}
@Database(entities = {WordLayListItems.class}, version = 1)
public abstract class WordLayListAppDatabase extends RoomDatabase {
public abstract WordLayListDao wordLayListDao();
}
这是我调用数据库的方式:
private void deleteLayList(){
class doTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute(){
progressDialog.setTitle("Please wait");
progressDialog.show();
}
@Override
protected String doInBackground(Void... aa){
WordLayListDatabaseClient.getInstance(WordLayListViewer.this).getAppDatabase().wordLayListDao().DeleteWordLayList(o.getID());
return null;
}
@Override
protected void onPostExecute(String var){
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "WordLay List Deleted", Toast.LENGTH_LONG).show();
setResult(RESULT_OK);
finish();
}
}
doTask dt = new doTask();
dt.execute();
}
答案 0 :(得分:0)
我已经通过这种方式解决了这个问题。
我不是使用单例类来插入或删除,而是直接直接调用以下代码:
WordLayListAppDatabase db = Room.databaseBuilder(getApplicationContext(),
WordLayListAppDatabase.class, "wordLayList").build();
db.wordLayListDao().DeleteWordLayListByObject(o);
现在,我正在使用singleton类来仅获取数据。为了更新数据库,我使用了上面的代码(以上始终在异步任务中调用)。