Android Room Database-无法将元素添加到列表列

时间:2019-07-12 17:19:37

标签: android sqlite sql-update android-room android-architecture-components

我正在我的应用程序中使用Room Persistence库,并且遇到了一个奇怪的错误,不确定如何解决。我有一个包含两个ArrayList类型的字段的实体。我已经实现了TypeConverters来告诉Room如何以可读的方式存储列表。在我的dao中,当我要修改列表中的任何元素时,可以使用Query和Update方法来更新列表:

    E/SQLiteLog: (1) near "?": syntax error
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
    Process: com.example.workoutlog, PID: 5413
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:354)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        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)
     Caused by: android.database.sqlite.SQLiteException: near "?": syntax error (code 1 SQLITE_ERROR): , while compiling: UPDATE exercise_table SET reps = ?,? WHERE id =?
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1086)
        at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.compileStatement(FrameworkSQLiteDatabase.java:64)
        at androidx.room.RoomDatabase.compileStatement(RoomDatabase.java:317)
        at com.example.workoutlog.db.ExerciseDao_Impl.updateRepsList(ExerciseDao_Impl.java:254)
        at com.example.workoutlog.db.ExerciseRepository$updateRepsAsyncTask.doInBackground(ExerciseRepository.java:103)
        at com.example.workoutlog.db.ExerciseRepository$updateRepsAsyncTask.doInBackground(ExerciseRepository.java:92)
        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) 

这是什么时候变得很奇怪:仅当我第一次将对象插入数据库时​​,当我修改的元素在列表的原始大小范围内时,Room才能成功执行更新。如果我将元素添加到任一列表中,请在新添加的元素上调用set(),然后使用Query方法更新数据库中的列表列,应用程序崩溃,并出现以下错误:

{{1}}

但是,当我使用Update方法时,我没有收到错误,它看起来好像Room能够成功地通过添加新元素来更新列表。但是,当我从数据库中重新加载对象时,新添加的元素消失了,列表又回到了其原始大小。有谁知道为什么会这样和/或解决方法?

0 个答案:

没有答案