安卓的SQLite数据库何时关闭数据库、SQLite

2023-09-11 20:26:10 作者:温柔半两

我对Android的SQLite数据库的工作。我的数据库管理器是一个独立的,并马上打开到数据库的连接时,它被初始化。它是安全的,使数据库打开的整段时间,这样,当有人叫我的班级与它已经打开数据库的工作?或者我应该开在每次访问后,需要关闭该数据库。有没有在刚离开它打开的全部时间任何伤害?

I am working with a SQLite database on android. My database manager is a singleton and right now opens a connection to the database when it is initialized. It is safe to leave the database open the entire time so that when someone calls my class to work with the database it is already open? Or should I open and close the database before and after each access is needed. Is there any harm in just leaving it open the whole time?

谢谢!

推荐答案

我会保持打开状态的全部时间,并且在一些生命周期的方法将其关闭,如的onStop 的onDestroy 。这样一来,你可以很容易地检查数据库已在使用通过调用 isDbLockedByCurrentThread isDbLockedByOtherThreads 上的单个 SQLiteDatabase 对象。这将prevent多个操作到数据库并保存您的应用程序从一个潜在的崩溃

i would keep it open the whole time, and close it in some lifecycle method such as onStop or onDestroy. that way, you can easily check if the database is already in use by calling isDbLockedByCurrentThread or isDbLockedByOtherThreads on the single SQLiteDatabase object every time before you use it. this will prevent multiple manipulations to the database and save your application from a potential crash

因此​​,在你单身,你可能有这样的方法,让您的单一 SQLiteOpenHelper 目标:

so in your singleton, you might have a method like this to get your single SQLiteOpenHelper object:

private SQLiteDatabase db;
private MyDBOpenHelper mySingletonHelperField;
public MyDBOpenHelper getDbHelper() {
    db = mySingletonHelperField.getDatabase();//returns the already created database object in my MyDBOpenHelper class(which extends `SQLiteOpenHelper`)
    while(db.isDbLockedByCurrentThread() || db.isDbLockedByOtherThreads()) {
        //db is locked, keep looping
    }
    return mySingletonHelperField;
}

所以,只要你想用你打开的帮助对象,调用该getter方法​​(确保它的线程)

so whenever you want to use your open helper object, call this getter method(make sure it's threaded)

在你的单身另一种方法可以(被称为EVERY TIME,然后再尝试拨打上面的​​吸气剂):

another method in your singleton may be(called EVERY TIME before you try to call the getter above):

public void setDbHelper(MyDBOpenHelper mySingletonHelperField) {
    if(null == this.mySingletonHelperField) {
        this.mySingletonHelperField = mySingletonHelperField;
        this.mySingletonHelperField.setDb(this.mySingletonHelperField.getWritableDatabase());//creates and sets the database object in the MyDBOpenHelper class
    }
}

您可能希望在单身关闭数据库,以及:

you may want to close the database in the singleton as well:

public void finalize() throws Throwable {
    if(null != mySingletonHelperField)
        mySingletonHelperField.close();
    if(null != db)
        db.close();
    super.finalize();
}

如果您的应用程序的用户必须非常快速地创造出许多数据库交互的能力,你应该使用类似我在上面展示。但如果有最小的数据库交互,我就不会担心它,只是创建每次关闭数据库。

if the users of your application have the ability to create many database interactions very quickly, you should use something like i have demonstrated above. but if there is minimal database interactions, i wouldn't worry about it, and just create and close the database every time.