[英]Escape special character # in table condition clause
在SQLite中使用包含特殊字符#
列名創建表。
不知道如何將其轉義,以便SQLite能夠識別它。
我發現#
的轉義代碼是%23
。 所以我嘗試這樣做:
condition = condition.replace("usernum#", "usernum%23");
其中condition是字符串類型的SQLite條件,例如, select * from users where id = '2' ORDER BY usernum#
但是上述方法是給SQLite異常。
日志:
04-12 20:00:44.395: E/AndroidRuntime(8054): android.database.sqlite.SQLiteException: no such column: usernum (code 1): , while compiling: select * from Users WHERE id !=1 order by Type DESC, usernum%23 ASC
錯誤在這一行:
Cursor curr = db.rawQuery("select " + fields + " from " + table + " "+ condition, null);
04-12 20:00:44.395:E / AndroidRuntime(8054):android.database.sqlite.SQLiteException:否這樣的列:usernum(code 1):,編譯時:選擇* from Users WHERE id!= 1按類型排序DESC,usernum%23 ASC
由於列包含特殊字符,解決方案是將其包裝在雙引號(整個列)中,它將起作用
select * from test order by "username#" desc;
在您的情況下:
String query = "Select * from users where id = ? ORDER BY \"usernum#\"";
Cursor c = db.rawQuery(query, new String[] {String.valueOf(id)});
使用query()方法的解決方案:
String[] columns = {...};
String selection = idColumn + " = ?";
String[] args = {String.valueOf(id)};
String orderBy = "\"" + orderByColumn + "\"";
Cursor c = db.query(table, columns, selection, args, null, null, orderBy);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.