Android - 引用数据库中数据的最佳方式

发布于 2024-11-26 14:56:28 字数 493 浏览 1 评论 0原文

我有一个使用 sqlite 数据库的应用程序。目前,我有一个数据库助手类,我从主类中调用它来访问数据库。然后,我将检索到的项目临时存储在游标中。每次我想访问数据库时,我都必须打开数据库,创建一个新游标,查询数据库并将结果添加到游标,从游标中提取信息,关闭游标,关闭数据库,然后显示用户。

现在,每次我想访问数据库时,我都会访问数据库帮助程序类 我每次都会创建一个新光标 我正在进行 sqlite 查询。

我想知道是否应该更改我的代码来执行以下操作: 打开数据库 将所有数据存储到游标 关闭数据库

,然后每当我想访问信息时,我可以只引用光标,从而无需再次调用数据库帮助器类。

或者将数据存储到数组中执行以下操作会更有效: 打开数据库 将数据存储到游标 循环游标并将数据存储到数组中 关闭光标 close db

它们之间有什么区别(处理时间和内存使用)?是否有更有效的方法来使用最少的资源来执行此操作?使用数组和游标哪个更快?我不想浪费CPU时间来调用不必要的函数或使用更多内存来存储臃肿的对象。

I have an app that uses an sqlite database. Currently I have a database helper class that I call from within my main class to access the database. I then temporarily store the items I retrieved in a cursor. Every time I want to access my database I have to open the db, create a new cursor, query the db and add the results to the cursor, pull the information out of the cursor, close the cursor, close the db, then display to the user.

Now I'm accessing the db helper class every time I want to access the db
I'm creating a new cursor every time
And I'm making sqlite queries.

I was wondering if I should change my code to do something like:
Open DB
Store all data to a cursor
close db

then whenever I want to access the information I can instead just reference the cursor eliminating the need to call the db helper class again.

Or would it be more efficient to instead store the data to an array doing something like:
open db
store data to a cursor
loop through cursor and store data into an array
close cursor
close db

What would the difference be (processing time, and memory use) between these and is there an even more efficient way of doing this that uses the least resources? Is it faster to use an array or a cursor? I don't want to waste cpu time calling unnecessary functions or use more memory storing bloated objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一片旧的回忆 2024-12-03 14:56:28

在您的活动中,如果您想执行一次选择查询并将该数据用于您的活动,那么您必须从 Cursor 创建一个 ArrayList 或 Hashmap,然后您必须关闭光标。

但是,如果您必须多次访问数据库,并且通过执行查询获得不同的结果,那么您必须使用游标并从游标更新中获取数据到 UI 并关闭游标。

您正在使用 Helper 类,因此它是最佳选择,因此您不必担心处理问题。

当您想要获取数据时,只需创建一个 Helper 类的对象,打开连接,执行查询并返回数据,从活动中的游标获取数据,更新 UI,关闭数据库。

这是正常的方式,也是一个好的方式。

In your activity if you want to execute a select query once and you using that data to your activity then you have to make a ArrayList or Hashmap from Cursor and then you have to close your cursor.

But if you have to access a database many times and you are getting a different different results by executing a query then you have to use cursor and fetch data from cursor update to UI and close Cursor.

You are using Helper class so it is the best choice so you don't have to wonder about processing.

when ever you want to fetch data then just make a object of Helper class,Open connection,execute query and return data,fetch data from cursor in your activity,Update UI,close database.

this is the normal way and also it is a good way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文