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

Java 更新SQLite数据库中的行会创建重复的行,java,android,database,sqlite,Java,Android,Database,Sqlite,我的Android应用程序中有一个SQLite数据库,有4个默认条目。当我尝试更新数据库中的一行时,它会正确地更新它,但它也会创建原始行的副本,并给它一个新ID 这个数据库只有一个列,加上它的ID列,但我有一个不同的数据库,它有多个列,并且更新可以很好地处理这些列/没有重复项,但不处理这个列 最初的4个默认值是工作、健康、家庭和精神,ID分别为1、2、3和4。更新后,它是Work2.0,健康、家庭、精神和工作,ID为1、2、3、4和5 以下是我在ContentProvider中的更新方法: @O

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

这个数据库只有一个列,加上它的ID列,但我有一个不同的数据库,它有多个列,并且更新可以很好地处理这些列/没有重复项,但不处理这个列

最初的4个默认值是工作、健康、家庭和精神,ID分别为1、2、3和4。更新后,它是Work2.0,健康、家庭、精神和工作,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);
编辑:

@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;
}
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));
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);
    }
}

下面是一个示例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;
}
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));
调用
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;
}
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));

希望这会有帮助~

您是如何创建数据库的?请确保包括所有步骤,这可能是行自动id问题,在我看到如何创建表之前还不确定。感谢dbHelper类及其执行的SQL。正在检查,试图使其在环境中工作。顺便说一句,我已经看到一些你可能想要使用它。整数主键自动递增,是否可以尝试删除唯一的?我认为主要的和独特的是造成这个问题。让我知道它是否有效。当然,将autoincrementRemoving设置为唯一,而不使用AutoIncrementWorks。我并没有故意使用AUTOINCREMENT,因为ID列来自BaseColumns,它有自己的功能。现在唯一修饰符消失了,有没有办法检查以确保不添加重复的值?谢谢,它可以工作,但我希望能够通过传入URI和值来更新单行。从名称列中删除唯一约束修复了它。您可以按如下方式从更新方法生成SelectionAgs:int count=db.update(tableName,values,selection,new String[]{String.valueOf(uri.getLastPathSegment())});谢谢你接受我的回答。如果我的回答似乎有用,那么你可以投赞成票。提前谢谢