我有SQLite的问题和一些定制的SQL:
public Cursor getExistingSpeciesDetails(String sub_id, String english_name) {
Cursor c = mDb.rawQuery(
"SELECT mobileobs.how_many, mobileobs.comment, mobileobs.sensitive, "
+ "mobileobs.breeding_evidence from mobileobs "
+ "WHERE mobileobs.sub_id = '" + sub_id
+ "' and mobileobs.english_name = '" + english_name
+ "'", null);
return c;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是英文名称中的撇号(Cetti's Warbler)导致SQL中的错误(强制关闭我的Android应用程序).在这种类型的条款中有没有办法逃避撇号?
提前致谢
为此,您需要使用SQLIte的参数注入.例如:
Cursor c = mDb.rawQuery(
"SELECT mobileobs.how_many, mobileobs.comment, mobileobs.sensitive, "
+ "mobileobs.breeding_evidence from mobileobs "
+ "WHERE mobileobs.sub_id = ? AND mobileobs.english_name = ?"
, new String[]{sub_id, english_name});
Run Code Online (Sandbox Code Playgroud)
或者更改输入参数如下:
Cetti''s Warbler
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2127 次 |
| 最近记录: |