无法使用 RoomDatabase.query 更新 sqlite_sequence table
Not able to update sqlite_sequence table using RoomDatabase.query
我们尝试使用以下代码更新 sqlite_sequence
。
WeNoteRoomDatabase weNoteRoomDatabase = WeNoteRoomDatabase.instance();
weNoteRoomDatabase.query(new SimpleSQLiteQuery("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'attachment'"));
但是,一点作用都没有。我使用 SQLite 浏览器检查 sqlite_sequence
table 内容。计数器未重置为 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
我认为查询是错误的,你应该尝试下面的查询
weNoteRoomDatabase.query(new SimpleSQLiteQuery("UPDATE sqlite_sequence SET seq = 0 WHERE name = attachment"));
Table sql_sequence
不受 Room 管理,因此您需要使用 SupportSQLiteDatabase
.
对其进行编辑
试试这个:
String sqlQuery = "DELETE FROM sqlite_sequence WHERE name='attachment'";
weNoteRoomDatabase().getOpenHelper().getWritableDatabase().execSQL(sqlQuery);
Room
不使用 SQLiteDatabase - but it uses SupportSQLiteDatabase, while it's source code 状态,它 delegates all calls to an implementation of {@link SQLiteDatabase}
... 我什至可以进一步挖掘 - 但我确信,这是一个一致性功能并且不是错误。
SQLiteDatabase.execSQL()
仍然可以正常工作,但是对于 SupportSQLiteDatabase.execSQL()
,针对内部 table 的相同 UPDATE
或 DELETE
查询无效并且不会抛出错误。
我的 MaintenanceHelper
在 GitHub. it is important that one initially lets Room
create the database - then one can manipulate the internal tables with SQLiteDatabase.execSQL()
. while researching I've came across annotation @SkipQueryVerification 可用,可能 允许 UPDATE
或 DELETE
在 table ] sqlite_sequence
;我只成功地用 Dao
执行了 SELECT
... 从公开可用 API;所有的操纵尝试都被默默地忽略了。
我使用的是房间数据库版本2.2.5
这里我无法使用 Room Dao 结构执行此查询,因此制作一个简单的 class 和这样的访问方法,我得到了成功的结果,所以这是测试结果。我正在使用 RxJava 和 RxAndroid。
public class SqlHelper {
private static SqlHelper helper = null;
public static SqlHelper getInstance() {
if (helper == null) {
helper = new SqlHelper();
}
return helper;
}
public Completable resetSequence(Context context) {
return Completable.create(emitter -> {
try {
AppDatabase.getDatabase(context)
.getOpenHelper()
.getWritableDatabase()
.execSQL("DELETE FROM sqlite_sequence WHERE name='<YOUR_TABLE_NAME>'");
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
});
}
}
Execute:
SqlHelper.getInstance()
.resetQuizSequence(context)
.subscribeOn(Schedulers.io()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {}, error -> {});
这对我有用 - 房间 2.2.6
String sqlQuery = "DELETE FROM sqlite_sequence WHERE name='attachment'";
<YourDatabase>.getInstance(mContext).getOpenHelper().getWritableDatabase().execSQL(sqlQuery);
我们尝试使用以下代码更新 sqlite_sequence
。
WeNoteRoomDatabase weNoteRoomDatabase = WeNoteRoomDatabase.instance();
weNoteRoomDatabase.query(new SimpleSQLiteQuery("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'attachment'"));
但是,一点作用都没有。我使用 SQLite 浏览器检查 sqlite_sequence
table 内容。计数器未重置为 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
我认为查询是错误的,你应该尝试下面的查询
weNoteRoomDatabase.query(new SimpleSQLiteQuery("UPDATE sqlite_sequence SET seq = 0 WHERE name = attachment"));
Table sql_sequence
不受 Room 管理,因此您需要使用 SupportSQLiteDatabase
.
试试这个:
String sqlQuery = "DELETE FROM sqlite_sequence WHERE name='attachment'";
weNoteRoomDatabase().getOpenHelper().getWritableDatabase().execSQL(sqlQuery);
Room
不使用 SQLiteDatabase - but it uses SupportSQLiteDatabase, while it's source code 状态,它 delegates all calls to an implementation of {@link SQLiteDatabase}
... 我什至可以进一步挖掘 - 但我确信,这是一个一致性功能并且不是错误。
SQLiteDatabase.execSQL()
仍然可以正常工作,但是对于 SupportSQLiteDatabase.execSQL()
,针对内部 table 的相同 UPDATE
或 DELETE
查询无效并且不会抛出错误。
我的 MaintenanceHelper
在 GitHub. it is important that one initially lets Room
create the database - then one can manipulate the internal tables with SQLiteDatabase.execSQL()
. while researching I've came across annotation @SkipQueryVerification 可用,可能 允许 UPDATE
或 DELETE
在 table ] sqlite_sequence
;我只成功地用 Dao
执行了 SELECT
... 从公开可用 API;所有的操纵尝试都被默默地忽略了。
我使用的是房间数据库版本2.2.5
这里我无法使用 Room Dao 结构执行此查询,因此制作一个简单的 class 和这样的访问方法,我得到了成功的结果,所以这是测试结果。我正在使用 RxJava 和 RxAndroid。
public class SqlHelper {
private static SqlHelper helper = null;
public static SqlHelper getInstance() {
if (helper == null) {
helper = new SqlHelper();
}
return helper;
}
public Completable resetSequence(Context context) {
return Completable.create(emitter -> {
try {
AppDatabase.getDatabase(context)
.getOpenHelper()
.getWritableDatabase()
.execSQL("DELETE FROM sqlite_sequence WHERE name='<YOUR_TABLE_NAME>'");
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
});
}
}
Execute:
SqlHelper.getInstance()
.resetQuizSequence(context)
.subscribeOn(Schedulers.io()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {}, error -> {});
这对我有用 - 房间 2.2.6
String sqlQuery = "DELETE FROM sqlite_sequence WHERE name='attachment'";
<YourDatabase>.getInstance(mContext).getOpenHelper().getWritableDatabase().execSQL(sqlQuery);