关于数据库助手
我的项目中有一个数据库助手类
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
Log.w(TAG, "Upgrading database from version " + oldVersion + "to" + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
,我需要创建这个 DatabaseHelper 的新对象,例如 DatabaseHelper aa = new DatabaseHelper(context)。如果这将执行 db.execSQL(DATABASE_CREATE);每次我的程序启动时?该行用于创建一个新数据库;
I have a database helper class
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
Log.w(TAG, "Upgrading database from version " + oldVersion + "to" + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
on my project, I need to create a new object of this DatabaseHelper like DatabaseHelper aa = new DatabaseHelper(context). If that will execute db.execSQL(DATABASE_CREATE); every time when my program start? This line is for creating a new database;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果数据库不存在,它将在创建DatabaseHelper期间运行onCreate;如果数据库存在并且是使用较低的DATABASE_VERSION创建的,它将运行onUpgrade
It will run onCreate during the creation of the DatabaseHelper if the database does not exists, and it will run onUpgrade if the database exists and was created with a lower DATABASE_VERSION
不,它不会每次都被调用。关于打开帮助程序和您发布的代码,您需要了解两件事:
OnCreate 只会调用一次(首次创建数据库时)。然后(只要您的数据库存在)该代码将永远不会再次被调用。
因为您在 onUpgrade 中手动调用 onCreate() (我不建议这样做):
在您的超级构造函数中,您解析数据库版本。该版本由助手存储,只要该版本保持不变,就不会调用 onUpgrade。
2.
No it will not be called every time. About the open helper and the code you postet you need to know 2 things:
OnCreate will only called once (when your database is first created). Then (as long as your database exists) this code will never be called again.
since you call manually onCreate() in your onUpgrade (which I wouldnt recommend):
In your super constructor you parse a Database version. This version is stored by the helper and as long this version stays same the onUpgrade will not be called.
2.
定义数据库适配器的正确方法如下所示。我在很多项目中都使用它。
用法是:
和DB Adapter类:
<前><代码>包....
/**
* 内部数据库适配器
* @作者亚尔
*/
公共类 DBAdapter {
私有静态最终字符串 DATABASE_NAME = "....";
私有静态最终 int DATABASE_VERSION = 1;
私有静态最终字符串TABLE_SOMETHING =“something_tab”;
// 列名称定义:
公共静态最终字符串COL_ID =“id”; //钥匙
公共静态最终字符串COL_NAME =“名称”;
私有 SQLiteDatabase mDb;
私有 DBOpenHelper mDbHelper;
公共 DBAdapter(上下文 pContext){
mDbHelper = new DBOpenHelper(pContext, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* 关闭数据库
*/
公共无效关闭(){
mDb.close();
}
/**
* 打开数据库,如果没有找到则创建
* @抛出SQLiteException
*/
公共无效开放()抛出SQLiteException {
mDb = mDbHelper.getWritableDatabase();
}
......从数据库获取/设置数据的函数
/**
* 数据库辅助类
* @作者亚尔
*/
私有静态类 DBOpenHelper 扩展 SQLiteOpenHelper {
公共DBOpenHelper(上下文上下文,字符串名称,CursorFactory工厂,int版本){
super(上下文、名称、工厂、版本);
}
// 创建新数据库的 SQL 语句。
私有静态最终字符串TABLE_SOMETHING_CREATE =
"创建表"+TABLE_SOMETHING+"("+COL_ID+"整数主键自增,"+
COL_NAME+" 文本不为空); ";
@覆盖
公共无效onCreate(SQLiteDatabase pDb){
pDb.execSQL(TABLE_SOMETHING_CREATE);
}
@覆盖
公共无效onUpgrade(SQLiteDatabase pDb,int pOldVersion,int pNewVersion){
// 删除旧表。
pDb.execSQL("如果存在则删除表" + TABLE_SOMETHING);
// 创建一个新的。
onCreate(pDb);
}
}
}
The proper way of defining DB adapter is like below. I use it in many projects.
The usage is:
and the DB Adapter class:
}