Reputation: 1492
I was refactoring my code and replacing all insert
statements with insert or replace
while i found the following method that uses db.insert()
instead of a prepared statement:
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();
}
I don't want to rewrite the code and use a raw query.
Is there a way to replace a row when a conflict occurs? I guess insertWithOnConflict()
would do that but I couldn't find a good example.
Upvotes: 3
Views: 15005
Reputation: 17067
Yup, (change BaseColumns._ID
to something suitable if you do not follow the standard name for the row identifier used by Android) and this should do what you want:
db.insertWithOnConflict(tableName, BaseColumns._ID, v,
SQLiteDatabase.CONFLICT_REPLACE);
But - that method sure does a lot of pointless things. In short, it could be boiled down to this:
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);
}
Upvotes: 15