使用完由 SQLiteOpenHelper 实现检索的数据库后,如何处理它?

发布于 2024-10-21 13:38:04 字数 1940 浏览 1 评论 0原文

亲爱的 Android 开发者们!


编辑:

谢谢大家的回答。我从他们中的许多人那里看到,在数据库适配器中编写自己的 close() 方法似乎是常见的(并且被接受的)做法。很公平。

但它如何与 ContentProvider 一起使用呢?通常,当通过我的 ContentProvider 查询我的数据库时,我只是发出类似这样的问题:

Cursor managedCursor = managedQuery(...);

我不知道如何使用这种方法来访问我的自定义 close() 方法自定义 ContentProvider 实现。我是否应该从我的 Activity 中执行以下操作:

MyCustomProvider myProvider = (MyCustomProvider) getContentResolver();

然后:

myProvider.query(...);
myProvider.close();

最重要的是;这有必要吗(从下面第 2 点开始)?

结束编辑


在某种程度上,我必须说我了解了 SQLiteOpenHelper 的概念,它是什么,它是如何使用的等等。我什至在编写自己的 ContentProvider 时定期使用它。

问题是我不确定如何处理由 myOpenHelper.getWritableDatabase() (或 myOpenHelper.getReadableDatabase())返回的 SQLiteDatabase 对象 功能重要)当我完成它时。

根据 Android ContentProvider.onCreate() 文档:

您应该推迟重要的初始化(例如打开、升级和扫描数据库),直到使用内容提供程序(通过 query(Uri, String[], String, String[], String)插入(Uri、ContentValues)等)。

[...]

如果您确实使用SQLiteOpenHelper,请确保避免从此方法调用getReadableDatabase()getWritableDatabase()。 (相反,重写 onOpen(SQLiteDatabase) 来在数据库首次打开时对其进行初始化。)

上面给出了在何处初始化数据库的提示(query(...)) code>、insert(...) 等函数),但它没有告诉我在使用完后如何处理创建的 SQLiteDatbase 对象它。

  1. 我应该将其保存为我的 ContentProvider 实现的成员变量(并将其视为“私有单例”以供将来使用)吗?

  2. 我应该只是退出query(...)insert(...)等函数时保留它,并相信SQLiteOpenHelper将管理在以后的通话中为我提供它吗?

  3. [在此处插入您的替代观点]< /em>

作为信任(或懒惰)的开发人员,我已经根据上面的第二个替代方案实现了我的代码。但我无法摆脱一种令人毛骨悚然的感觉,那就是我忽略了一些重要的事情。

Dear Fellow Android Developers!

EDIT:

Thank you all for your answers. I see from many of them that it seems to be common (and accepted) practice to write your own close() method in your database adapter. Fair enough.

But how does that work with a ContentProvider? Usually when querying my database through my ContentProvider I simply issue something like:

Cursor managedCursor = managedQuery(...);

I don't see how I, with this methodology, can access the custom close() method in my custom ContentProvider implementation. Should I instead, from my Activity, do something like:

MyCustomProvider myProvider = (MyCustomProvider) getContentResolver();

and then:

myProvider.query(...);
myProvider.close();

And above all; is this at all necessary (as of point 2 below)?

END EDIT

To a certain degree I must say that I get the concept of the SQLiteOpenHelper, what it is, how it's used and so. I even use it on a regular basis when I write my own ContentProvider's.

The thing is that I'm not sure what to do with the SQLiteDatabase object, returned by the myOpenHelper.getWritableDatabase() (or the myOpenHelper.getReadableDatabase() function for what matters) when I'm done with it.

According to Android ContentProvider.onCreate() documentation:

You should defer nontrivial initialization (such as opening, upgrading, and scanning databases) until the content provider is used (via query(Uri, String[], String, String[], String), insert(Uri, ContentValues), etc).

[...]

If you do use SQLiteOpenHelper, make sure to avoid calling getReadableDatabase() or getWritableDatabase() from this method. (Instead, override onOpen(SQLiteDatabase) to initialize the database when it is first opened.)

The above gives me a hint where to initialize the database (the query(...), insert(...), etc functions), but it doesn't tell me anything on how to treat the created SQLiteDatbase object when I've finished using it.

  1. Should I save it as a member variable of my ContentProvider implementation (and treat it much like a "private singleton" for future use)?

  2. Should I just leave it when exiting the query(...), insert(...), etc. functions and trust that the SQLiteOpenHelper will manage it for me in future calls?

  3. [Insert your alternative point-of-view here]

Being the confiding (or lazy) developer I've implemented my code according to the second alternative above. But I can't get rid of the creepy feeling that I'm neglecting something important.

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

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

发布评论

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

评论(3

木落 2024-10-28 13:38:04

这取决于您对数据库的操作。如果您只是执行插入、删除或选择返回业务对象的位置,那么您可以在使用后立即关闭数据库。据我所知,它的设计目的是让您只需关闭它并在需要时请求一个新的。

但在使用游标时要小心,只要使用游标,就必须保持数据库打开。否则,当游标必须重新加载数据时,应用程序将崩溃。

It depends on what you're doing with your database. If you just do an insert, delete or select where you get an business object back, then you can close the database right after using it. As far as I know it is designed that you simply close it and request a new one when ever you need it.

But be careful when you're working with a cursor then you have to keep the database open as long as the cursor is in use. Otherwise the application will crash when the cursor has to reload data.

不可一世的女人 2024-10-28 13:38:04

我想你应该关闭它,例如在使用它的活动的 onDestroy() 中。

因此,在我的 DBAdapter 类中,我有:

/**
 * Close the database
 */
 public void close() {
     mDb.close();   //mDb was obtained using mDbHelper.getWritableDatabase();
 }

在我的活动中:

 public void onCreate(Bundle bundle){
     ...
     mDBAdapter = new DBAdapter(this);
    // Open or create the database
    mDBAdapter.open();
 }    

@Override
public void onDestroy() {
    // Close the database
    mDBAdapter.close();
    super.onDestroy();        
}

不确定这是否适合您的提供程序概念。

I guess you should close it, for example in onDestroy() of an activity that is using it.

So in my DBAdapter class I have:

/**
 * Close the database
 */
 public void close() {
     mDb.close();   //mDb was obtained using mDbHelper.getWritableDatabase();
 }

And in my activity:

 public void onCreate(Bundle bundle){
     ...
     mDBAdapter = new DBAdapter(this);
    // Open or create the database
    mDBAdapter.open();
 }    

@Override
public void onDestroy() {
    // Close the database
    mDBAdapter.close();
    super.onDestroy();        
}

Not sure if this is suitable for your provider concept.

泪之魂 2024-10-28 13:38:04

如果您在 API 中检查该对象的使用示例在 Android 中,您可以看到该对象刚刚被使用,但不需要关闭。

他们虽然实现了 close() 方法,但我还没有看到他们使用它。

If you check the example of use for that object in the API of Android, you can see the object is just used, but no close is necesary.

They implement the method close() though, but I havent seen they use it.

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