Resetting Autoincrement in Android SQLite

前端 未结 4 1732
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 10:53

I have this method which will remove all rows from a table but I also want it to reset the autoincrement so that when a new row is added it will start again. The SQL stateme

相关标签:
4条回答
  • 2020-12-31 11:36

    I was trying too hard.

    Finally, I got this answer code...

    Book.deleteAll(Book.class);
    book=new Book(user, pass);
    book.setId(Long.valueOf(book.listAll(Book.class).size()));
    book.save();
    

    this code work like delete and recreating the table.and reset id.

    good luck

    0 讨论(0)
  • 2020-12-31 11:48

    you'll need single quotes around your table name, i.e.

    db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + TABLE_NAME + "'");
    
    0 讨论(0)
  • 2020-12-31 11:56

    you can also use delete() method of SQLiteDatabase, like this

    db.delete("SQLITE_SEQUENCE","NAME = ?",new String[]{TABLE_NAME});
    
    0 讨论(0)
  • 2020-12-31 11:57

    Building on dldnh's answer:

    The following 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.

    UPDATE sqlite_sequence SET seq = (SELECT MAX(col) FROM Tbl) WHERE name="Tbl"
    
    0 讨论(0)
提交回复
热议问题