Tab*_*boo 3 database android kotlin sqldelight
我有大量数据库,只需在安装应用程序后创建,这就是我将数据库文件放置在资产文件夹中的原因,但我不确定如何使用 SQLDELIGHT 读取它,有什么想法?
是的,这是可能的。
在创建驱动程序实现之前,请在以下代码之上:
val driver: SqlDriver = AndroidSqliteDriver (Database.Schema, context, "test.db")
Run Code Online (Sandbox Code Playgroud)
您的预填充数据库必须以指定名称(此处为“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")
Run Code Online (Sandbox Code Playgroud)
请注意:
| 归档时间: |
|
| 查看次数: |
1498 次 |
| 最近记录: |