Android 在 SQLite 查询中使用 LIMIT 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2497677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the LIMIT statement in a SQLite query
提问by Mike Cialowicz
I have a query that selects rows in a ListView
without having a limit. But now that I have implemented a SharedPreferences
that the user can select how much rows will be displayed in the ListView
, my SQLite query doesn't work. I'm passing the argument this way:
我有一个查询,可以在ListView
没有限制的情况下选择行。但是现在我已经实现了SharedPreferences
用户可以选择将在 中显示的行数ListView
,我的 SQLite 查询不起作用。我以这种方式传递论点:
return wDb.query(TABELANOME, new String[] {IDTIT, TAREFATIT, SUMARIOTIT}, CONCLUIDOTIT + "=1", null, null, null, null, "LIMIT='" + limite + "'");
回答by Mike Cialowicz
The equals (=
) operator is not used with the LIMIT
clause. Remove it.
等号 ( =
) 运算符不与LIMIT
子句一起使用。去掉它。
Here's an example LIMIT
query:
这是一个示例LIMIT
查询:
SELECT column FROM table ORDER BY somethingelse LIMIT 5, 10
Or:
或者:
SELECT column FROM table ORDER BY somethingelse LIMIT 10
In your case, the correct statement would be:
在你的情况下,正确的说法是:
return wDb.query(TABELANOME, new String[] {IDTIT, TAREFATIT, SUMARIOTIT}, CONCLUIDOTIT + "=1", null, null, null, null, String.valueOf(limite));
Take a look here at the SQLite select syntax: http://www.sqlite.org/syntaxdiagrams.html#select-stmt
在此处查看 SQLite 选择语法: http://www.sqlite.org/syntaxdiagrams.html#select-stmt
This image is rather useful: http://www.sqlite.org/images/syntax/select-stmt.gif
回答by Jon O
For anyone stumbling across this answer looking for a way to use a LIMIT
clause with an OFFSET
, I found out from this bugthat Android uses the following regex to parse the limit clause of a query:
对于任何在这个答案中绊倒寻找一种使用带有 的LIMIT
子句的方法的人OFFSET
,我从这个错误中发现 Android 使用以下正则表达式来解析查询的限制子句:
From <framework/base/core/java/android/database/sqlite/SQLiteQueryBuilder.java>
从 <framework/base/core/java/android/database/sqlite/SQLiteQueryBuilder.java>
LIMIT clause is checked with following sLimitPattern.
使用以下 sLimitPattern 检查 LIMIT 子句。
private static final Pattern sLimitPattern = Pattern.compile("\s*\d+\s*(,\s*\d+\s*)?");
Note that the regex does accept the format offsetNumber,limitNumber
even though it doesn't accept the OFFSET
statement directly.
请注意,正则表达式确实接受格式,offsetNumber,limitNumber
即使它不OFFSET
直接接受语句。
回答by Pepijn
Due to this bug which also doesn't allow for negative limits
由于这个错误也不允许负限制
8,-1
8,-1
I had to use this workaround
我不得不使用这个解决方法
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(table);
String query = builder.buildQuery(projection, selection, null, null, null, sortOrder, null);
query+=" LIMIT 8,-1";