3

我正在重构我的代码并将所有insert语句替换为insert or replacewhile 我发现使用以下方法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()会这样做,但我找不到一个很好的例子。

4

1 回答 1

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);
}
于 2012-08-01T11:35:12.273 回答