android - 使用适配器、助手创建或访问数据库有什么好处?

标签 android database sqlite

目前我正在使用以下代码在 android 中创建 SQLite 数据库:

    SQLiteDatabase db;
try{
            SQLiteDatabase  dbe = SQLiteDatabase.openDatabase("/data/data/bangla.rana.fahim/databases/dictionary", null,0);
            dbe.close();
   }
            catch(SQLiteException e){

                db = openOrCreateDatabase("dictionary", MODE_PRIVATE, null);
                db.execSQL("CREATE TABLE IF NOT EXISTS LIST(wlist varchar,ex varchar);");                
                db.close();
            }

并使用游标检索数据,如:

  Cursor cc = db.rawQuery(q, null);//q is the desired query.

但是我已经看到很多使用助手和适配器来达到相同目的的例子。那么,我的问题是使用它们有什么好处?

最佳答案

我猜你指的是 SQLiteOpenHelper,文档非常清楚:

A helper class to manage database creation and version management.

You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.

有了它,您不必手动创建您的数据库并负责升级它。您只需使用数据库逻辑实现 onCreate()onUpgrade() 这两个方法,android 将确保在需要时调用这些方法。当你说适配器时,我不知道你指的是什么。

关于android - 使用适配器、助手创建或访问数据库有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9655217/

相关文章:

android搜索栏定制,

android - 如何以友好的名称获取绑定(bind)的蓝牙设备列表

android - 插入子实体后,GreenDAO 不会更新实体的子树

android - 在 Spinner View Android 中显示最后一项

c++ - 是否有支持大尺寸多维搜索的现有数据库(首选嵌入式数据库)?

mysql - 三个表之间的 SQL 查询 - 哪个连接?

android - 如何在我的 Android 应用程序中加入两个 SQLite 表?

sqlite - 如何在 Titanium 中使用 SQLite 加载 ListView?

java - 尝试从数据库(android)获取数据时内存耗尽

内容提供者中的Android自定义方法获取表中的记录数?