我想根据血型选择数据。假设我想选择所有' A +'我的桌子里有血型。将应用什么查询。
public void selectbybloodtypes(){
String s = "A+" ;
String query = "SELECT * FROM " + TABLE_CONTACTS + "WHERE bloodtypes= '" + s ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, String[] { });
Contact contact = null;
if (cursor.moveToFirst())
{
do
{
contact = new Contact();
contact.setbloodtypes(cursor.getString(3));
Log.d("getAllContacts()", contact.getBloodtypes().toString());
}while(cursor.moveToNext());
}
}
答案 0 :(得分:1)
您缺少查询参数的结束引用,加上WHERE关键字之前的空格。应该是:
"SELECT * FROM " + TABLE_CONTACTS + " WHERE bloodtypes = '" + s + "'";
我还建议使用任何query()
方法而不是rawQuery()
。它们有助于避免常见错误。