Android SQLite转义Where子句

时间:2014-02-18 22:01:12

标签: android sql sqlite android-sqlite

Android中的SQLite需要转义哪些子句?

ContentValues vals= new ContentValues();
vals.put(KEY, val);

String where = DatabaseUtils.sqlEscapeString(val);

db.update(TABLE_NAME, vals, KEY + "='" + where + "'", null);

出于某种原因,如果我db.update(TABLE_NAME, vals, KEY + "=?", new String[] {where});它不起作用。

编辑:我的问题是我是否需要使用where转义DatabaseUtils.sqlEscapeStringupdate方法(或任何其他SQLite方法)为我逃避。

3 个答案:

答案 0 :(得分:0)

update() method期望第三个参数槽中有一个SQL WHERE子句,而不是转义参数值。它期望一组值插入WHERE子句,而不是单词“where”。

以下是方法签名:

public int update (
    String table, ContentValues values, String whereClause, String[] whereArgs)

答案 1 :(得分:0)

您应该使用DatabaseUtils.sqlEscapeString(stringToEscape)

http://developer.android.com/reference/android/database/DatabaseUtils.html

答案 2 :(得分:0)

在sqlite更新方法中假设的位置。

ContentValues vals= new ContentValues();
vals.put(KEY, val);
db.update(TABLE_NAME, vals, KEY + " = ?",new String[] {String.valueOf(DatabaseUtils.sqlEscapeString(val)) });