我想在Android中使用SQLite的rawQuery()执行以下SQL查询 但我得到了 “android.database.sqlite.SQLiteException:near”status“:语法错误:,同时编译:select emp_id,start_date,end_date fromleave_plans1where status =?”
SQL查询:从leave_plans中选择emp_id,start_date,end_date,其中status =“approved”或status =“pending”
和我使用的rawQuery:
db.rawQuery("select emp_id,start_date,end_date from" + TABLE_NAME + "where status = ?",new String[] {"approved","pending"});
请帮忙
感谢
斯纳
答案 0 :(得分:1)
试试这段代码:
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);
query
调用的第三个参数中需要两个问号,因为每个?
字符引用whereArguments
数组中的一个元素。因此,此调用将转换为:
SELECT emp_id,start_date,end_date FROM {TABLE_NAME} WHERE status =“approved”OR status =“pending”;
({TABLE_NAME}显然会被它所代表的任何字符串常量替换。)
答案 1 :(得分:0)
String status="approved";
db.rawQuery("select emp_id,start_date,end_date from " + TABLE_NAME + " where status = '"+status+"'",new String[] {"approved","pending"});
或
db.rawQuery("select emp_id,start_date,end_date from " + TABLE_NAME + " where status = 'approved'",new String[] {"approved","pending"});
我不明白你为什么这样使用
您可以使用
return db.query(TABLE_NAME, new String[] {emp_id,start_date,end_date
},status +"='"+status+"'", null, null, null, null);
答案 2 :(得分:0)
对于字符串值,它必须像status ='approved'
喜欢这个'BID'
db_obj.rawQuery(“从BOOK_MARKS中选择page_no,其中page_no =”+ page +“和book_id =”+“'”+ BID +“'”,null);