SQLite的选择查询与rawQuery在安卓SQLite、rawQuery

2023-09-06 02:27:50 作者:風流倜傥??

我知道这是一个基本的问题,但我不能找到问题。 我试图得到一些列与rawQuery。

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

我有5P的CAR表,我敢肯定,因为我用它其他的查询。

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

推荐答案

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

You either need to quote that value, or better yet, use positional parameters:

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

使用位置参数的好处是,SQLite的将处理引用的字符串,转义的所有嵌入式引号,等等。

The advantage of using positional parameters is that SQLite will handle quoting the string, escaping any embedded quotes, etc.