我遇到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;
}
我的问题是英文名称中的撇号(Cetti's Warbler)导致SQL出错(强制关闭我的Android应用程序)。在这种类型的条款中有没有办法逃避撇号?
提前致谢
答案 0 :(得分:8)
为此,您需要使用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});
或者更改输入参数如下:
Cetti''s Warbler