SQLiteOpenHelper 无法调用 onCreate? [英] SQLiteOpenHelper failing to call onCreate?

查看:45
本文介绍了SQLiteOpenHelper 无法调用 onCreate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sqlite 在 android 手机上创建本地数据库.

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.

问题:

每当我创建类的对象时,它似乎都没有在帮助器中调用 onCreate 方法.我以为这是应该发生的.我认为 onCreate 基本上是类的构造函数.(我相信我错了)

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 方法?
  • 或者我如何让它在我创建类的对象的地方创建数据库和表?
  • 如果这不是一个好方法,什么是更好的方法?
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());
   }
}

推荐答案

onCreate 不是数据库类的构造函数.仅当您尝试打开不存在的数据库时才会调用它.要打开/创建数据库,您需要添加对以下方法之一的调用:

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();
   }
}

它有点大,但这是我的 DatabaseAdapter,你可以看看:http://saintfeintcity.org/projects/sfc/repository/entry/trunk/src/org/saintfeintcity/database/GameDbAdapter.java

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

这篇关于SQLiteOpenHelper 无法调用 onCreate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆