Android 使用 ContentValues 和更新方法更新 sql 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3760774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
update sql database with ContentValues and the update-method
提问by spierala
I would like to update my SQL lite database with the native update-method of the SQLiteDatabase class of android.
我想用 android 的 SQLiteDatabase 类的本机更新方法更新我的 SQL lite 数据库。
ContentValues dataToInsert = new ContentValues();
dataToInsert.put("name", "flo");
dataToInsert.put("location", "flotown");
String where = "id" + "=" + id;
try{
db.update(DATABASE_TABLE, dataToInsert, where, null);
}
catch (Exception e){
String error = e.getMessage().toString();
}
but I get following error:
但我收到以下错误:
android.database.sqlite.SQLiteException: near "15": syntax error: , while compiling: UPDATE mytable SET location=?, name=? WHERE id=2010-09-21 15:05:36.995
android.database.sqlite.SQLiteException:“15”附近:语法错误:,编译时:UPDATE mytable SET location=?, name=? WHERE id=2010-09-21 15:05:36.995
I don′t know what should be the problem. Somehow the values do not arrive in the SQL statement. I did nearly the same with the insert method and that worked quite fine.
我不知道应该是什么问题。不知何故,这些值不会到达 SQL 语句中。我对插入方法做了几乎相同的事情,而且效果很好。
回答by TheContstruct
You're using the update function wrong. It should be like this:
您使用的更新功能错误。应该是这样的:
String where = "id=?";
String[] whereArgs = new String[] {String.valueOf(id)};
db.update(DATABASE_TABLE, dataToInsert, where, whereArgs);
The Strings in the whereArgs array gets substituted in for each '?' in the where variable.
whereArgs 数组中的字符串被替换为每个 '?' 在 where 变量中。
ie. if you had where = "name=? AND type=? then the first '?' would get replaced by whereArgs[0] and the second by whereArgs[1].
IE。如果你有 where = "name=? AND type=? 那么第一个 '?' 将被 whereArgs[0] 替换,第二个被 whereArgs[1] 替换。
回答by Adam Javin
Actually, you just need to add apostrophes to your where clause. So it ought to be:
实际上,您只需要在 where 子句中添加撇号即可。所以它应该是:
String where = "id='" + id + "'"
(note: however, this is not best practice, as it theoretically leaves open to injection attacks)
(注意:然而,这不是最佳实践,因为它理论上会导致注入攻击)
回答by Anuroop Pendela
Actually what exactly you written is correct. The syntax is correct. But you have to check these. String where = "id" + "=" + id; In the above declaration "id" should be type number and id should be int. And if id is a type of TEXT then follow @Adam javin answer.
其实你写的都是对的。语法是正确的。但是你必须检查这些。字符串 where = "id" + "=" + id; 在上面的声明中,“id”应该是数字类型,id 应该是 int。如果 id 是一种 TEXT 类型,则按照@Adam javin 的回答。
回答by Sam
I have an other approach
我有另一种方法
public boolean updateEmployee(TalebeDataUser fav) {
SQLiteDatabase database = dbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelper.COLUMN_ID, fav.getId());
contentValues.put(DBHelper.COLUM_AD, fav.getAd());
contentValues.put(DBHelper.COLUMN_NUMARA, fav.getNumara());
contentValues.put(DBHelper.COLUMN_YURD_ID, fav.getYurtID());
contentValues.put(DBHelper.COLUMN_EGITIM_ID, fav.getEgitimTur());
contentValues.put(DBHelper.COLUMN_TEL, fav.getTel());
contentValues.put(DBHelper.COLUMN_EMAIL, fav.getEmail());
contentValues.put(DBHelper.COLUMN_ADDRESS, fav.getAdres());
String whereClause = DBHelper.COLUM_AD + " = ? AND " + DBHelper.COLUMN_NUMARA + " = ? ";
final String whereArgs[] = {fav.getAd(), String.valueOf(fav.getNumara())};// old nameler taran?yor
int affectedRows = database.update(DBHelper.TABLE_NAME_OGR, contentValues, whereClause, whereArgs);
return affectedRows > 0;
}