-3

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 :

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

YCF_L
  • 49,027
  • 13
  • 75
  • 115

1 Answers1

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

Graham
  • 6,577
  • 17
  • 55
  • 76
YCF_L
  • 49,027
  • 13
  • 75
  • 115