Android数据库连接最佳实践
什么被认为是处理数据库连接的最佳实践? (我省略了DatabaseHelper类中的构造函数和onUpgrade方法)这些只是我在互联网上找到的两种方法,也许您有更好的处理方法?我很想听。
选项 1
public class DatabaseManager {
private SQLiteDatabase mDb;
public DatabaseManager(Context context) {
DatabaseHelper helper = new DatabaseHelper(context);
helper.getWritableDatabase();
}
// ... methods that use mDb
private class DatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
mDb = db;
//create database
}
@Override
public void onOpen(SQLiteDatabase db) {
mDb = db;
}
}
}
选项 2
public class DatabaseManager {
private DatabaseHelper mDbHelper;
public DatabaseManager(Context context) {
mDbHelper = new DatabaseHelper(context);
}
// ... methods that fetch the db
private void sampleMethod() {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
//do stuff with database
mDbHelper.close();
}
private static class DatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
//create database
}
}
}
另外,每次在选项 2 中使用数据库时是否都需要调用 close() ?至于使用选项1,我猜你需要在调用应用程序的onDestroy时调用close()?
What is considered to be best practice for handling database connections? (I omitted the constructor and onUpgrade method within the DatabaseHelper class) These are just 2 ways I found on the internet, perhaps you have a better way of handling? I would love to hear.
Option 1
public class DatabaseManager {
private SQLiteDatabase mDb;
public DatabaseManager(Context context) {
DatabaseHelper helper = new DatabaseHelper(context);
helper.getWritableDatabase();
}
// ... methods that use mDb
private class DatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
mDb = db;
//create database
}
@Override
public void onOpen(SQLiteDatabase db) {
mDb = db;
}
}
}
Option 2
public class DatabaseManager {
private DatabaseHelper mDbHelper;
public DatabaseManager(Context context) {
mDbHelper = new DatabaseHelper(context);
}
// ... methods that fetch the db
private void sampleMethod() {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
//do stuff with database
mDbHelper.close();
}
private static class DatabaseHelper extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {
//create database
}
}
}
Also, is it needed to call close() everytime you used the database within option 2? As for using option 1, I guess you need to call close() when the app's onDestroy is called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我曾经担心这一切,但最近我开始使用 ORMLite。它是一个非常轻量级的 ORM,带有 Android 库,让您无需担心此类问题。
我想说这很快就会成为最佳实践,因为它在处理数据库时删除了大量重复的代码。它也定期更新,维护者对查询的响应非常快。
I used to worry about all this but recently I started using ORMLite. It is a very light ORM with Android libraries and saves you worrying about this kind of stuff.
I would say that this could very soon become best practice as it removes a lot of repeated code when dealing with databases. It is also being updated regularly and the maintainer responds to queries very quickly.