在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自动管理。即使删除或清空其他表,该表中的值仍会保存。
有两种方法可以重置自动增量计数器。
DELETE from table; DELETE from sqlite_sequence where name='table';
update sqlite_sequence set seq=5 where name='table';
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句