gpt4 book ai didi

java - Room - 删除查询后数据库被锁定

转载 作者:行者123 更新时间:2023-12-01 20:09:15 26 4
gpt4 key购买 nike

我正在尝试从数据库中删除一行。当我第一次删除它时,该行被成功删除。在我尝试删除另一行后,出现数据库未打开/已锁定的错误。我已经发布了 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();
}

最佳答案

我已经通过这种方式解决了这个问题。

我没有使用单例类来插入或删除,而是直接调用此代码:

 WordLayListAppDatabase db = Room.databaseBuilder(getApplicationContext(),
WordLayListAppDatabase.class, "wordLayList").build();
db.wordLayListDao().DeleteWordLayListByObject(o);

现在,我使用单例类来仅获取数据。为了更新数据库,我使用上面的代码(上面的代码总是在异步任务中调用)。

关于java - Room - 删除查询后数据库被锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58970801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com