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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:04:55  来源:igfitidea点击:

Using the LIMIT statement in a SQLite query

androidsqlite

提问by Mike Cialowicz

I have a query that selects rows in a ListViewwithout having a limit. But now that I have implemented a SharedPreferencesthat 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 LIMITclause. Remove it.

等号 ( =) 运算符不与LIMIT子句一起使用。去掉它。

Here's an example LIMITquery:

这是一个示例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

这张图很有用: 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 LIMITclause 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,limitNumbereven though it doesn't accept the OFFSETstatement 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";