insertWithOnConflict()与插入或替换

时间:2012-08-01 10:16:18

标签: android sqlite

我正在重构我的代码并用insert替换所有insert or replace语句,而我发现以下方法使用db.insert()而不是预准备语句:

public static void insertIntoTableByNameAndFieldsAndValues(
        String tableName, String[] fields, String[] values, Context c)
        throws FieldsAndValuesMismatchException, UnrecognizedTypeException   {

    DatabaseAbstractionLayer dal = new DatabaseAbstractionLayer(c);
    SQLiteDatabase db = dal.getWritableDatabase();

    ContentValues con = new ContentValues();

    if (fields.length != values.length)
        throw new FieldsAndValuesMismatchException(
                "fieldsString and values are not the same size");

    int fieldCount = fields.length;

    for (int i = 0; i < fieldCount; i++) {
        String[] fieldArr = fields[i].split(":");
        String value = values[i];
        if (fieldArr[1].equalsIgnoreCase("long")) {
            // long
            if(value.equalsIgnoreCase("null")){
                con.putNull(fieldArr[0]);
            }else{
                con.put(fieldArr[0],(long) Integer.parseInt(value));
            }
        } else if (fieldArr[1].equalsIgnoreCase("string") || fieldArr[1].equalsIgnoreCase("text")) {
            // string

            if(value.equalsIgnoreCase("null")){
                con.putNull(fieldArr[0]);
            }else{
                con.put(fieldArr[0], value);
            }


        }else if(fieldArr[1].equalsIgnoreCase("double")){
            //double

            if(value.equalsIgnoreCase("null")){
                con.putNull(fieldArr[0]);
            }else{
                con.put(fieldArr[0], Double.valueOf(value));
            }

        } else {
            throw new UnrecognizedTypeException(fieldArr[1]
                    + " is not string,text, long or double");
        }
    }

    db.insert(tableName, null, con);

 db.close();
}

我不想重写代码并使用原始查询。 发生冲突时有没有办法替换行?我想insertWithOnConflict()会这样做,但我找不到一个好的例子。

1 个答案:

答案 0 :(得分:15)

是的,(如果您不遵循Android使用的行标识符的标准名称,请将BaseColumns._ID更改为适合的内容),这应该是您想要的:

db.insertWithOnConflict(tableName, BaseColumns._ID, v, 
        SQLiteDatabase.CONFLICT_REPLACE);

但是 - 那种方法肯定会做很多毫无意义的事情。简而言之,它可以归结为:

public static void insertIntoTableByNameAndFieldsAndValues(
    String tableName, String[] fields, String[] values, Context c)
    throws FieldsAndValuesMismatchException {

    if (fields.length != values.length) {
        throw new FieldsAndValuesMismatchException(
            "fields[] and values[] are not the same length");
    }
    ContentValues v = new ContentValues();
    // You really don't need to distinguish between integers, doubles
    // etc. etc. - SQLite is type agnostic, just dumping Strings from Java
    // will work just fine - the *only* point in converting stuff is to check
    // the format prior to inserts.
    // Using "null" as the null identifier is also pointless. Pass actual null
    // values instead - calling #putNull("somecolumn") is the same 
    // as #put("somecolumn", null)
    for (int i = 0; i < fields.length; i++) {
        v.put(fields[i], values[i]);
    }
    db.insertWithOnConflict(tableName, BaseColumns._ID, v, 
            SQLiteDatabase.CONFLICT_REPLACE);
}