在SQLite中创建包含特殊字符#
的列名的表。
不知道如何逃避这一点,以便SQLite识别它。
我发现#
的转义码是%23
。所以我试着这样做:
condition = condition.replace("usernum#", "usernum%23");
其中condition是string类型的SQLite条件,例如select * from users where id = '2' ORDER BY usernum#
但上面的方法是给出SQLite异常。
LOG:
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);
答案 0 :(得分:0)
04-12 20:00:44.395:E / AndroidRuntime(8054): android.database.sqlite.SQLiteException:没有这样的列:usernum(代码 1):,同时编译:select * from Users WHERE id!= 1 order by Type 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);