简体   繁体   English

表条件子句中的转义特殊字符#

[英]Escape special character # in table condition clause

Creating the table in SQLite with column names which contains special character # . 在SQLite中使用包含特殊字符#列名创建表。

Don't know how to escape this so that SQLite recognize it. 不知道如何将其转义,以便SQLite能够识别它。

I found that the escape code for # is %23 . 我发现#的转义代码是%23 So I tried to do like this: 所以我尝试这样做:

condition = condition.replace("usernum#", "usernum%23");

where condition is the SQLite condition of type string, for example, select * from users where id = '2' ORDER BY usernum# 其中condition是字符串类型的SQLite条件,例如, select * from users where id = '2' ORDER BY usernum#

But the above way is giving SQLite Exception. 但是上述方法是给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

Error is in this line: 错误在这一行:

Cursor curr = db.rawQuery("select " + fields + " from " + table + " "+ condition, null);

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 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

Since column contains special character, solution is to wrap it to double quotes(whole column) and it will work 由于列包含特殊字符,解决方案是将其包装在双引号(整个列)中,它将起作用

select * from test order by "username#" desc;

In your case: 在您的情况下:

String query = "Select * from users where id = ? ORDER BY \"usernum#\"";
Cursor c = db.rawQuery(query, new String[] {String.valueOf(id)});

Solution with query() method: 使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM