Android SQLite 中的 rawQuery() 未编译参数
rawQuery() in Android SQLite not compiling parameters
同时尝试将 SQLite 存储 运行 实现为 st运行ge 行为。
“?”符号未被替换。
我的代码:
public class DBHandler extends SQLiteOpenHelper {
public void writeTask(JSONObject object) throws JSONException {
SQLiteDatabase db = this.getWritableDatabase();
String id = object.get(OBJECT_ID).toString();
String content = object.toString();
String md5 = "md5"; //testing
Cursor c = db.rawQuery("INSERT OR REPLACE INTO ? ( ? , ? , ? ) VALUES ( ? , ? , ?);", new String[] {TABLE_OBJECTS, OBJECT_ID, OBJECT_CONTENT, OBJECT_MD5, id, content, md5 });
}
}
然后它抛出一个 st运行ge 错误:
android.database.sqlite.SQLiteException: near "?": syntax error (code 1): , while compiling: INSERT OR REPLACE INTO ? ( ? , ? , ? ) VALUES ( ? , ? , ?);
第一个错误已更正,但仍然无法正常工作:
String selectQuery = "INSERT OR REPLACE INTO " + TABLE_OBJECTS + " ("
+ OBJECT_ID + "," + OBJECT_CONTENT + "," + OBJECT_MD5 + ") "
+ "VALUES ( ? , ? , ?);";
String[] args = { id, content, md5 };
Log.d("FP", selectQuery);
Cursor c = db.rawQuery(selectQuery,args);
现在查询后数据库没有被触及。日志显示我的查询:
INSERT OR REPLACE INTO objects (id,content,md5) VALUES (?,?,?);
有什么建议吗?
所以,rawQuery() 仅适用于 SELECT.
但我仍然需要转义特殊字符,因为 content-变量是字符串化的 JSON 而 execSQL 不允许这样做。
您只能将 ?
用于绑定文字,例如 VALUES()
中的那些,不能用于 table 等标识符或 SQL 中较早的列名。
如果您需要使用变量作为标识符,请在 Java 中使用常规字符串连接。
另请注意,单独 rawQuery()
不会执行您的 SQL。考虑改用 execSQL()
。
同时尝试将 SQLite 存储 运行 实现为 st运行ge 行为。 “?”符号未被替换。
我的代码:
public class DBHandler extends SQLiteOpenHelper {
public void writeTask(JSONObject object) throws JSONException {
SQLiteDatabase db = this.getWritableDatabase();
String id = object.get(OBJECT_ID).toString();
String content = object.toString();
String md5 = "md5"; //testing
Cursor c = db.rawQuery("INSERT OR REPLACE INTO ? ( ? , ? , ? ) VALUES ( ? , ? , ?);", new String[] {TABLE_OBJECTS, OBJECT_ID, OBJECT_CONTENT, OBJECT_MD5, id, content, md5 });
}
}
然后它抛出一个 st运行ge 错误:
android.database.sqlite.SQLiteException: near "?": syntax error (code 1): , while compiling: INSERT OR REPLACE INTO ? ( ? , ? , ? ) VALUES ( ? , ? , ?);
第一个错误已更正,但仍然无法正常工作:
String selectQuery = "INSERT OR REPLACE INTO " + TABLE_OBJECTS + " ("
+ OBJECT_ID + "," + OBJECT_CONTENT + "," + OBJECT_MD5 + ") "
+ "VALUES ( ? , ? , ?);";
String[] args = { id, content, md5 };
Log.d("FP", selectQuery);
Cursor c = db.rawQuery(selectQuery,args);
现在查询后数据库没有被触及。日志显示我的查询:
INSERT OR REPLACE INTO objects (id,content,md5) VALUES (?,?,?);
有什么建议吗?
所以,rawQuery() 仅适用于 SELECT.
但我仍然需要转义特殊字符,因为 content-变量是字符串化的 JSON 而 execSQL 不允许这样做。
您只能将 ?
用于绑定文字,例如 VALUES()
中的那些,不能用于 table 等标识符或 SQL 中较早的列名。
如果您需要使用变量作为标识符,请在 Java 中使用常规字符串连接。
另请注意,单独 rawQuery()
不会执行您的 SQL。考虑改用 execSQL()
。