【问题标题】:filter contact names by sql query通过 sql 查询过滤联系人姓名
【发布时间】:2012-06-21 10:55:26
【问题描述】:

我一直在尝试使用 ContactsContract 获取所有联系人。我想使用“选择”参数过滤结果。换句话说,我想检索所有以字母A开头的联系人。所以我写了这个sn-p。但它崩溃了。

Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
        Contacts._ID, // the contact id column
        Contacts.DISPLAY_NAME,  // column if this contact is deleted
        Contacts.HAS_PHONE_NUMBER   };

String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + 
            " COLLATE LOCALIZED ASC";
cursor = getContentResolver().query
            (uri,
            projection,
            "android.provider.ContactsContract.Contacts.DISPLAY_NAME like 'A'",
            null,
            sortOrder);

据我所知,我可以像在 SQL 语句的 where 子句中插入的内容一样插入选择参数,这就是我所做的。

【问题讨论】:

    标签: android sql android-contacts


    【解决方案1】:

    给你。

    db.query(
      TABLE_NAME,
      projection, 
      Contacts.DISPLAY_NAME + " like " + "'?%'",
      new String[] { "A" },
      null, null, null
    );
    

    【讨论】:

    • 最后只有一个null。但是我写了 cursor = getContentResolver().query(uri, projection, Contacts.DISPLAY_NAME + " like " + "'?%'", new String[] { "A" }, sortOrder);它崩溃了。
    • 是的,我试过你的查询,?没有和我一起工作。但它以这种方式工作; getContentResolver().query(uri, projection, Contacts.DISPLAY_NAME + " like " + "'A%'", null, null)。谢谢
    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多