目前我正在使用以下代码在 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/