rawQuery() syntax error

2019-08-20 16:42发布

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

3条回答
爷、活的狠高调
2楼-- · 2019-08-20 16:59

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);

查看更多
成全新的幸福
3楼-- · 2019-08-20 17:01
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); 
查看更多
不美不萌又怎样
4楼-- · 2019-08-20 17:09

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

查看更多
登录 后发表回答