我已经在我的SQLiteOpenHelper onCreate()中创建了我的表

SQLiteException: no such table

or

SQLiteException: no such column

错误。为什么?

注意: (这是每周数十个类似问题的汇总。试图在这里提供一个“规范的”社区wiki问题/答案,以便所有这些问题都可以指向一个很好的参考。)


当前回答

Sqlite数据库覆盖两种方法

1) onCreate (): 该方法仅在应用程序第一次启动时调用一次。所以它只调用一次

2) onUpgrade () 当我们改变数据库版本时,这个方法被调用,然后这个方法被调用。它用于改变表结构,如在创建DB Schema后添加新列

其他回答

扩展SQLiteOpenHelper时要记住的要点

super(context, DBName, null, DBversion); - This should be invoked first line of constructor override onCreate and onUpgrade (if needed) onCreate will be invoked only when getWritableDatabase() or getReadableDatabase() is executed. And this will only invoked once when a DBName specified in the first step is not available. You can add create table query on onCreate method Whenever you want to add new table just change DBversion and do the queries in onUpgrade table or simply uninstall then install the app.

如果你忘记提供一个“name”字符串作为构造函数的第二个参数,它会创建一个“内存中”数据库,当你关闭应用程序时,这个数据库会被擦除。

从模拟器或设备卸载应用程序。再次运行应用程序。(OnCreate()不执行时,数据库已经存在)

数据库名必须以.db结尾,查询字符串必须有结束符(;)

Sqliteopenhelper的方法有create方法和upgrade方法,create方法在任何表第一次创建时使用,而upgrade方法将在每次表的列数发生变化时调用。