更新SQLite数据库中项的ID

时间:2012-11-07 14:51:32

标签: android sqlite auto-increment

我仍无法解决数据库中的更新问题。

我的应用程序在两个活动中定义了两个列表。 清单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

当我删除I​​D为72的产品时,它会在列表B中的列表A(nomer 0)项目中保持第一,ID为89也会被删除。 id = 90的项目得到nomer = 0,因为它的父项占据列表A中的第一个位置。但是id = 91的项目保持nomer = 2,但它应该是nomer = 1。因此,自动增量仅适用于删除后的下一个项目,其他项目保持不变。

2 个答案:

答案 0 :(得分:0)

在此代码中:

db.update(..., DBHelper.COLUMN_NOMER + "=?", pat);

参数是一个字符串,但列中的值是数字,因此它们永远不会相等 (强制所有参数都是字符串是Android SQLite API的设计错误。)

要确保参数的类型正确,请使用以下内容:

db.update(..., DBHelper.COLUMN_NOMER + "= CAST(? AS INTEGER)", pat);

答案 1 :(得分:0)

我认为解决方案介于sqlite lingvo的优秀知识和编程逻辑之间。这就是:

`           for (int i = 0; i <= cursor.getCount(); i++) {

                ContentValues cv = new ContentValues();

                cv.put(DBHelper.COLUMN_NOMER, nomer + i);

                int updCount = db.update(DBHelper.TABLE_ITEMS, cv,
                        DBHelper.COLUMN_NOMER + "=?",
                        new String[] { String.valueOf((nomer + i) + 1) });

                Log.d(LOG_TAG, "updated rows count = " + updCount);
            }
`

因此,我们将((nomer + i)+ 1)的所有项的值更改为(nomer + i)。