使用 Kotlin 读取 Android SQLite 数据库时出错
Error reading Android SQLite database using Kotlin
当 运行 下面的代码时,我收到错误消息:
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()
}
这在主 activity 中调用如下:
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 phone 来测试应用程序。
When running the code below I get the error message
很可能,/data/data/com.example.songapp/databases/
还不作为目录存在。
This is being called in the main activity as follows
请不要硬编码路径。您可以通过 calling getDatabasePath()
on your Activity
or other Context
.
获取数据库的标准位置
I do not know how to check whether copying has succeeded
如果您使用的是 Android Studio,设备文件资源管理器工具默认停靠在右侧,可让您浏览设备的文件系统。
当 运行 下面的代码时,我收到错误消息:
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()
}
这在主 activity 中调用如下:
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 phone 来测试应用程序。
When running the code below I get the error message
很可能,/data/data/com.example.songapp/databases/
还不作为目录存在。
This is being called in the main activity as follows
请不要硬编码路径。您可以通过 calling getDatabasePath()
on your Activity
or other Context
.
I do not know how to check whether copying has succeeded
如果您使用的是 Android Studio,设备文件资源管理器工具默认停靠在右侧,可让您浏览设备的文件系统。