db.execSQL("CREATE TABLE " +DBTable0+ "("+ROW2+" INTEGER PRIMARY KEY AUTOINCREMENT, "+ROW3+" VARCHAR NOT NULL, "+ROW0+" TEXT NOT NULL, "+ROW1+" VARCHAR NOT NULL)");
这是我的数据库的创建表命令。
我想重置ROW2的值。
例如,我在此表上有5条记录。
1,save1,这是一个Save1,11.25
2,save2,这是一个Save2,23.48
3,save3,这是一个Save3,09.45
4,save4,这是一个Save4,11.55
5,save5,这是一个Save5,21.00
我要删除save2。当我删除它或进行其他删除时,我想重置ROW2索引,所以
1,save1,这是一个Save1,11.25
2,save3,这是一个Save3,09.45
3,save4,这是一个Save4,11.55
4,save5,这是一个Save5,21.00
喜欢这个...
我该怎么做。?
答案 0 :(得分:1)
AUTOINCREMENT
关键字的重点是永远不要重复使用rowid。它存储分配给表的最高位,并且在插入没有在insert语句中明确指定rowid的新行时,选择一个比存储的高的数字。如果没有AUTOINCREMENT
,它只会选择一个比表中当前最大rowid高的数字(如果最大值是可能的最大有符号64位整数,它将随机选择一些较小的整数,以希望之前缺少一个整数)放弃)。
如果要回收行号,您要么手动找到丢失的行号,然后在UPDATE
语句中明确使用它们,要么没有INTEGER PRIMARY KEY
列,然后运行VACUUM
删除行后。如果没有INTEGER PRIMARY KEY
列作为rowid的别名,则sqlite将在这种情况下重新排列它们。也可以将减去INTEGER PRIMARY KEY
列的表的行复制到新表中以获取新的行ID。所有这些都是效率极低的方法,因此不理想。生活在已删除行中的空白处。
必须阅读以了解rowid,INTEGER PRIMARY KEY
列和AUTOINCREMENT
:
https://www.sqlite.org/rowidtable.html
https://www.sqlite.org/lang_createtable.html#rowid
https://www.sqlite.org/autoinc.html
我将通过ListViews选择记录。然后,我可以删除和更新所选项目上的记录。因此,当我删除中间的任何记录时。 ID正在混合并删除错误记录
听起来像您在假设某个列表的位置与行的rowid相同吗?难怪你遇到问题了。明确跟踪rowid,并在删除请求的数据时使用它。 XY problem的经典案例。
答案 1 :(得分:0)
要在使用AUTOINCREMENT时重置为补偿已删除行而分配的 rowid ,您必须进行两项更改:-
这根本不是可取的做法,并且会大大加剧使用AUTOINCREMENT的效率低下(当很有可能不需要使用AUTOINCREMENT时)。
AUTOINCREMENT关键字强加了额外的CPU,内存,磁盘空间和磁盘I / O开销,如果没有严格要求,应避免。通常不需要。
实际上,永远不要依靠 rowid
除SQlite分配的内容之外的任何东西
<column_name> INTEGER PRIMARY KEY
(具有或不具有AUTOINCREMENT)的名称和 rowid 的别名。)我想重置ROW2的值。
我建议您重新评估您的意愿,仅在实际上有需要的情况下才想要。
我将通过ListViews选择记录。然后,我可以删除和更新所选项目上的记录。因此,当我删除中间的任何记录时。 ID正在混合并删除错误记录
如果您使用CursorAdapter,例如SimpleCursorAdapter,onItemClick和onItemLongClick的第四个参数是 id 。
请注意,要使用CursorAdapter,必须在名为Liststrong的Cursor中存在一个名为 _id 的列,并且该列应包含 rowid 。您可以使用 BaseColumns._ID ,这是一个值为 _id 的常量。
通常,您将表中的列定义为_id INTEGER PRIMARY KEY
或者,您可以使用rowid AS _id, *
,在这种情况下, _id 列将作为所有其他列的补充。
可以找到here
的其他适配器的此选项和其他选项/植入物但是,如果您坚持要求,那么以下示例可以满足我的要求。
注意:此示例对2个表执行相同的操作(有效)。
主要代码位于数据库帮助程序(SQLiteOpenHelper的子类)中,即 DBHelper.java :-
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TBL_TABLE1 = "table1";
public static final String TBL_TABLE2 = "table2";
public static final String COL_TABLE_COL1 = "col1";
public static final String COL_TABLE_COL2 = "col2";
public static final String COL_TABLE_COL3 = "col3";
private static final String crt_table1_sql = "CREATE TABLE IF NOT EXISTS " + TBL_TABLE1 + "(" +
COL_TABLE_COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_TABLE_COL2 + " TEXT NOT NULL," +
COL_TABLE_COL3 + " TEXT NOT NULL" +
")";
private static final String crt_table2_sql = "CREATE TABLE IF NOT EXISTS " + TBL_TABLE2 + "(" +
COL_TABLE_COL1 + " INTEGER PRIMARY KEY," +
COL_TABLE_COL2 + " TEXT NOT NULL," +
COL_TABLE_COL3 + " TEXT NOT NULL" +
")";
SQLiteDatabase mDB;
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.disableWriteAheadLogging();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(crt_table1_sql);
db.execSQL(crt_table2_sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public void insert(String col2, String col3) {
ContentValues cv = new ContentValues();
cv.put(COL_TABLE_COL2,col2);
cv.put(COL_TABLE_COL3,col3);
mDB.beginTransaction();
mDB.insert(TBL_TABLE1,null,cv);
mDB.insert(TBL_TABLE2,null,cv);
mDB.setTransactionSuccessful();
mDB.endTransaction();
}
public void deleteByValues(String col2, String col3) {
String whereclause = COL_TABLE_COL2 + "=? AND " + COL_TABLE_COL3 + "=?";
String[] args = new String[]{col2,col3};
mDB.beginTransaction();
mDB.delete(TBL_TABLE1,whereclause,args);
mDB.delete(TBL_TABLE2,whereclause,args);
rationaliseCol1Values();
mDB.setTransactionSuccessful();
mDB.endTransaction();
}
private void rationaliseCol1Values() {
ContentValues cv = new ContentValues();
Cursor csr = mDB.query(TBL_TABLE1,null,null,null,null,null,COL_TABLE_COL1 + " ASC");
int rowcount = csr.getCount();
long expected_id = 1;
long current_id;
String where_clause = COL_TABLE_COL1 + "=?";
String[] args = new String[1];
while (csr.moveToNext()) {
current_id = csr.getLong(csr.getColumnIndex(COL_TABLE_COL1));
if (current_id != expected_id) {
cv.clear();
cv.put(COL_TABLE_COL1,expected_id);
args[0] = String.valueOf(current_id);
mDB.update(TBL_TABLE1,cv,where_clause,args);
mDB.update(TBL_TABLE2,cv,where_clause,args);
}
expected_id++;
}
// Now adjust sqlite_sequence
where_clause = "name=?";
args[0] = TBL_TABLE1;
cv.clear();
cv.put("seq",String.valueOf(rowcount));
mDB.update("sqlite_sequence",cv,where_clause,args);
}
public void logTableRows(int stage) {
String tablenamne_column = "tablename";
Cursor[] csr = new Cursor[] {
mDB.query(TBL_TABLE1,new String[]{"'table1' AS " + tablenamne_column + ",*"},null,null,null,null,null),
mDB.query(TBL_TABLE2,new String[]{"'table2' AS " + tablenamne_column + ",*"}, null,null,null,null,null)
};
MergeCursor csr3 = new MergeCursor(csr);
StringBuilder sb = new StringBuilder("Data in both tables consists of " + String.valueOf(csr3.getCount()) + " rows :-");
while (csr3.moveToNext()) {
sb.append(
"\n\tTableName = " + csr3.getString(csr3.getColumnIndex(tablenamne_column)
)
).append(" " + COL_TABLE_COL1 + " value is " + csr3.getString(csr3.getColumnIndex(COL_TABLE_COL1))
).append(" " + COL_TABLE_COL2 + " value is " + csr3.getString(csr3.getColumnIndex(COL_TABLE_COL2))
).append(" " + COL_TABLE_COL3 + " value is" + csr3.getString(csr3.getColumnIndex(COL_TABLE_COL3))
);
}
Log.d("DATA4STAGE" + String.valueOf(stage),sb.toString());
}
}
用于测试上述内容的调用活动是:-
public class MainActivity extends AppCompatActivity {
DBHelper mDBHlpr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHlpr = new DBHelper(this);
manipulateSomeData();
}
private void manipulateSomeData() {
mDBHlpr.logTableRows(0);
mDBHlpr.insert("TEST001","TESTING001");
mDBHlpr.insert("TEST002","TESTING001");
mDBHlpr.insert("TEST003","TESTING001");
mDBHlpr.insert("TEST004","TESTING001");
mDBHlpr.insert("TEST005","TESTING001");
mDBHlpr.insert("TEST006","TESTING001");
mDBHlpr.insert("TEST007","TESTING001");
mDBHlpr.insert("TEST008","TESTING001");
mDBHlpr.insert("TEST009","TESTING001");
mDBHlpr.logTableRows(1);
mDBHlpr.deleteByValues("TEST005","TESTING001");
mDBHlpr.logTableRows(2);
mDBHlpr.deleteByValues("TEST008","TESTING001");
mDBHlpr.logTableRows(3);
mDBHlpr.deleteByValues("TEST003","TESTIN001");
mDBHlpr.logTableRows(4);
}
}
输出到日志的结果是:-
2018-12-31 12:43:21.618 2269-2269/so53976714.so53976714 D/DATA4STAGE0: Data in both tables consists of 0 rows :-
2018-12-31 12:43:21.657 2269-2269/so53976714.so53976714 D/DATA4STAGE1: Data in both tables consists of 18 rows :-
TableName = table1 col1 value is 1 col2 value is TEST001 col3 value isTESTING001
TableName = table1 col1 value is 2 col2 value is TEST002 col3 value isTESTING001
TableName = table1 col1 value is 3 col2 value is TEST003 col3 value isTESTING001
TableName = table1 col1 value is 4 col2 value is TEST004 col3 value isTESTING001
TableName = table1 col1 value is 5 col2 value is TEST005 col3 value isTESTING001
TableName = table1 col1 value is 6 col2 value is TEST006 col3 value isTESTING001
TableName = table1 col1 value is 7 col2 value is TEST007 col3 value isTESTING001
TableName = table1 col1 value is 8 col2 value is TEST008 col3 value isTESTING001
TableName = table1 col1 value is 9 col2 value is TEST009 col3 value isTESTING001
TableName = table2 col1 value is 1 col2 value is TEST001 col3 value isTESTING001
TableName = table2 col1 value is 2 col2 value is TEST002 col3 value isTESTING001
TableName = table2 col1 value is 3 col2 value is TEST003 col3 value isTESTING001
TableName = table2 col1 value is 4 col2 value is TEST004 col3 value isTESTING001
TableName = table2 col1 value is 5 col2 value is TEST005 col3 value isTESTING001
TableName = table2 col1 value is 6 col2 value is TEST006 col3 value isTESTING001
TableName = table2 col1 value is 7 col2 value is TEST007 col3 value isTESTING001
TableName = table2 col1 value is 8 col2 value is TEST008 col3 value isTESTING001
TableName = table2 col1 value is 9 col2 value is TEST009 col3 value isTESTING001
2018-12-31 12:43:21.666 2269-2269/so53976714.so53976714 D/DATA4STAGE2: Data in both tables consists of 16 rows :-
TableName = table1 col1 value is 1 col2 value is TEST001 col3 value isTESTING001
TableName = table1 col1 value is 2 col2 value is TEST002 col3 value isTESTING001
TableName = table1 col1 value is 3 col2 value is TEST003 col3 value isTESTING001
TableName = table1 col1 value is 4 col2 value is TEST004 col3 value isTESTING001
TableName = table1 col1 value is 5 col2 value is TEST006 col3 value isTESTING001
TableName = table1 col1 value is 6 col2 value is TEST007 col3 value isTESTING001
TableName = table1 col1 value is 7 col2 value is TEST008 col3 value isTESTING001
TableName = table1 col1 value is 8 col2 value is TEST009 col3 value isTESTING001
TableName = table2 col1 value is 1 col2 value is TEST001 col3 value isTESTING001
TableName = table2 col1 value is 2 col2 value is TEST002 col3 value isTESTING001
TableName = table2 col1 value is 3 col2 value is TEST003 col3 value isTESTING001
TableName = table2 col1 value is 4 col2 value is TEST004 col3 value isTESTING001
TableName = table2 col1 value is 5 col2 value is TEST006 col3 value isTESTING001
TableName = table2 col1 value is 6 col2 value is TEST007 col3 value isTESTING001
TableName = table2 col1 value is 7 col2 value is TEST008 col3 value isTESTING001
TableName = table2 col1 value is 8 col2 value is TEST009 col3 value isTESTING001
2018-12-31 12:43:21.675 2269-2269/so53976714.so53976714 D/DATA4STAGE3: Data in both tables consists of 14 rows :-
TableName = table1 col1 value is 1 col2 value is TEST001 col3 value isTESTING001
TableName = table1 col1 value is 2 col2 value is TEST002 col3 value isTESTING001
TableName = table1 col1 value is 3 col2 value is TEST003 col3 value isTESTING001
TableName = table1 col1 value is 4 col2 value is TEST004 col3 value isTESTING001
TableName = table1 col1 value is 5 col2 value is TEST006 col3 value isTESTING001
TableName = table1 col1 value is 6 col2 value is TEST007 col3 value isTESTING001
TableName = table1 col1 value is 7 col2 value is TEST009 col3 value isTESTING001
TableName = table2 col1 value is 1 col2 value is TEST001 col3 value isTESTING001
TableName = table2 col1 value is 2 col2 value is TEST002 col3 value isTESTING001
TableName = table2 col1 value is 3 col2 value is TEST003 col3 value isTESTING001
TableName = table2 col1 value is 4 col2 value is TEST004 col3 value isTESTING001
TableName = table2 col1 value is 5 col2 value is TEST006 col3 value isTESTING001
TableName = table2 col1 value is 6 col2 value is TEST007 col3 value isTESTING001
TableName = table2 col1 value is 7 col2 value is TEST009 col3 value isTESTING001
2018-12-31 12:43:21.681 2269-2269/so53976714.so53976714 D/DATA4STAGE4: Data in both tables consists of 14 rows :-
TableName = table1 col1 value is 1 col2 value is TEST001 col3 value isTESTING001
TableName = table1 col1 value is 2 col2 value is TEST002 col3 value isTESTING001
TableName = table1 col1 value is 3 col2 value is TEST003 col3 value isTESTING001
TableName = table1 col1 value is 4 col2 value is TEST004 col3 value isTESTING001
TableName = table1 col1 value is 5 col2 value is TEST006 col3 value isTESTING001
TableName = table1 col1 value is 6 col2 value is TEST007 col3 value isTESTING001
TableName = table1 col1 value is 7 col2 value is TEST009 col3 value isTESTING001
TableName = table2 col1 value is 1 col2 value is TEST001 col3 value isTESTING001
TableName = table2 col1 value is 2 col2 value is TEST002 col3 value isTESTING001
TableName = table2 col1 value is 3 col2 value is TEST003 col3 value isTESTING001
TableName = table2 col1 value is 4 col2 value is TEST004 col3 value isTESTING001
TableName = table2 col1 value is 5 col2 value is TEST006 col3 value isTESTING001
TableName = table2 col1 value is 6 col2 value is TEST007 col3 value isTESTING001
TableName = table2 col1 value is 7 col2 value is TEST009 col3 value isTESTING001