我已经在我的SQLiteOpenHelper onCreate()中创建了我的表
SQLiteException: no such table
or
SQLiteException: no such column
错误。为什么?
注意: (这是每周数十个类似问题的汇总。试图在这里提供一个“规范的”社区wiki问题/答案,以便所有这些问题都可以指向一个很好的参考。)
我已经在我的SQLiteOpenHelper onCreate()中创建了我的表
SQLiteException: no such table
or
SQLiteException: no such column
错误。为什么?
注意: (这是每周数十个类似问题的汇总。试图在这里提供一个“规范的”社区wiki问题/答案,以便所有这些问题都可以指向一个很好的参考。)
当前回答
在我的例子中,我从XML-file中获取带有<string-array>的项,其中存储<item>s。在这些<item>s我持有SQL字符串和应用databaseBuilder.addMigrations(迁移)一个接一个。我犯了一个错误,忘记在引用前添加\,得到了异常:
android.database.sqlite.SQLiteException:没有这样的列:some_value(代码1 SQLITE_ERROR):,而编译:INSERT INTO table_name(id, name) VALUES(1, some_value)
所以,这是一个正确的变体:
<item>
INSERT INTO table_name(id, name) VALUES(1, \"some_value\")
</item>
其他回答
当需要创建表时,第一次调用onCreate。我们需要重写这个方法,在这里我们编写了由SQLiteDatabase执行的表创建脚本。execSQL方法。在第一次部署中执行后,此方法将不再被调用。
onUpgrade 当数据库版本升级时调用此方法。假设第一次部署时,数据库版本为1,在第二次部署时,数据库结构发生了变化,例如在表中增加了额外的列。假设数据库版本现在是2。
onCreate ()
When we create DataBase at a first time (i.e Database is not exists) onCreate() create database with version which is passed in SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) onCreate() method is creating the tables you’ve defined and executing any other code you’ve written. However, this method will only be called if the SQLite file is missing in your app’s data directory (/data/data/your.apps.classpath/databases). This method will not be called if you’ve changed your code and relaunched in the emulator. If you want onCreate() to run you need to use adb to delete the SQLite database file.
onUpgrade()
SQLiteOpenHelper应该调用超级构造函数。 onUpgrade()方法只会在版本整数大于应用程序中运行的当前版本时被调用。 如果希望调用onUpgrade()方法,则需要增加代码中的版本号。
Sqliteopenhelper的方法有create方法和upgrade方法,create方法在任何表第一次创建时使用,而upgrade方法将在每次表的列数发生变化时调用。
数据库名必须以.db结尾,查询字符串必须有结束符(;)
在DatabaseHandler/DatabaseManager类中重新检查您的查询(您曾经使用过的类)