【发布时间】:2015-09-21 14:01:54
【问题描述】:
我正在创建一个 Android 应用程序,并在其中使用 sqlite 数据库。为此,我在项目的资产文件夹中放置了一个 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
但该表存在于资产文件中。所以我使用这种方法从手机中获取数据库文件。
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 4、Htc 820、Moto E、Galxy S3 和 Galaxy Quottro 中正常工作
【问题讨论】:
-
void exportDB。将该布尔值设为布尔值并添加代码以防 currentDB 不存在。该代码将/不能工作。 -
有时我发现了同样的问题。
-
我正在试验我在 OP2 中开发的应用程序的问题,这让我发疯了
-
你找到解决这个问题的方法了吗?
-
否:/仍在试图找出问题所在
标签: android sqlite assets oneplustwo