gpt4 book ai didi

android - 数据库未在 OnePlus 2 中正确复制

转载 作者:IT老高 更新时间:2023-10-28 23:00:48 26 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,并在其中使用 sqlite 数据库。为此,我在项目的 Assets 文件夹中放置了一个 sqlite 文件,并且在我第一次使用下面的代码执行应用程序时将此文件复制到手机。

 private void copyDataBase() throws IOException {
new File(DB_PATH).mkdirs();
InputStream myInput = appContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;

while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();
}

但我收到此错误。

 09-21 18:03:56.841: E/SQLiteLog(7850): (1) no such table: tbl_player

但该表存在于 Assets 文件中。所以我使用这种方法从手机中获取数据库文件。

public static void exportDB(String databaseName, Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();

if (sd.canWrite()) {
String currentDBPath = "//data//" + context.getPackageName()
+ "//databases//" + databaseName + "";
String backupDBPath = "sensor_game.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);

if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB)
.getChannel();
FileChannel dst = new FileOutputStream(backupDB)
.getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}

} catch (Exception e) {

}
}

我发现提取的数据库文件中没有表。

注意:此问题仅在 OnePlus Two 中出现,并且在 Nexus 4Htc 820Moto E< 中正常工作Galxy S3Galaxy Quottro

最佳答案

不同设备的数据库路径可能不同。您需要使用 Context.getDatabasePath(String) 来获取数据库路径。

例如

File backupDB = context.getDatabasePath(backupDBPath);

关于android - 数据库未在 OnePlus 2 中正确复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com