gpt4 book ai didi

android - `.db` 文件在调用 Room.createFromAsset ("initialData.db"时应该采用什么格式)?

转载 作者:行者123 更新时间:2023-12-05 00:12:18 24 4
gpt4 key购买 nike

场景

我正在尝试用一些条目预填充 Room 数据库。 api 调用的文档很清楚 ( developer.android docs ),我读过一篇很好的博客 ( Medium article ),但我不熟悉实际预打包数据库文件 (developmentData.txt) 的语法/格式。 db 在下面的例子中)。我在任何地方都找不到任何文档。

Room
.databaseBuilder(
context.applicationContext,
MyDatabase::class.java,
"my_database")
.fallbackToDestructiveMigration()
.createFromAsset("database/developmentData.db")
.build()

想法

我关注了 documentation用于导出架构。以下是该过程的结果,供引用。在这种情况下,我只是为了在这里提出我的问题而创建一个虚拟对象。

{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "0ad43a6714902eedbb90c1f77ab1ffcb",
"entities": [
{
"tableName": "library_table",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`libraryId` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT NOT NULL, `description` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "libraryId",
"columnName": "libraryId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "description",
"columnName": "description",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"libraryId"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"0ad43a6714902eedbb90c1f77ab1ffcb\")"
]
}
}

这是我的 developmentData.db 的当前内容,但它没有通过验证:

CREATE TABLE IF NOT EXISTS `library_table` (
`libraryId` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`title` TEXT NOT NULL,
`description` TEXT NOT NULL
);
INSERT INTO `library_table` (title, description) VALUES ("Library 1", "A whopping amazing library!");
INSERT INTO `library_table` (title, description) VALUES ("Library the Second", "A smashing library indeed!");
INSERT INTO `library_table` (title, description) VALUES ("Thou Third Library", "Quite possibly the most amazing library ever.");
INSERT INTO `library_table` (title, description) VALUES ("Ye Olde Fouthe Librarye", "So many profound thoughts in here.");

这里是错误。它没有加密,所以这一定是语法问题。E/SQLiteLog: (26) 文件已加密或不是数据库

问题

调用 createFromAsset() 时引用的 .db 文件应该采用什么格式?如果某处有现有文档,我很乐意提供引用。

最佳答案

这里的解决方案是创建一个实际的 SQLite 数据库二进制文件,而不是我上面的一系列 SQL 语句,然后使用该文件。从文档中看不出创建它的过程,因此我将在此处进行解释,以防其他人需要指出正确的方向。 @CommonsWare 应该为这个答案赢得荣誉,因为他为我指明了正确的方向。

在应用程序中导出 Room 数据库的架构。

此步骤有很好的文档 here .简而言之,我需要将此注释添加到我的 RoomDatabase 类中。注意 exportSchema = true部分。这会导致 Gradle 构建在 app/schemas 中删除一个 .json 文件目录。

@Database(entities = [Library::class], version = 1, exportSchema = true)
abstract class MyDatabase : RoomDatabase() {

该 json 文件包含 tableNamecreateSql我在下一步中使用的键值对。它看起来像这样:

"tableName": "library_table",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`libraryId` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT NOT NULL, `description` TEXT NOT NULL)",

创建 SQLite 二进制文件

然后我使用了一个名为 DB Browser for SQLite 的免费应用程序引用上面提到的 .json 实际创建我需要的 .db 二进制文件。在我向其中添加几条记录并保存后,数据就会毫无问题地预填充。

将二进制文件放在正确的目录中

最后一点,我没有从文档中立即发现这一点:对 createFromAsset() 的调用默认情况下,看起来在 /assets 中目录。该目录需要位于所需的源集内。因此,例如,我将我的 .db 二进制文件放在这里:
<project-root>/app/src/main/assets/initialData.db

关于android - `.db` 文件在调用 Room.createFromAsset ("initialData.db"时应该采用什么格式)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61778041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com