我仍然无法解决数据库更新的问题。
我的应用程序在两个活动中定义了两个列表。列表 A 有职位和列表 B 项目。两个列表中的数据都保存到数据库并从数据库上传。从代码中可以看出,我在删除时没有任何问题。
当我删除列表 A 中的一个位置时,列表 B 中属于该位置的一组项目也将被删除。
更新有一个问题:删除后的项目集应该自动递增,但它仅适用于已删除项目之后的第一行,其他项目保持不变。
这是我的代码:
public void deleteItemsFromProduct(long nomer) {
cursor = DataSource.queryAllinItems();
if (cursor != null) {
String [] pat = new String[] { String.valueOf(nomer) };
db.delete(DBHelper.TABLE_ITEMS, DBHelper.COLUMN_NOMER + "=?", pat);
for (int i=0; i<cursor.getCount(); i++){
ContentValues cv = new ContentValues();
cv.put(DBHelper.COLUMN_NOMER, nomer);
pat = new String[] { String.valueOf(nomer+1) };
int updCount = db.update(DBHelper.TABLE_ITEMS, cv, DBHelper.COLUMN_NOMER + "=?", pat);
Log.d(LOG_TAG, "updated rows count = " + updCount);
}
if (cursor.moveToFirst()) {
Log.d(LOG_TAG, "--- Left in the table for items and prices: ---");
idColIndex = cursor.getColumnIndex(DBHelper.COLUMN_ITEM_ID);
nameColIndex = cursor.getColumnIndex(DBHelper.COLUMN_ITEM);
priceColIndex = cursor.getColumnIndex(DBHelper.COLUMN_PRICE);
nomerColIndex = cursor.getColumnIndex(DBHelper.COLUMN_NOMER);
do {
Log.d(LOG_TAG, "item id = "
+ cursor.getInt(idColIndex)
+ ", item name = "
+ cursor.getString(nameColIndex)
+ ", item price = "
+ cursor.getInt(priceColIndex)
+ ", product nomer = "
+ cursor.getInt(nomerColIndex));
} while (cursor.moveToNext());
} else {
Log.d(LOG_TAG, "Cursor is null");
cursor.close();
}
}
在这里记录
11-07 14:37:49.862: D/(1813): item id = 89, item name = б, item price = 0, product nomer = 0
11-07 14:37:49.862: D/(1813): item id = 90, item name = т, item price = 1, product nomer = 1
11-07 14:37:49.922: D/(1813): item id = 91, item name = м, item price = 2, product nomer = 2
11-07 14:37:58.442: D/(1813): MainActivity: onResume()
11-07 14:38:05.382: D/(1813): --- Delete from table for products: ---
11-07 14:38:05.465: D/(1813): deleted row id = 72, name = х, image = /mnt/sdcard/download/sliced-bread-icon.png
11-07 14:38:05.712: D/(1813): updated rows count = 1
11-07 14:38:05.752: D/(1813): updated rows count = 0
11-07 14:38:05.752: D/(1813): --- Left in the table for items and prices: ---
11-07 14:38:05.804: D/(1813): item id = 90, item name = т, item price = 1, product nomer = 1
11-07 14:38:05.822: D/(1813): item id = 91, item name = м, item price = 2, product nomer = 2
11-07 14:38:10.416: D/(1813): MainActivity: onPause()
11-07 14:38:10.622: D/(1813): ChildActivity: onCreate()
11-07 14:38:10.723: D/(1813): item id = 90, item name = т, item price = 1, product nomer = 0
当我删除 id 为 72 的产品时,它在列表 B 中的列表 A(名义 0)中的第一个 id 为 89 的项目也被删除。id=90 的项目获得 nomer=0,因为它的父项在列表 A 中占据首位。但是 id=91 的项目保持 nomer=2,但它应该是 nomer=1。因此,自动增量仅适用于删除后的下一个项目,所有其他项目保持不变。