我有大量的数据库,我只需要创建一旦应用程序是安装的,这就是为什么我把数据库文件放在资产文件夹,但我不确定如何读取它使用SQLDELIGHT,有什么想法吗?
发布于 2021-05-18 21:12:43
是的,这是可能的。
在创建驱动程序实现之前,上面的代码如下:
val driver: SqlDriver = AndroidSqliteDriver (Database.Schema, context, "test.db")您预先填充的数据库必须以指定的名称(此处为"test.db")放置到Android存储数据库的目录中:
// 1. get the database file from the application context obtained in the DatabaseDriverFactory constructor
val database = context.getDatabasePath("test.db")
// 2. check the database file (in first launch the app it does not exist yet)
if (!database.exists()) {
// 3. copy your pre-populated database from resources into the Android database directory
val inputStream = context.assets.open("dbFileName")
val outputStream = FileOutputStream(database.absolutePath)
inputStream.use { input: InputStream ->
outputStream.use { output: FileOutputStream ->
input.copyTo(output)
}
}
}
// 4. create the driver
val driver: SqlDriver = AndroidSqliteDriver(Database.Schema, context, "test.db")请注意:
放置在资产中的
https://stackoverflow.com/questions/60480150
复制相似问题