Smitha
Smitha

Reputation: 6134

rawQuery() syntax error

I want to execute the following SQL query using SQLite's rawQuery() in Android.
But I am getting
"android.database.sqlite.SQLiteException: near "status": syntax error: , while compiling: select emp_id,start_date,end_date fromleave_plans1where status = ?"

SQL query : select emp_id,start_date,end_date from leave_plans where status="approved" or status="pending"

and rawQuery i have used :

db.rawQuery("select emp_id,start_date,end_date from" + TABLE_NAME + "where status = ?",new String[] {"approved","pending"});  

Please help

Thanks
sneha

Upvotes: 0

Views: 2287

Answers (3)

rana
rana

Reputation: 87

for string values it must like status='approved'

like this 'BID'

db_obj.rawQuery("select page_no from BOOK_MARKS where page_no= "+page + " and book_id= "+"'"+BID+"'", null);

Upvotes: 0

epochengine
epochengine

Reputation: 2062

Try this code:

String[] columns = { "emp_id", "start_date", "end_date" };
String[] whereArguments = { "approved", "pending" };
Cursor cursor = db.query(TABLE_NAME, columns, "status = ? OR status = ?", whereArguments, null, null, null);

The two question marks are necessary in the third argument in the query call as each ? character references one element in the whereArguments array. So this call will translate to:

SELECT emp_id, start_date, end_date FROM {TABLE_NAME} WHERE status = "approved" OR status = "pending";

(Where {TABLE_NAME} will obviously be replace by whatever String constant it represents.)

Upvotes: 1

Rasel
Rasel

Reputation: 15477

String status="approved";
db.rawQuery("select emp_id,start_date,end_date from " + TABLE_NAME + " where status = '"+status+"'",new String[] {"approved","pending"});

or

db.rawQuery("select emp_id,start_date,end_date from " + TABLE_NAME + " where status = 'approved'",new String[] {"approved","pending"});

I am not understanding why you are using like this

You can use

return db.query(TABLE_NAME, new String[] {emp_id,start_date,end_date

        },status +"='"+status+"'", null, null, null, null); 

Upvotes: 0

Related Questions