您好如何进行SQLite
查询以更新数据库中的现有行,这是我的代码,但它不起作用:
public void update(int id, String name){
SQLiteDatabase db = getWritableDatabase();
String query = "UPDATE " + TABLE_NAME +
" SET " + COLUMN_TWO_ID + " = " + name +
" WHERE " + COLUMN_ONE_ID + " = " + id + ";";
db.execSQL(query);
db.close();
}
这就是logcat:
android.database.sqlite.SQLiteException: no such column: jdjxjcii (code 1): , while compiling: UPDATE Contact SET name = jdjxjcii WHERE id = 20;
更新
我解决了问题,它错过了引号,因为它是一个字符串:
public void update(int id, String name){
SQLiteDatabase db = getWritableDatabase();
String query = "UPDATE " + TABLE_NAME +
" SET " + COLUMN_TWO_ID + " = \"" + name + "\"" +
" WHERE id = " + id + ";"; alors on choisi la colonne
db.execSQL(query);
db.close();
}
答案 0 :(得分:2)
您看到的错误很可能是因为您的字符串参数被连接到查询非转义中,因此被解释为字符串文字(例如列)之外的其他内容。你可以在名称周围加上单引号,但更安全的做法是做一些学习并使用准备好的语句:
String query = "UPDATE " + TABLE_NAME +
" SET " + COLUMN_TWO_ID + " = ?"
" WHERE " + COLUMN_ONE_ID + " = ?";
try (Connection conn = this.connect();
PreparedStatement ps = conn.prepareStatement(query)) {
ps.setString(1, name);
ps.setInt(2, id);
ps.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
答案 1 :(得分:0)
最后不要使用半结肠“;”;使用
String query = "UPDATE " + TABLE_NAME +
" SET " + COLUMN_TWO_ID + " = " + name +
" WHERE " + COLUMN_ONE_ID + " = " + id + "";
答案 2 :(得分:0)
public void update(int id, String name){
SQLiteDatabase db = getWritableDatabase();
String query = "UPDATE " + TABLE_NAME +
" SET " + COLUMN_TWO_ID + " = " + name +
" WHERE " + COLUMN_ONE_ID + " = " + id;
Cursor cursor = db.rawQuery( , null);
if (cursor != null)
cursor.moveToFirst();
cursor.close();
db.execSQL(query);
db.close();
}
答案 3 :(得分:0)
更新行的简便方法之一是:
ContentValues cv=new ContentValues();
cv.put("Column","value");
db.update("TABLE",cv,"id = ?", new String[]{"" + id});
其中字符串数组中的id是您的行ID。
答案 4 :(得分:0)
使用SQLiteDatabase更新方法更新行,这样更容易。
public void update(int id, String name)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(columnName, name);
long updateRow = db.update(DBConstant.TABLE_NAME, cv, COLUMN_ONE_ID + "=?", new String[]{String.valueOf(id)});
return updateRow;
}
答案 5 :(得分:0)
看看:
public void updateUser(User user){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data=new ContentValues();
data.put(COLUMN_USERNAME, user.getUsername());
db.update(TABLE_USERS, data, KEY_USERID + "='" + user.getID()+"'" , null);
db.close();
}
确保表格中存在用户名栏。
答案 6 :(得分:0)
我解决了问题,它错过了引号,因为它是一个字符串:
public void update(int id, String name){
SQLiteDatabase db = getWritableDatabase();
String query = "UPDATE " + TABLE_NAME +
" SET " + COLUMN_TWO_ID + " = \"" + name + "\"" +
" WHERE id = " + id + ";"; alors on choisi la colonne
db.execSQL(query);
db.close();
}