在sqlite java android上从中删除1行列后如何重置自动增量值?

斋月启示:

在MYSQLI上,此方法对我有效,然后ALTER TABLE tablename AUTO_INCREMENT = 0将在最后一个自动增量位置重置

但我不知道如何在SQLITE上执行此操作,所以我的想法是,我想删除1行列表,同时我想将自动增量值重置为最后一个位置,这是我的代码

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            getName = parent.getItemAtPosition(position).toString();
            getID = myDB.getID(getName);
            getIDtoString = String.valueOf(getID);

            AlertDialog.Builder builder = new AlertDialog.Builder(Notepad.this);
            builder.setMessage("You Want To Delete "+getName+" From Notepad?").setPositiveButton("Yes Please!", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean isDeleted = myDB.deleteLol(getIDtoString,getName);
                    if (isDeleted) {
                        toastMessage("Delete Success!");
                        refresh();
                    } else {
                        toastMessage("Delete Failed!");
                    }
                }
            }).setNegativeButton("No, Don't Do That!", null);

            AlertDialog alertDialog = builder.create();
            alertDialog.show();

        return true;
        }
    });

这是我的DatabaseHelper类

public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table usersData(id integer primary key autoincrement, username text, email text, password text)");
    db.execSQL("create table notepadData(id integer primary key autoincrement, notepad text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists usersData");
    db.execSQL("drop table if exists notepadData");
    onCreate(db);
}

public boolean deleteLol(String id,String notepad) {
    SQLiteDatabase myDB = this.getWritableDatabase();
    myDB.delete("notepadData", "id = ?",new String[] {id});
    myDB.execSQL("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE notepad = '"+notepad+"'");// i found this on internet recently and doesn't work
    return true;
}
阿蒂什·阿格罗瓦尔

SQLite将最后ROW-ID一个存储在一个表中SQLITE_SEQUENCE,该由SQLite自动管理。即使删除或清空其他表,该表中的值仍会保存。

有两种方法可以重置自动增量计数器。

  1. 删除整个表,然后重新创建它。(也许使用虚拟的临时表来保存当前数据)。从SQLITE_SEQUENCE元表中删除有关表的信息。
DELETE from table;
DELETE from sqlite_sequence where name='table';
  1. 使用更新查询更新sqlite_sequence中的序列
update sqlite_sequence set seq=5 where name='table';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章