pro*_*007 29 java sqlite android oncreate
我正在尝试使用Android手机创建本地数据库sqlite.
我有一个帮助类,如下所示,用于创建数据库并提供"帮助".
我想在我的代码的主要部分创建这个类的对象,它也在那里创建数据库和表.
问题:
每当我创建类的对象时,它似乎都没有调用onCreate帮助器中的方法.我以为这应该发生.我认为这onCreate基本上是班级的构造函数.(我相信我错了)
onCreate方法呢?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
}
}
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
War*_*ith 68
该onCreate不是数据库类的构造函数.只有在尝试打开不存在的数据库时才会调用它.要打开/创建数据库,您需要添加对这些方法之一的调用:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
它有点大,但是我的DatabaseAdapter你可以查看:http://saintfeintcity.org/projects/sfc/repository/entry/trunk/src/org/saintfeintcity/database/GameDbAdapter.java