Gaurav Agarwal
Gaurav Agarwal

Reputation: 19102

SQL rawquery in android

I know my question is very simple, I am trying to work out this rawQuery for hour, any help will be highly appreciated...

String queryString = "SELECT _id FROM " +  TABLE_SELECTED_ONE + " WHERE DifficultyLevel=? AND Attempt=?"; 
    Cursor cursor = Db.rawQuery(queryString, new String[] {beginnerOrAdvanced, "no"} );

but it crashes

12-08 02:18:33.091: ERROR/AndroidRuntime(546): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wissenways.practicequestion/com.wissenways.practicequestion.QuesBankSelect}: android.database.sqlite.SQLiteException: near ".": syntax error: , while compiling: SELECT _id FROMQuantQuesBank1WHEREQuantQuesBank1.DifficultyLevel='beginner' ANDQuantQuesBank1.Attempt ='no'

Upvotes: 1

Views: 2108

Answers (1)

Chris
Chris

Reputation: 1637

I never got args to work in all the apps I published, try

Db.rawQuery(queryString, null);

and build up your string in it's full state as a pure string

example :

String dateString = Utils.makeDateString(mCalDate);
String sqlQuery = "select * from table where entrydate = '" + dateString + "'";

mSearchCursor = db.rawQuery(sqlQuery, null);

That way you can always run the SQL query in a SQL browser app on the table (extract from phone via DDMS) and check it works outside of Android.

It's just the output looks very wrong and spaces have been removed that were essential

Upvotes: 2

Related Questions