Android从sqlite数据库中获取多个项目
我想在不使用 for/while/dowhile 循环的情况下更新(检索并可能编辑)SQLite 数据库中的多个项目
这可以使用游标吗?我想限制表中的个人查找
是否有一些复杂的 Android 数据对象,我可以将 SQL 查询的所有结果转储到其中,然后我可以解析 for 循环
我听说过 Parceable
和可序列化的对象,但这就是我使用数据库的目的。
我目前有通过单个行调用元素的方法。我可以将该方法放入 for 循环中,或者我可以将 Cursor 的查询放入 for 循环中,但我不想进行那么多访问。
public String fetchDay(long rowId) throws SQLException {
Cursor mCursor =
mapDb.query(true, DAY_TABLE, new String[] {KEY_ROWID, KEY_DAY}, KEY_ROWID + "=" + String.valueOf(rowId), null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
String result = mCursor.getString(mCursor.getColumnIndex(KEY_DAY));
mCursor.close();
return result;
}
我如何获取该表的所有元素并将其存储在一个复杂的对象中而不循环
洞察力赞赏
I was like to update (retrieve and possibly edit) multiple items in an SQLite database without using a for/while/dowhile loop
Is this possible using the Cursor? I would like to limit individuals lookups in the table
Is there some complex Android data object that I can dump all results from an SQL query into, that I could then parse for loop
I've heard about Parceable
and serializable objects, but that is sort of what I am using the database for.
I currently have methods to call elements by an individual row. And I could put that method in a for loop, or I could have Cursor's query be in a for loop BUT I don't want to do that many accesses.
public String fetchDay(long rowId) throws SQLException {
Cursor mCursor =
mapDb.query(true, DAY_TABLE, new String[] {KEY_ROWID, KEY_DAY}, KEY_ROWID + "=" + String.valueOf(rowId), null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
String result = mCursor.getString(mCursor.getColumnIndex(KEY_DAY));
mCursor.close();
return result;
}
How would I get all the elements of that table and store in in a complex object without looping
Insight appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下内容更改查询以返回多行:
作为 where 子句。
这样做时,游标将返回一系列行,然后您可以使用 while 循环对其进行迭代,如下所示:
结果为
List
请注意,此中使用的 while 循环解决方案比一遍又一遍地查询表要便宜得多,因为它在内存上的 Cursor 对象上进行迭代,而不是进行多次 I/O 操作
You can change your query to return more than a single row, by using something like:
as your where clause.
Doing so, the Cursor will return a range of rows, and you can then iterate on it using a while loop, like this:
With results being a
List<String>
Note that the while loop used in this solution is way less expensive than querying the table over and over, bexause it iterates on the Cursor object, that's on the memory, instead of making several I/O operations
“是否有一些复杂的 Android 数据对象,我可以将 SQL 查询的所有结果转储到其中,然后我可以解析 for 循环?”
是的,那就是 光标。它表示数据库查询返回的结果集,类似于 JDBC 结果集。游标应该被视为数据库查询结果的容器,尽管我不确定它是否保存数据本身。
如果您的数据库查询返回多行,您将通过游标引用它们。
阅读这篇文章以获得更好的想法。
无论哪种方式,您都必须使用某种循环来迭代结果集每一行的游标。
"Is there some complex Android data object that I can dump all results from an SQL query into, that I could then parse for loop?"
Yes, and that's The Cursor. It represents the result set returned by a database query, similar to the JDBC result set. The Cursor should be seen as a container for the results of a database query, although I am not sure if it holds the data itself.
If your database query returns multiple rows, you will have them referenced via the cursor.
Take a loot at this article to get better idea.
Either way, you have to use some kind of loop to iterate over your cursor for each row of the result set.