SQLiteException:没有这样的表:数据库注释(代码 1 SQLITE_ERROR)
我正在尝试迁移新的数据库版本。唯一改变的是添加了一个列。我总是收到以下错误:
android.database.sqlite.SQLiteException: no such table: database-notes (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE 'database-notes' ADD COLUMN image TEXT
我不明白为什么会收到此异常,因为我的表名为 database-notes
,如 .build() 调用中所写。
这是我的数据库类:
@Database(
version = 2,
entities = [Note::class],
exportSchema = true)
abstract class AppDatabase : RoomDatabase() {
abstract fun noteDao(): NoteDAO
companion object {
fun build(context: Context) = Room.databaseBuilder(context, AppDatabase::class.java, "database-notes")
.addMigrations(MIGRATION_1_2).build()
}
}
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE 'database-notes' ADD COLUMN image TEXT")
}
}
数据库名称与以前的版本完全相同。我复制了它以排除拼写错误。 我在这里忽略了什么?先感谢您!
I'm trying to migrate a new database version. The only thing changed is an added column. I always get the following error:
android.database.sqlite.SQLiteException: no such table: database-notes (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE 'database-notes' ADD COLUMN image TEXT
I don't understand why I get this exception, because my table is named database-notes
as written in the .build() call.
This is my database class:
@Database(
version = 2,
entities = [Note::class],
exportSchema = true)
abstract class AppDatabase : RoomDatabase() {
abstract fun noteDao(): NoteDAO
companion object {
fun build(context: Context) = Room.databaseBuilder(context, AppDatabase::class.java, "database-notes")
.addMigrations(MIGRATION_1_2).build()
}
}
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE 'database-notes' ADD COLUMN image TEXT")
}
}
The database name was exactly the same in the previous version. I copied it to rule out typos.
What have I overlooked here? Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于失败,它看起来不会,并且可能是对数据库名称和表名称之间的差异的误解。
一个数据库可以有多个表。数据库名称是文件本身的名称(表、索引、视图和触发器等组件的容器)。
在您的代码database-notes中,Room.databaseBuilder 的第三个参数是数据库(文件)的名称。
对于 Room,表名称是从使用 @Entity 注释并通过 @Database 注释的实体参数提供的类派生的。在您的情况下,Note 类。
表的名称将为 Note,除非您使用
@Entity
注释的tableName =
参数来提供其他名称。示例
如果以下是您的 Note 类:-
那么表名称将为 Note (类的名称)
如果 Note 类是:-
表名称将为 notes (由
@Entity
注释的tableName =
参数指定)。It would appear not, due to the failure, and is probably a misunderstanding of the difference between the database name and table name(s).
A Database can have multiple tables. The database name is the name of the file itself (the container of the components such as tables, indexes, views and triggers).
In your code database-notes, as per the 3rd parameter to the Room.databaseBuilder is the name of the database (the file).
With Room the table names are derived from the classes that are both annotated with @Entity and provided, via the entities parameter of the @Database annotation. In your case the Note class.
The name of the table will be Note unless you use the
tableName =
parameter of the@Entity
annotation to provide another name.Example
If the following were your Note class :-
Then the table name would be Note (the name of the class)
If the Note class were :-
The the table name would be notes (as specified by the
tableName =
parameter of the@Entity
annotation).