Reputation: 1879
I need some suggestions about how to update data in SQLite database. Now I am doing it in this way:
public void insertadoMedidasAdoptarCorrectamente(String codMedAdC,
String codigoServicio) {
AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelper
.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(AndroidOpenDbHelper.insertadoCorrectamente, 1);
sqliteDatabase.update("MedidasAdoptarSeleccionadas", contentValues,
"codigomedida = '" + codMedAdC + "' AND codigoservicio = '"
+ codigoServicio + "'", null);
sqliteDatabase.close();
}
I heard that this is not the best way to update data. I think that sometimes it faults. Because of that code:
sqliteDatabase.update("MedidasAdoptarSeleccionadas", contentValues,
"codigomedida = '" + codMedAdC + "' AND codigoservicio = '"
+ codigoServicio + "'", null);
sqliteDatabase.close();
You can observe that I am filtering items by two fields. I have seen that this could be done with whereclause arguments.
So, my question is, which is the best way to filter data that is gonna to update?
Thanks.
Upvotes: 0
Views: 231
Reputation: 63955
You should not do "codigomedida = '" + codMedAdC + "'
since that will break if there is a '
in codMedAdC
.
But doing it like
String[] whereArgs = { codMedAdC, codigoServicio };
sqliteDatabase.update("MedidasAdoptarSeleccionadas", contentValues,
"codigomedida = ? AND codigoservicio = ?", whereArgs);
is fine.
You could think about keeping the database open if you do those updates regularly. Creating new AndroidOpenDbHelper
and opening the database each time takes some time.
Upvotes: 1