如何使用选择参数来查询Android中ContentProvider中的特定行 [英] How to use selection args to query specific rows from a contentprovider in android

查看:50
本文介绍了如何使用选择参数来查询Android中ContentProvider中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个基本的内容提供程序,用于存储SMS消息以供学习,到目前为止,我可以阅读(不选择args),插入,更新和删除.

i have constructed a basic content provider that stores SMS messages for learning purposes, so far i can read(without selection args), insert, update and delete.

但是我很困惑,试图弄清楚如何在我的提供程序中格式化WHERE子句的选择参数:

However i have been stumped trying to figure out how to format the selection args for the WHERE clause in my provider:

基本上,我的应用程序需要搜索特定的时间戳(长格式)并返回其_id

Basicly my application needs to search for a specific timestamp (in long format) and return its _id

说您的数据库有一个您试图访问的条目,如下所示:

say your database has an entry like this that your trying to access:

2 | 1 | 1410293471300 |测试类型1 ||测试| 0

2|1|1410293471300|test type 1||testing|0

整个数据库如下:

_id | CLIENTTRXID | CREATED_AT | TYPE | MESSAGEPRIO | MESSAGE | Acepted

_id|CLIENTTRXID|CREATED_AT|TYPE|MESSAGEPRIO|MESSAGE|ACCEPTED

1 | 1 | 1410293471000 |测试类型1 ||测试| 0

1|1|1410293471000|test type 1||testing|0

2 | 1 | 1410293471300 |测试类型1 ||测试| 0

2|1|1410293471300|test type 1||testing|0

3 | 1 | 1410293471600 |测试类型1 ||测试| 0

3|1|1410293471600|test type 1||testing|0

在sql中查询将是从CREATED_AT = 1410293471300的警报中选择_id;"

in sql the query would be "select _id from alerts where CREATED_AT=1410293471300;"

我希望的代码能做到同样的效果:

the code i was hoping would do the equivalent:

//normally i would get the string dynamically but to make it equal to the sql
String date = "1410293471300";
String[] selectionArgs = new String[]{ date };

Cursor cursor = getContext().getContentResolver().query(AlertContract.CONTENT_URI, null, AlertContract.Column.CREATED_AT, selectionArgs, AlertContract.DEFAULT_SORT);

无论我作为selectionArgs尝试什么,似乎总是会产生以下错误

seems to always produce the following error no matter what i try as selectionArgs

Exception caught﹕ Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.

这是我的内容提供商的查询方法:

here is the query method of my contentprovider:

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables( AlertContract.TABLE);

    switch (sURIMatcher.match(uri)) {
        case AlertContract.STATUS_DIR:
            break;
        case AlertContract.STATUS_ITEM:
            qb.appendWhere(AlertContract.Column.ID + "=" + uri.getLastPathSegment());
            break;
        default:
            throw new IllegalArgumentException( "illegal uri: " + uri);
    }
    String orderBy = (TextUtils.isEmpty(sortOrder)) ? AlertContract.DEFAULT_SORT : sortOrder;
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

    //register for uri changes
    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    Log.d(TAG, "queried records: "+cursor.getCount());
    return cursor;
}

想必我错过了一个非常明显的问题,并且对于发布此问题会感到很傻.

Presumably im missing something extremely obvious, and will feel quite silly for having posted this question.

但是目前我非常感谢任何帮助,因为我很沮丧.

But for the moment i would very much appreciate any help, as i am quite stumped.

推荐答案

看来,问题出在您的 selection ,而不是您的 selectionArgs 本身.选择应该是"where"之后的整个查询.在这里,您的选择"CREATED_AT" .您还需要两项才能使其正常工作:

It looks like your issue is with your selection, rather than with your selectionArgs per se. The selection should be the whole query after the "where". Here your selection is "CREATED_AT". You need two more items to get it to work:

  1. 一个 = ,因为您想要相等(当然,您也可以执行其他运算符)
  2. 一个?.这是您的selectionArgument插入的位置(每个自变量在选择中需要一个?,因此,选择中应该有与selectionArguments相同数量的?.
  1. an =, since you want equality (you can also do other operators, of course)
  2. a ?. This is where your selectionArgument will be inserted (each argument needs a ? in the selection, so there should be the same number of ?s in the selection as selectionArguments.

最终结果应该更像"CREATED_AT =?"

签出

Check out the documentation and this tutorial for more info on how to correctly construct a ContentProvider query.

这篇关于如何使用选择参数来查询Android中ContentProvider中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆