在 Android 中编程时,我是否需要游标来从 SQLite 检索简单的只读值?

发布于 2024-10-15 09:52:42 字数 764 浏览 2 评论 0原文

我有 PHP 背景,但从未使用过游标。

我按照 Google Android 记事本示例进行了使用游标和数据库适配器的练习。我模糊地理解光标的意义是“协助 Android 中发生的生命周期活动”或引用示例,“...这允许 Android 处理光标生命周期,而不是我们需要担心它” 。

我的问题是,如果您正在做一些简单的事情,例如获取只读值,那么使用游标仍然是最好的主意吗?是否还有其他更好(更快)的方式来检索数据?使用游标似乎比该特定查询所需的开销更多。

下面代码的目的是获取购物车中的商品数量。

public long getNumItemsInCart(Long rowId) throws SQLException {
        Cursor mCursor =
            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NUM_ITEMS_IN_CART}, KEY_ROWID + "='" + rowId + "'", null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor.getLong(mCursor.getColumnIndexOrThrow(ProductsDbAdapter.KEY_NUM_ITEMS_IN_CART));        
    }

I am from a PHP background where I never used cursors.

I have followed the Google Android Notepad example where I got practice using cursors and database adaptors. I vaguely understand the point of the cursor is "to assist with the life cycle activities that happen in Android" or quoting the example, "...that allows Android to take care of the Cursor lifecycle instead of us needing to worry about it".

My question is if you're doing something simple like obtaining a read-only value, is it still the best idea to use a cursor? Is there maybe some other better (faster) way of retrieving the data? Using a cursor seems more overhead than needed for this particular query.

The point of the code below is to obtain the number of items in a cart.

public long getNumItemsInCart(Long rowId) throws SQLException {
        Cursor mCursor =
            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NUM_ITEMS_IN_CART}, KEY_ROWID + "='" + rowId + "'", null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor.getLong(mCursor.getColumnIndexOrThrow(ProductsDbAdapter.KEY_NUM_ITEMS_IN_CART));        
    }

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

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

发布评论

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

评论(1

乜一 2024-10-22 09:52:42

我不知道还有其他更好(更快)的方法来从数据库获取简单值。

当然,您可以在数据库(ContentProvider)和应用程序逻辑之间放置您自己的代码层,您可以在其中创建自己的便捷方法来执行此类操作。但是,正如您肯定已经知道的那样,如果编写不当,额外的层可能会导致性能下降。

您还可以缓存您的简单值(在初始化期间读取一次并将其保存在类的适当成员字段中)。但这意味着您失去了对其生命周期的控制。您不能直接对 Long 或 Integer 值调用“重新查询”(不存在 Long.requery() 之类的东西)。

因此,是的,对于某些用例来说,为单个值提供整个 Cursor 对象可能看起来有些过分,但是;从数据库检索单个值(一列、一行)并不是最常见的用例。对于此类值,您可能会使用 SharedPreferences 基础设施。您很可能会从数据库中获得几列和几行,从这个角度来看,我认为您会发现 Cursor 对象非常方便。

I don't know of any other better (faster) way of getting a simple value from the database.

You can of course put your own layer of code between your database (ContentProvider) and your application logic where you can create your own convenience methods for performing such manoeuvres. But, as you surely already know, an extra layer might cause you performance penalties if not written properly.

You could also cache your simple value (read it once, during initialization and save it in an appropriate member field of your class). But this would mean that you loose control over it's life cycle. You can't call a "re-query" directly on a Long or Integer value (there is no such thing as Long.requery()).

So, yes, having an entire Cursor object for a single value might seem overkill for some use-cases, but then; it's not the most common use-case to retrieve a single value (one column, one row) from a database. For such values you would perhaps use the SharedPreferences infrastructure. Most likely you will get several columns and several rows from a database, and from that perspective I think you will find the Cursor object very handy.

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