如何为 Android SQLite 查询从动态 ArrayList 编写 WHERE IN 子句

发布于 2024-11-30 12:42:43 字数 627 浏览 3 评论 0原文

如何在 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, 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, new String[] { KEY_CUSTOMER_ID, KEY_NAME}, KEY_GROUP_ID+" = "+groupIds, null, null, null, null);
}

groupId ArrayList 的大小是动态的。

How can i write the where in clause in Android SQLite query?

Function to retrieve a single customer

public Cursor getCustomers(int groupId) {
    return db.query(TABLE_CUSTOMERS, new String[] { KEY_CUSTOMER_ID, KEY_NAME}, KEY_GROUP_ID+" = "+groupId, null, null, null, null);
}

Function to retrieve a more customers

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, new String[] { KEY_CUSTOMER_ID, KEY_NAME}, KEY_GROUP_ID+" = "+groupIds, null, null, null, null);
}

The size of the groupId ArrayList is dynamic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生死何惧 2024-12-07 12:42:43

您可以使用 Android TextUtils 类 join 方法来创建逗号分隔列表要放入 in 子句中的 ID 数。

String selection = KEY_GROUP_ID + " IN (" + TextUtils.join(", ", groupIds) + ")";
return db.query(TABLE_CUSTOMERS, new String[] { KEY_CUSTOMER_ID, KEY_NAME}, selection, null, null, null, null);

顺便说一句,如果 groupIds 是另一个表中的主键列表,您应该使用长整型来存储它们而不是整数,否则当 ID 变大时它会溢出。

You can use the Android TextUtils class join method to make a comma separated list of IDs to put in the in clause.

String selection = KEY_GROUP_ID + " IN (" + TextUtils.join(", ", groupIds) + ")";
return db.query(TABLE_CUSTOMERS, new String[] { KEY_CUSTOMER_ID, KEY_NAME}, selection, null, null, null, null);

BTW if groupIds is a list of primary keys from another table you should be using Longs to store them not Integers otherwise it will overflow when the IDs get large.

維他命╮ 2024-12-07 12:42:43
return db.query(TABLE_CUSTOMERS, new String[] { KEY_CUSTOMER_ID, KEY_NAME }, KEY_GROUP_ID + " >=" + groupIdsLowLimit + " and " + KEY_GROUP_ID + " <=" + groupIdsUpLimit, null, null, null, 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, new String[] { KEY_CUSTOMER_ID, KEY_NAME }, where, null, null, null, null);
return db.query(TABLE_CUSTOMERS, new String[] { KEY_CUSTOMER_ID, KEY_NAME }, KEY_GROUP_ID + " >=" + groupIdsLowLimit + " and " + KEY_GROUP_ID + " <=" + groupIdsUpLimit, null, null, null, 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, new String[] { KEY_CUSTOMER_ID, KEY_NAME }, where, null, null, null, null);
遗弃M 2024-12-07 12:42:43

我想在 @JosephL's 答案根据我的研究:

我有两个ArrayList,具有以下值:

第一个ArrayList(在第一个列)具有重复值,而第二个 ArrayList(在第二列中)具有唯一值。

=> 67 : 35
=> 67 : 36
=> 70 : 41
=> 70 : 42

处理后打印两者,如下所示:

  1. 第一个数组:"(" + TextUtils.join(",", arrayList1) + ")"
  2. 第二个数组:"(" + TextUtils.join(",", arrayList2) + ")"
  3. 第一个数组(使用 new 删除重复项HashSet<>(arrayList1)):

    "(" + TextUtils.join(",", new HashSet<>(arrayList1)) + ")"

    =>第一个数组:(67,67,70,70)

    =>第二个数组:(35,36,41,42)

    =>第一个数组(使用 new HashSet<>(arrayList1) 删除重复项):(67,70)

希望它有用。谢谢。

I want to add something in @JosephL's answer as per my study:

I have two ArrayList with following values:

First ArrayList (in First Column) have duplicate values and Second ArrayList (in Second Column) has unique values.

=> 67 : 35
=> 67 : 36
=> 70 : 41
=> 70 : 42

Printing both after processing as below:

  1. First Array : "(" + TextUtils.join(",", arrayList1) + ")"
  2. Second Array : "(" + TextUtils.join(",", arrayList2) + ")"
  3. First Array (Remove Duplicates using new HashSet<>(arrayList1)):

    "(" + TextUtils.join(",", new HashSet<>(arrayList1)) + ")"

    => First Array : (67,67,70,70)

    => Second Array : (35,36,41,42)

    => First Array (Remove Duplicates using new HashSet<>(arrayList1)): (67,70)

Hope it will useful. Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文