.db文件应以哪种格式调用Room.createFromAsset("initialData.db")? [英] What format should the `.db` file take in a call to Room.createFromAsset("initialData.db")?

查看:102
本文介绍了.db文件应以哪种格式调用Room.createFromAsset("initialData.db")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一些条目预填充Room数据库. api调用上的文档非常清晰( developer.android docs ),并且我读了一个不错的博客(中型文章),但我不熟悉实际的预打包数据库文件(在下面的示例中为developmentData.db)的语法/格式.我在搜索过的任何地方都找不到任何文档.

I am attempting to pre-populate a Room database with a few entries. The documentation on the api call is clear (developer.android docs), and I've read a good blog (Medium article), but I'm unfamiliar with the syntax/format for the actual prepackaged database file (developmentData.db in the example below). I cannot find any documentation anywhere I've searched.

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

想法

我已遵循文档用于导出架构.这是该过程的结果,以供参考.在这种情况下,我只是为了在此处提出问题而创建一个虚拟对象.

Thoughts

I've followed the documentation for exporting the schema. Here is what came out of that process, for reference. In this case, I'm just creating a dummy object for the sake of asking my question here.

{
  "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的当前内容,但未通过验证:

Here are the current contents of my developmentData.db, but it fails validation:

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) file is encrypted or is not a database

Here is the error. It's not encrypted, so this must be an issue with the syntax. E/SQLiteLog: (26) file is encrypted or is not a database

在调用createFromAsset()时引用的.db文件应采用什么格式?如果某处有现有文档,我将很高兴为您提供参考.

What format should the .db file that is referenced in the call to createFromAsset() take? If there is existing documentation somewhere, I would be glad for the reference.

推荐答案

服务器端数据库系统(MySQL,Oracle,SQL Server等)不太可能关闭时,SQLite数据库是单个文件. createFromAsset()希望使用这种文件,而不是SQL脚本.

Unlikely server-side database systems (MySQL, Oracle, SQL Server, etc.), when closed, a SQLite database is a single file. createFromAsset() is expecting that sort of file, not a SQL script.

所以,我通常要做的是:

So, what I have usually done is:

  • 让应用使用Room的架构,使用标准Room的东西在设备上创建一个空数据库
  • 将该数据库复制到设备上(例如,使用Android Studio中的设备文件资源管理器)
  • 使用SQL浏览器或sqlite3或其他工具将数据添加到我的应用程序表中
  • Have the app create an empty database on a device using Room's schema, using standard Room stuff
  • Copy that database off the device (e.g., using Device File Explorer in Android Studio)
  • Use SQL Browser or sqlite3 or other tools to add data to my app's tables

这样,我保证数据库将具有正确的结构和Room的元数据.

That way, I'm guaranteed that the database will have the right structure and Room's metadata.

这篇关于.db文件应以哪种格式调用Room.createFromAsset("initialData.db")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆