限制CursorLoader查询 [英] Limit the query in CursorLoader

查看:528
本文介绍了限制CursorLoader查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过CursorLoader查询到的SQLite数据库的数据。当我通过在Android开发者网站上的文档,我找不到限制查询。

I am trying to query data from sqlite db through CursorLoader. When i go through the documentation in android developer website, i can't find the limit query.

CursorLoader(上下文的背景下,开放的URI,字符串[]投影,选择字符串,字符串[] selectionArgs两个,字符串排序顺序)

在CursorLoader我怎样才能实现限制吗?

How can i implement limit in CursorLoader?

在此先感谢

推荐答案

在hackish的方式:

The hackish way:

String sortOrder = "ROWID LIMIT 5"

这将导致 ORDER BY ROWID LIMIT 5 ROWID 将由隐行ID是的SQLite保持排序 - 非常接近,当你不指定排序奥德都发生了什么。 不,这是滥用的事实,命令参数不检查所提供的值仅仅是一个有效的订单类型。怎样的一个缓冲区溢出攻击。

which will result in ORDER BY ROWID LIMIT 5. ROWID would sort by the implicit row ids that SQLite keeps - very close to what happens when you don't specify a sort oder at all. Not that this is abusing the fact that the order parameter does not check if the supplied value is just a valid order type. Kind of a buffer overflow attack.

有一个更好的方法是定义乌里参数。

A better way would be to define Uri parameters.

// query code
Uri queryUri = Uri.parse("content://what/ever/items");
queryUri = queryUri.buildUpon().appendQueryParameter("limit", "5").build();

// in ContentProvider
String limit = queryUri.getQueryParameter("limit");

这两种方法都将只工作,如果的ContentProvider 与上述企图限制兼容。其实是有没有明确定义的方式从的ContentProvider 限制或选择特定的数据。每个的ContentProvider 带有它自己的合同,以便上面不会完全提供商合作。

Both approaches will only work if the ContentProvider is compatible with above attempts to limit. There is actually no well-defined way to limit or select certain data from a ContentProvider. Each ContentProvider comes with it's own contract so above will not work with every provider.

这篇关于限制CursorLoader查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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