Create tables via content provider or access DBHelper directly? Other?

时间:2018-05-06 17:08:19

标签: android sqlite android-contentprovider

I am building an Android app that uses a DB helper class and content provider used only for accessing the DB. I noticed that when my app launches, it does not ever hit the onCreate() of my DBHelper class which has all of the DDL for the table creations, if they do not exist.

I have manually created one of the tables and have already confirmed that I can perform CRUD operations via my provider. My goal is to have only the DBhelper class be accessed by the content provider and have the UI usilize the content provider for CRUD operations.

I feel like I am missing part of the chair here and could use some help in understanding how this process should work such that when my app launches, the system will check if the tables exist and create when they do not exist.

I have implemented my DBhelper class as a singleton, which there seems to be much debate about.

I understand that I do need to get a readable or writable db before the create operations can proceed, at least I think that is right. The problem I am having is have not been able to piece together if I should do this by getting an instance of my db class from the onCreate of my MainActivity when the app launches? Or, if there is something I should be doing in my content provider that will handle this when the app launches? FYI - I do have my content provider registered within my manifest, as as mentioned previously, in my MainActivity onCreate(), I have successfully used my content provider for CRUD operations on a table I build manually.

Any direction here would be very much appreciated.

Here is my AppDB class

IN

1 个答案:

答案 0 :(得分:0)

  

我注意到,当我的应用程序启动时,它并没有打到   我的DBHelper类的onCreate(),它包含表的所有DDL   创作,如果它们不存在。

onCreate类的SQLIteOpenHelper方法在创建数据库时运行一次。每次实例化子类的实例时它都不会运行。

开发App时,更改数据库结构的最简单方法是执行以下操作之一(假设数据可能丢失): -

  • 从设备上的设置中删除/清除应用程序的数据,然后重新运行应用程序。
  • 卸载然后重新安装该应用程序,然后重新运行该应用程序。
  • onUpgrade方法中使用适当的代码来删除表格,然后调用更改后的onCreate方法。

如果您需要保留数据,则可以使用onUpgrade方法在ALTER TABLE的限制内使用ALTER表。

您可以通过查询 sqlite_master 表来检查表格是否存在。这将返回一个包含5列的游标,即: -

  • 类型
    • 表格表格
  • 名称实体名称
  • tbl_name 与实体相关的表的名称。
  • rootpage
  • sql 用于创建实体的SQL

  • (实体与类型相关,例如表,索引,视图,触发器)

  

....当我的应用程序启动时,系统将检查表是否存在   当它们不存在时创造。

因此,要检查tablex,您可以使用

SELECT tbl_name FROM sqlite_master WHERE tbl_name = 'tablex' AND type = 'table';

所以你可以在以下方面找到一个方法: -

public boolean doesTableExist(String table_name) {
    boolean rv = false;
    String[] columns = new String[]{"sqlite_master"};
    String whereclause = "tbl_name=? AND type=?";
    String[] whereargs = new String[]{table_name,"table"};
    Cursor csr = yoursqlitedatabase.query("sqlite_master",columns,whereclause,wwhereargs,null,null,null);
    rv = csr.getCount() > 0;
    csr.close();
    return rv;
}
  • 如果您在DBhelper中包含上述内容,那么Cursor csr = yoursqlitedatabase.query("sqlite_master",columns,whereclause,wwhereargs,null,null,null);可以是Cursor csr = this.getWritableDatabase().query("sqlite_master",columns,whereclause,wwhereargs,null,null,null);

    • 您经常会看到SQliteDatabase db = this.getWritabledatabase()然后db.query........

    • 然后您可以在AppDatabase myDBHlper;作为类变量,然后myDBHelper = Appdatabase.getInstance(this)后跟if (myDBHelper.doestableExist("your_table")) { ....... }在您要检查的类(活动)中。

当然,只需使用CREATE TABLE IF NOT EXISTS ......即可。

您可以通过覆盖作为SQLiteOpenHelper(又名DBHelper)或onOpen方法的子类的类的onConfigure方法来实现检查等。