【问题标题】:Error reading Android SQLite database using Kotlin使用 Kotlin 读取 Android SQLite 数据库时出错
【发布时间】:2020-11-27 16:05:07
【问题描述】:

运行下面的代码时,我收到错误消息:

java.lang.RuntimeException: Unable to start activity
 ComponentInfo{com.example.songapp/com.example.songapp.MainActivity}: java.io.FileNotFoundException:
 /data/data/com.example.songapp/databases/songs.db: open failed: ENOENT (No such file or directory)

给出错误的代码是这样的:

@Throws(IOException::class)
fun copyDataBase(context: Context, OUTPUT_DB_PATH: String, DB_NAME: String) {
    print("*** in copyDataBase ***")
    // Path to output database path (application database path).
    var APP_DB_FILE_PATH = OUTPUT_DB_PATH + "/" + DB_NAME

    // Path to input database file.
    var INPUT_DB_PATH = "database/" + DB_NAME

    // Open your local db as the input stream
    // This is how to get path from assets directory
    val databaseInput: InputStream = context.assets.open(INPUT_DB_PATH)

    // Open the empty db as the output stream
    val databaseOutput: OutputStream = FileOutputStream(APP_DB_FILE_PATH)

    // Transfer bytes from the inputfile to the outputfile
    val buffer = ByteArray(1024)
    var length: Int
    while (databaseInput.read(buffer).also { length = it } > 0) {
        databaseOutput.write(buffer, 0, length)
    }

    // Close the streams
    databaseOutput.flush()
    databaseOutput.close()
    databaseInput.close()
}

在主要活动中调用如下:

var DB_PATH = "/data/data/com.example.songapp/databases/"
var DB_NAME = "songs.db"

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Get context and supply it to the function
        // This is where error happens, by disabling this the app run successful.
        copyDataBase(this, DB_PATH, DB_NAME)

        setContent {
            Column() {
                TopAppBar(title = { Text(text = "Nyimbo") })
                ScrollableColumn(Modifier.fillMaxWidth()
                    .padding(10.dp)) {
                    lyricsPageComposable(lyricsMap = songMap)
                }
            }
        }
    }
}

我在资产目录 assets/database/songs.db 中有一个预填充的 SQLite 数据库,我希望将它复制到 /data/data/com.example.songapp/databases/songs.db。我不知道如何检查复制是否成功。我在这里想念什么?我知道找不到数据库文件。是不是路径不对?难道是数据库没有被复制?我怎么知道?我正在使用实体 Android 手机来测试应用。

【问题讨论】:

  • 您是否声明了相关权限并授予您的应用这些权限? <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  • @DavidKroukamp:代码没有写入外部存储。
  • @CommonsWare 既然你这么说,那就对了。好吧值得一试????

标签: android kotlin android-sqlite android-jetpack


【解决方案1】:

运行下面的代码时,我收到错误消息

很可能,/data/data/com.example.songapp/databases/ 尚未作为目录存在。

在主要活动中调用如下

请不要对路径进行硬编码。您可以通过calling getDatabasePath() on your Activity or other Context 获取数据库的标准位置。

不知道如何查看复制是否成功

如果您使用的是 Android Studio,默认停靠在右侧的设备文件资源管理器工具可让您浏览设备的文件系统。

【讨论】:

  • 谢谢,从资源管理器我可以看到数据库没有写入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多