且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQLiteOpenHelper不能打电话的onCreate?

更新时间:2023-11-19 13:52:58

的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)

  • So, why is it not calling the 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