Android数据库连接最佳实践

发布于 2024-11-09 06:34:42 字数 1237 浏览 6 评论 0原文

什么被认为是处理数据库连接的最佳实践? (我省略了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 技术交流群。

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

发布评论

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

评论(1

小…红帽 2024-11-16 06:34:42

我曾经担心这一切,但最近我开始使用 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.

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