Not able to update sqlite_sequence table using RoomDatabase.query

前端 未结 4 1318
一整个雨季
一整个雨季 2020-12-06 10:58

We try to update sqlite_sequence with the following code.

WeNoteRoomDatabase weNoteRoomDatabase = WeNoteRoomDatabase.instance();
weNoteRoomDatab         


        
相关标签:
4条回答
  • 2020-12-06 11:35

    i think query is wrong, you should try below query

    weNoteRoomDatabase.query(new SimpleSQLiteQuery("UPDATE sqlite_sequence SET seq = 0 WHERE name = attachment"));
    
    0 讨论(0)
  • 2020-12-06 11:37

    I'm using room database version 2.2.5

    Here I'm unable to execute this query using Room Dao structure, so make one simple class and access method as like this and I got successful outcomes so this one is tested result. I'm using RxJava and RxAndroid for same.

    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 -> {});
    
    0 讨论(0)
  • 2020-12-06 11:41

    Room doesn't use SQLiteDatabase - but it uses SupportSQLiteDatabase, while it's source code states, that it delegates all calls to an implementation of {@link SQLiteDatabase}... I could even dig further - but I'm convinced, that this is a consistency feature and not a bug.

    SQLiteDatabase.execSQL() still works fine, but with SupportSQLiteDatabase.execSQL() the same UPDATE or DELETE queries against internal tables have no effect and do not throw errors.

    my MaintenanceHelper is available on 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, which could possibly permit UPDATE or DELETE on table sqlite_sequence; I've only managed to perform a SELECT with Dao... which in general all hints for the internal tables are being treated as read-only, from the perspective of the publicly available API; all manipulation attempts are being silently ignored.

    0 讨论(0)
  • 2020-12-06 11:50

    Table sql_sequence is not managed by Room, so you need to edit it using a SupportSQLiteDatabase.

    Try this:

    String sqlQuery = "DELETE FROM sqlite_sequence WHERE name='attachment'";
    
    weNoteRoomDatabase().getOpenHelper().getWritableDatabase().execSQL(sqlQuery);
    
    0 讨论(0)
提交回复
热议问题