更新SQLite数据库中的行会创建重复的行

时间:2017-05-05 17:06:36

标签: java android database sqlite

我的Android应用程序中有一个SQLite数据库,有4个默认条目。当我尝试更新数据库中的一行时,它会正确更新它,但它也会创建原始行的副本并为其提供一个新ID。

这个数据库只有一个列,加上它的ID列,但是我有一个具有多个列的不同数据库,并且更新适用于/不重复,但不适用于此。

原始的4默认值分别是工作,健康,家庭和精神,ID分别为1,2,3和4。更新后,它是Work 2.0,Health,Family,Spiritual和Work,ID为1,2,3,4和5.

以下是我的ContentProvider中的更新方法:

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    String tableName;
    int uriNum = uriMatcher.match(uri);

    switch (uriNum) {
        //Case 1 is for the entire Goals table
        case 1:
            break;

        //Case 2 is for a specific row in the Goals table
        case 2:
            if (selection != null) {
                selection = selection + "_ID = " + uri.getLastPathSegment();
            }
            else {
                selection = "_ID = " + uri.getLastPathSegment();
            }
            break;

        //Case 3 is for the entire Categories table
        case 3:
            break;

        //Case 4 is for a specific row in the Categories table
        case 4:
            if (selection != null) {
                selection = selection + "_ID = " + uri.getLastPathSegment();
            }
            else {
                selection = "_ID = " + uri.getLastPathSegment();
            }
            break;

        default:
            throw new IllegalArgumentException("URI not found.");
    }

    if(uriNum == 1 || uriNum == 2) {
        tableName = GoalsContract.Goals.TABLE_NAME;
    }
    else {
        tableName = CategoriesContract.Categories.TABLE_NAME;
    }

    db = dbHelper.getWritableDatabase();
    dbHelper.onCreate(db);

    Log.d(TAG,
            tableName + "\n" +
            selection + "\n"
    );

    int count = db.update(tableName, values, selection, selectionArgs);

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

调用代码:

    ContentValues updatedValues = new ContentValues();
    updatedValues.put(CategoriesContract.Categories.COLUMN_NAME_NAME, "Work 2.0");

    Uri uri = ContentUris.withAppendedId(CategoriesContract.Categories.CONTENT_URI, 1);

    int count = getContentResolver().update(uri, updatedValues, null, null);

修改

dbHelper类:

public void onCreate(SQLiteDatabase db) {
    db.execSQL(GoalsContract.Goals.SQL_CREATE_GOALS);
    db.execSQL(CategoriesContract.Categories.SQL_CREATE_CATEGORIES);

    //TODO Do on a different thread
    addDefaultData(db);
}

private void addDefaultData(SQLiteDatabase db) {
    String[] defaultCategories = {"Work", "Health", "Family", "Spiritual"};

    for(int x = 0; x < defaultCategories.length; x++) {
        ContentValues values = new ContentValues();
        values.put(CategoriesContract.Categories.COLUMN_NAME_NAME, defaultCategories[x]);
        db.insert(CategoriesContract.Categories.TABLE_NAME, null, values);
    }
}

创建表的实际SQL:

    static final String SQL_CREATE_CATEGORIES =
     "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
     _ID + " INTEGER PRIMARY KEY, " +
     COLUMN_NAME_NAME + " TEXT NOT NULL UNIQUE);" ;

1 个答案:

答案 0 :(得分:1)

以下是SQLiteDatabase update方法示例:

db.update(TABLE_NAME, ContentValues, KEY_COLUMN_NAME + " = ? ", new String[]{String.valueOf(COLUMN_VALUE)})

修改您的update()方法,如下所示:

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

    String tableName;
    int uriNum = uriMatcher.match(uri);

    switch (uriNum) {
        //Case 1 is for the entire Goals table
        case 1:
            break;

        //Case 2 is for a specific row in the Goals table
        case 2:
            if (selection != null) {
                selection = selection + "_ID = ? ";
            }
            else {
                selection = "_ID = ? ";
            }
            break;

        //Case 3 is for the entire Categories table
        case 3:
            break;

        //Case 4 is for a specific row in the Categories table
        case 4:
            if (selection != null) {
                selection = selection + "_ID = ? ";
            }
            else {
                selection = "_ID = ? ";
            }
            break;

        default:
            throw new IllegalArgumentException("URI not found.");
    }

    if(uriNum == 1 || uriNum == 2) {
        tableName = GoalsContract.Goals.TABLE_NAME;
    }
    else {
        tableName = CategoriesContract.Categories.TABLE_NAME;
    }

    db = dbHelper.getWritableDatabase();
    dbHelper.onCreate(db);

    Log.d(TAG, tableName + "\n" + selection + "\n");

    int count = db.update(tableName, values, selection, selectionArgs);

    getContext().getContentResolver().notifyChange(uri, null);

    return count;
}

按以下方式调用update()方法:

ContentValues updatedValues = new ContentValues();
updatedValues.put(CategoriesContract.Categories.COLUMN_NAME_NAME, "Work 2.0");

Uri uri = ContentUris.withAppendedId(CategoriesContract.Categories.CONTENT_URI, 1);

int count = getContentResolver().update(uri, updatedValues, null, new String[]{String.valueOf(1));

希望这会有所帮助〜