SQLiteException:没有这样的表:数据库注释(代码 1 SQLITE_ERROR)

发布于 2025-01-16 00:23:55 字数 926 浏览 4 评论 0原文

我正在尝试迁移新的数据库版本。唯一改变的是添加了一个列。我总是收到以下错误:

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 技术交流群。

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

发布评论

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

评论(1

深爱成瘾 2025-01-23 00:23:55

因为我的表名为database-notes

由于失败,它看起来不会,并且可能是对数据库名称和表名称之间的差异的误解。

一个数据库可以有多个表。数据库名称是文件本身的名称(表、索引、视图和触发器等组件的容器)。

在您的代码database-notes中,Room.databaseBuilder 的第三个参数是数据库(文件)的名称。

对于 Room,表名称是从使用 @Entity 注释并通过 @Database 注释的实体参数提供的类派生的。在您的情况下,Note 类。

表的名称将为 Note,除非您使用 @Entity 注释的 tableName = 参数来提供其他名称。

示例

如果以下是您的 Note 类:-

@Entity // No tableName parameter so the table name is the class name
data class Note(
    @PrimaryKey
    var noteId: Long? = null,
    var noteText: String,
    var image: String
)

那么表名称将为 Note (类的名称)

如果 Note 类是:-

@Entity(tableName = "notes") //<<<<< specifically names the table
data class Note(
    @PrimaryKey
    var noteId: Long? = null,
    var noteText: String,
    var image: String
)

表名称将为 notes (由 @Entity 注释的 tableName = 参数指定)。

because my table is named database-notes

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 :-

@Entity // No tableName parameter so the table name is the class name
data class Note(
    @PrimaryKey
    var noteId: Long? = null,
    var noteText: String,
    var image: String
)

Then the table name would be Note (the name of the class)

If the Note class were :-

@Entity(tableName = "notes") //<<<<< specifically names the table
data class Note(
    @PrimaryKey
    var noteId: Long? = null,
    var noteText: String,
    var image: String
)

The the table name would be notes (as specified by the tableName = parameter of the @Entity annotation).

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