在android中打开和关闭数据库连接?
我搜索了很多问题,但没有得到正确答案。 在活动生命周期中打开和关闭数据库的最佳方式是什么。 请有人帮我回答正确的问题在android中打开和关闭数据库连接?,android,sqlite,Android,Sqlite,我搜索了很多问题,但没有得到正确答案。 在活动生命周期中打开和关闭数据库的最佳方式是什么。 请有人帮我回答正确的问题 提前感谢。您可以像这样打开数据库 public void openDataBase() throws SQLException { String myPath = DB_PATH + DB_NAME; myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQL
提前感谢。您可以像这样打开数据库
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
关闭数据库
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
//you need to extend the class with SQLiteOpenHelper
super.close();
}
您可以像这样打开数据库
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
关闭数据库
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
//you need to extend the class with SQLiteOpenHelper
super.close();
}
使用单例模式并使用
db=DatabaseHelper.getInstance(context)
访问。
它保证在整个应用程序生命周期中只存在一个数据库助手
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper sInstance;
private static final String DATABASE_NAME = "database_name";
private static final String DATABASE_TABLE = "table_name";
private static final int DATABASE_VERSION = 1;
public static synchronized DatabaseHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}
return sInstance;
}
/**
* Constructor should be private to prevent direct instantiation.
* make call to static method "getInstance()" instead.
*/
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
并使用以下方式访问:
db=DatabaseHelper.getInstance(this);
如果需要,还可以在catch块中关闭数据库连接。
我希望它能有所帮助。使用单例模式,并使用
db=DatabaseHelper.getInstance(context)
进行访问。
它保证在整个应用程序生命周期中只存在一个数据库助手
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper sInstance;
private static final String DATABASE_NAME = "database_name";
private static final String DATABASE_TABLE = "table_name";
private static final int DATABASE_VERSION = 1;
public static synchronized DatabaseHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}
return sInstance;
}
/**
* Constructor should be private to prevent direct instantiation.
* make call to static method "getInstance()" instead.
*/
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
并使用以下方式访问:
db=DatabaseHelper.getInstance(this);
如果需要,还可以在catch块中关闭数据库连接。
我希望它能有所帮助。为此使用单例模式,这样您就不需要在活动生命周期中处理它。为此使用单例模式,这样您就不需要在活动生命周期中处理它。虽然这个答案很好,但请向用户解释应在何时打开和关闭连接。据我所知,用户希望在应用程序中使用数据库,并希望知道何时开始连接,何时结束连接。例如,数据库应该在“MainActivity”中打开。
onCreate()
,然后在“MainActivity”中关闭。onDestroy
,这样,数据库就可以在整个堆栈中使用,同时在单个点中进行管理。否则,它也可以在ApplicationContext中完成,但关闭会更加困难(不知道应用程序)。是的,正确的用户可以在onCreate()中打开,并在使用后或在onStop中关闭。但是请检查这个答案。根据这篇文章,保持数据库连接打开没有什么错。因此,在应用程序中处理单个db实例是一种更好的方法。虽然这个答案很好,但请向用户解释应该在何时打开和关闭连接。据我所知,用户希望在应用程序中使用数据库,并希望知道何时开始连接,何时结束连接。例如,数据库应该在“MainActivity”中打开。onCreate()
,然后在“MainActivity”中关闭。onDestroy
,这样,数据库就可以在整个堆栈中使用,同时在单个点中进行管理。否则,它也可以在ApplicationContext中完成,但关闭会更加困难(不知道应用程序)。是的,正确的用户可以在onCreate()中打开,并在使用后或在onStop中关闭。但是请检查这个答案。根据这篇文章,保持数据库连接打开没有什么错。因此,在应用程序中处理单个db实例是一种更好的方法。