【发布时间】:2019-05-03 13:33:56
【问题描述】:
我们尝试使用以下代码更新sqlite_sequence。
WeNoteRoomDatabase weNoteRoomDatabase = WeNoteRoomDatabase.instance();
weNoteRoomDatabase.query(new SimpleSQLiteQuery("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'attachment'"));
但是,它根本没有效果。我使用 SQLite 浏览器检查 sqlite_sequence 表内容。计数器未重置为 0。
如果我们尝试在同一个 SQLite 文件上使用 SQLite 浏览器手动运行相同的查询,它工作得很好。
我们的房间数据库非常简单。
@Database(
entities = {Attachment.class},
version = 6
)
public abstract class WeNoteRoomDatabase extends RoomDatabase {
private volatile static WeNoteRoomDatabase INSTANCE;
private static final String NAME = "wenote";
public abstract AttachmentDao attachmentDao();
public static WeNoteRoomDatabase instance() {
if (INSTANCE == null) {
synchronized (WeNoteRoomDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
WeNoteApplication.instance(),
WeNoteRoomDatabase.class,
NAME
)
.build();
}
}
}
return INSTANCE;
}
}
知道我们错过了什么吗?
其他信息:clearing sqlite_sequence is not working in android room
【问题讨论】:
-
name 的确切条件是什么?
name = attachment时会这样吗?为什么你使用name = \"attachment\"?为什么不name = 'attachment'? -
name = \"attachment\"和name = 'attachment'没有什么不同。如果我使用 Window SQLite 浏览器对其进行测试,两者都可以使用。但是,问题不在于 WHERE 条件。如果我执行weNoteRoomDatabase.query(new SimpleSQLiteQuery("UPDATE sqlite_sequence SET seq = 0"));,sqlite_sequence仍然没有被更新。 Room 似乎在处理sqlite_sequence表时遇到问题。 -
您能在后台线程上执行此操作吗?我不确定,但到目前为止,我只是使用后台线程来更新并在房间数据库中写入一些东西
-
是的,在测试过程中,查询是在后台线程中执行的。
-
你为什么不使用 DAO 类? Official docs也建议使用。
标签: android android-room