我想创建一个使用在Android手机上的本地数据库源码
。
我有一个辅助类,如下图所示,用于创建数据库并提供帮助。
我想在我的code的主要部分创建这个类的一个对象,它创建的数据库和表有作为。
的问题:
每当我创建一个类的对象时,它似乎并没有被要求在助手的的onCreate
方法。我认为这是应该发生。我认为,的onCreate
基本上是一个类构造函数。 (我相信我错了)
的onCreate
的方法?
或者我如何得到它来创建数据库和表在那里我创建的类的对象呢?
在什么将是一个更好的方式来做到这一点,这是不是一个好方法?
公共类SmartDBHelper扩展SQLiteOpenHelper {
私有静态最后弦乐DATABASE_NAME =smart_lite_db.db;
私有静态最终诠释DATABASE_VERSION = 2;
私有静态最后弦乐NOTIFY_TABLE_NAME =user_notify_data;
私有静态最后弦乐HR_TABLE_NAME =user_hr_data;
私有静态最后弦乐NOTIFY_TABLE_CREATE =
CREATE TABLE+ NOTIFY_TABLE_NAME +
(计数器INTEGER PRIMARY KEY,+
userresponse INTEGER,+
notifytime整数);
私有静态最后弦乐DATA_TABLE_CREATE =
CREATE TABLE+ HR_TABLE_NAME +
(计数器INTEGER PRIMARY KEY,+
HR INTEGER,+
行动INTEGER,+
时间戳整数);
公共SmartDBHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
// TODO自动生成构造函数存根
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
// TODO自动生成方法存根
Log.v(smartdbhelper,创世以前);
db.execSQL(NOTIFY_TABLE_CREATE);
Log.v(smartdbhelper,中创建);
db.execSQL(DATA_TABLE_CREATE);
Log.v(smartdbhelper,创建后);
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
// TODO自动生成方法存根
}
}
公共类SmartApp扩展活动实现OnShared preferenceChangeListener {
私人SmartDBHelper dBHelper;
公共无效的onCreate(包savedInstanceState){
//我在哪里想创建数据库和表
dBHelper =新SmartDBHelper(的getContext());
}
}
解决方案
在的onCreate
不是构造数据库类。如果你试图打开一个不存在的数据库它只调用。
要打开/创建数据库,你需要添加一个调用这些方法之一:
公共类SmartApp扩展活动实现OnShared preferenceChangeListener {
私人SmartDBHelper dBHelper;
公共无效的onCreate(包savedInstanceState){
//我在哪里想创建数据库和表
dBHelper =新SmartDBHelper(的getContext());
//打开读写
dBHelper.getWritableDatabase();
//或者只读
// dBHelper.getReadableDatabase();
}
}
这是一个有点大,但这里是我的DatabaseAdapter你可以看看:http://saintfeintcity.org/projects/sfc/repository/entry/trunk/src/org/saintfeintcity/database/GameDbAdapter.java
I am trying to create a local database on an android phone using sqlite
.
I have a helper class, shown below, that is used to create the database and provide the "help".
I am wanting to create an object of this class in the main part of my code and it create the database and tables there as well.
The problem:
Whenever i create an object of the class, it does not seem to be calling the onCreate
method in the helper. I thought this is supposed to happen. I thought that onCreate
was basically a constructor for the class. (I believe i am wrong)
onCreate
method?
Or how do i get it to create the database and tables where i am creating the object of the class?
What would be a better way to do this, if this is not a good approach?
public class SmartDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "smart_lite_db.db";
private static final int DATABASE_VERSION = 2;
private static final String NOTIFY_TABLE_NAME = "user_notify_data";
private static final String HR_TABLE_NAME = "user_hr_data";
private static final String NOTIFY_TABLE_CREATE =
"CREATE TABLE " + NOTIFY_TABLE_NAME +
" (counter INTEGER PRIMARY KEY, " +
"userresponse INTEGER, " +
"notifytime INTEGER);";
private static final String DATA_TABLE_CREATE =
"CREATE TABLE " + HR_TABLE_NAME +
" (counter INTEGER PRIMARY KEY, " +
"hr INTEGER, " +
"act INTEGER, " +
"timestamp INTEGER);";
public SmartDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.v("smartdbhelper", "before creation");
db.execSQL(NOTIFY_TABLE_CREATE);
Log.v("smartdbhelper", "middle creation");
db.execSQL(DATA_TABLE_CREATE);
Log.v("smartdbhelper", "after creation");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {
private SmartDBHelper dBHelper;
public void onCreate(Bundle savedInstanceState) {
//where i am wanting to create the database and tables
dBHelper = new SmartDBHelper(getContext());
}
}
解决方案
The onCreate
is not a constructor for the database class. It is only called if you try to open a database that does not exist.
To open/create the database you need to add a call to one of these methods:
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {
private SmartDBHelper dBHelper;
public void onCreate(Bundle savedInstanceState) {
//where i am wanting to create the database and tables
dBHelper = new SmartDBHelper(getContext());
// open to read and write
dBHelper.getWritableDatabase();
// or to read only
// dBHelper.getReadableDatabase();
}
}
It is a bit big but here is my DatabaseAdapter you can take a look at: http://saintfeintcity.org/projects/sfc/repository/entry/trunk/src/org/saintfeintcity/database/GameDbAdapter.java