SQLiteOpenHelper 无法调用 onCreate?

发布于 2024-10-17 16:10:52 字数 2148 浏览 8 评论 0原文

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

我有一个助手类,如下所示,用于创建数据库并提供“帮助”。

我想在代码的主要部分创建此类的对象,并在那里创建数据库和表。

问题:

每当我创建该类的对象时,它似乎都没有调用帮助器中的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

    }

}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乖乖哒 2024-10-24 16:10:52

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

它有点大,但这是我的 DatabaseAdapter,您可以看一下:http://saintfeintcity.org/projects/sfc/repository/entry/trunk/src/org/saintfeintcity/数据库/GameDbAdapter.java

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

演出会有结束 2024-10-24 16:10:52

您必须调用 getWritableDatabase()getReadableDatabase()

You have to call getWritableDatabase() or getReadableDatabase().

┼── 2024-10-24 16:10:52

oncreate() 方法不是构造函数,也是在第一次创建数据库时调用。所以你必须调用 getWritableDatabase()getReadableDatabase() 用于打开或创建您的数据库。

getReadableDatabase() :- 创建和/或打开数据库。

getWritableDatabase() :- 创建和/或打开一个数据库,该数据库将
用于阅读和写作。

oncreate() method is not a constructor and also Called when the database is created for the first time.So you have to call getWritableDatabase() or getReadableDatabase() for open or create to your database.

getReadableDatabase() :- Create and/or open a database.

getWritableDatabase() :- Create and/or open a database that will be
used for reading and writing.

温柔戏命师 2024-10-24 16:10:52

未调用 SQLiteOpenHelper.onCreate() 的原因之一是设备上已存在数据库文件。

如果您转到 View >工具窗口>在Android Studio中的App Inspection中,您可以转到Database Inspector并查看设备上的SQLite文件。

如果由于已有具有该名称的现有数据库而未调用 onCreate,则您可以将应用程序中的数据库名称更改为新名称。那么数据库文件将不存在,因此 onCreate 将被调用。

static class MyDatabaseHelper extends SQLiteOpenHelper {
    private DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION); // A new DB_NAME here means onCreate() will get called.
        mDatabaseHelper = this;
    }
    ....
    ....
}

如果上面的 DB_NAME 以前从未使用过,则 onCreate() 应该运行,但如果您尝试重复使用名称,则它已经存在,并且 onCreate() 将不会运行。

我发现最简单的方法就是在迭代应用开发时不断创建新的 DB_NAME

One reason SQLiteOpenHelper.onCreate() doesn't get called is because the database file already exists on the device.

If you go to View > Tool Windows > App Inspection in Android Studio, you can go to Database Inspector and see the SQLite files on the device.

In the case where onCreate is not being called because there's an existing database with that name already, you can change the database name in your app to something new. Then database file won't exist, and so onCreate will get called.

static class MyDatabaseHelper extends SQLiteOpenHelper {
    private DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION); // A new DB_NAME here means onCreate() will get called.
        mDatabaseHelper = this;
    }
    ....
    ....
}

If the above DB_NAME is something never used before, onCreate() should run, but if you're trying to re-use a name, it already exists, and onCreate() won't run.

I found the easiest thing to do is just keep making new DB_NAMEs as you're iterating through app development.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文