11

I know it is a basic question, but i can not find problem. I try to get some columns with rawQuery.

    SQLiteDatabase db=database.getReadableDatabase();

    try 
    {
       String sql="SELECT * FROM CAR WHERE name = "; 
       Cursor crs = db.rawQuery(sql+"5P", null);
    }

I always get the same exception.

android.database.sqlite.SQLiteException: **unrecognized token**: "5P": , while compiling: SELECT * FROM CAR WHERE name = 5P

I have 5P at CAR table, i am sure because i used it other queries.

4

1 回答 1

20

您需要引用该值,或者更好的是,使用位置参数:

String[] args={"5P"};
Cursor crs=db.rawQuery("SELECT * FROM CAR WHERE name = ?", args);

使用位置参数的优点是 SQLite 将处理引用字符串、转义任何嵌入的引号等。

于 2013-04-21T22:03:28.957 回答