如何在
Android sqlite查询中编写where in子句?
检索单个客户的功能
public Cursor getCustomers(int groupId) { return db.query(TABLE_CUSTOMERS,new String[] { KEY_CUSTOMER_ID,KEY_NAME},KEY_GROUP_ID+" = "+groupId,null,null); }
功能检索更多客户
public Cursor getCustomers(ArrayList<Integer> groupIds) { // Need to apply SELECT id,name FROM customers WHERE id IN (11,13,...18); //return db.query(TABLE_CUSTOMERS,KEY_GROUP_ID+" = "+groupIds,null); }
groupId ArrayList的大小是动态的.
解决方法
return db.query(TABLE_CUSTOMERS,KEY_NAME },KEY_GROUP_ID + " >=" + groupIdsLowLimit + " and " + KEY_GROUP_ID + " <=" + groupIdsUpLimit,null); String where = ""; for (int i = 0; i < list.size(); i++) { where = where + KEY_GROUP_ID + " =" + list.get(i).toString() + ""; if (i != (list.size() - 1)) where = where + " or"; } return db.query(TABLE_CUSTOMERS,where,null);