android.database.sqlite.SQLiteException:near“”:语法错误(代码1):通过匹配两个不同的关键字从数据库读取时出错

时间:2021-01-04 23:02:27

I am trying to get/read data by matching an Integer ID and a specific String, But while compiling the code I am getting this error :

我试图通过匹配整数ID和特定的字符串来获取/读取数据,但在编译代码时我收到此错误:

> android.database.sqlite.SQLiteException: near "Computing": syntax
> error (code 1): , while compiling: SELECT * FROM midSemAnsBank WHERE
> id = 4 AND subject = Cloud Computing

Here's what I'm trying :

这是我正在尝试的:

    openReadable();
    String select_query= "SELECT * FROM " +MID_SEM_ANSWERS_TABLE + " WHERE " + KEY_ID + " = " + answerID +" AND " + KEY_SUBJECT + " = " + subject +" " ;
    Log.d(LOG, select_query);
    Cursor c= m_sqLiteDatabase.rawQuery(select_query,null);
    if(c!=null)
        c.moveToFirst();

what's the proper query to read data by matching two keywords?

通过匹配两个关键字来读取数据的正确查询是什么?

1 个解决方案

#1


1  

String should be between two '' so instead :

字符串应该在两个''之间,所以相反:

String select_query= "SELECT * FROM " +MID_SEM_ANSWERS_TABLE + " WHERE " + 
     KEY_ID + " = " + answerID +" AND " + KEY_SUBJECT + " = '" + subject + "'";
//----------------------------------------------------------^---------------^

Note

The important thing, your code still not secure, and can cause Syntax error or SQL Injection, i suggest to use PreparedStatement instead, read also more about How do I use prepared statements in SQlite in Android?.

重要的是,你的代码仍然不安全,并且可能导致语法错误或SQL注入,我建议使用PreparedStatement,另外阅读更多关于如何在Android中的SQlite中使用预准备语句?

#1


1  

String should be between two '' so instead :

字符串应该在两个''之间,所以相反:

String select_query= "SELECT * FROM " +MID_SEM_ANSWERS_TABLE + " WHERE " + 
     KEY_ID + " = " + answerID +" AND " + KEY_SUBJECT + " = '" + subject + "'";
//----------------------------------------------------------^---------------^

Note

The important thing, your code still not secure, and can cause Syntax error or SQL Injection, i suggest to use PreparedStatement instead, read also more about How do I use prepared statements in SQlite in Android?.

重要的是,你的代码仍然不安全,并且可能导致语法错误或SQL注入,我建议使用PreparedStatement,另外阅读更多关于如何在Android中的SQlite中使用预准备语句?