How can I reset a autoincrement sequence number in sqlite

后端 未结 5 1665
广开言路
广开言路 2020-12-01 15:22

How to update table sqlite_sequence in Ormlite ? I just need update seq. How can I get that table via ORMLite ?

EDIT

I can\'t find ORLite t

相关标签:
5条回答
  • 2020-12-01 15:34

    Building on Marcos Vasconcelos' answer:

    UPDATE sqlite_sequence SET seq = (SELECT MAX(col) FROM Tbl) WHERE name="Tbl"
    

    This query will set seq to the largest value in the col identity column in the Tbl table, so there is no risk of violating constraints.

    0 讨论(0)
  • 2020-12-01 15:34
    UPDATE SQLITE_SEQUENCE SET SEQ= 'value' WHERE NAME='table_name';
    
    0 讨论(0)
  • 2020-12-01 15:36

    Inside your .db file there's an table called sqlite_sequence

    Each row has two columns name which is the name of the table seq a integer indicating the current last value at this table

    You can update it to 0

    But beware if your table use this id as the unique identifier.

    0 讨论(0)
  • 2020-12-01 15:41

    If you want to issue general database commands in ORMLite, you can use the updateRaw method. See the javadocs. There is also executeRaw for other commands.

    lessonDao.updateRaw("delete from 'lesson';");
    lessonDao.updateRaw("delete from sqlite_sequence where name='lesson';");
    weekDefinitionDao.updateRaw("delete from 'weekdefinition';");
    weekDefinitionDao.updateRaw(
        "delete from sqlite_sequence where name='weekdefinition';");
    

    You could also drop and recreate the table as well:

    TableUtils.dropTable(WeekDefinition.class);
    TableUtils.dropTable(Lesson.class);
    TableUtils.createTable(Lesson.class);
    TableUtils.createTable(WeekDefinition.class);
    

    I think the real question is why is your application depending on this database internal number? It really shouldn't care.

    • How about not displaying the number at all so it can be 1 or 1001 and your application won't matter?
    • You could also never remove the lessons at all but maybe add a hidden boolean field. So if they get re-added, the hidden field could be set to false and Math would still be at id #1.
    0 讨论(0)
  • 2020-12-01 15:55

    This worked for me in my database : (I Set the id before one, then after deleting one row, when I add new data row again, the auto increment serial remain ok ) :

     public void  updateSerialNumber ( long memberId){
            String query = "UPDATE SQLITE_SEQUENCE SET SEQ= '"+(memberId-1)+"' WHERE NAME='"+ReportBigHelper.TABLE_MEMBER+"'";
            database.execSQL(query);
        }
    
    0 讨论(0)
提交回复
热议问题