我有一个带有SQLite数据库的android应用程序。当我需要更新我的数据库时,我将我需要保存的所有信息加载到本地变量,删除已安装的数据库,复制新数据库,并将我的变量内容存储回新数据库。我的问题是,在将信息复制回新数据库后,我的应用程序崩溃了。但是,如果我再次使用已经复制的新数据库启动我的应用程序,应用程序正常启动没有问题,我的所有信息都在应用程序中。这是我的日志猫:
05-17 15:11:16.563 4238-4652/? I/SendBroadcastPermission: action:android.net.wifi.RSSI_CHANGED, mPermissionType:0
05-17 15:11:16.565 4238-4652/? E/WifiConfigStore: updateConfiguration freq=5180 BSSID=24:a2:e1:eb:18:c3 RSSI=-65 "InoveOnline_5Ghz"WPA_PSK
05-17 15:11:16.823 4238-4652/? E/WifiConfigStore: updateConfiguration freq=5180 BSSID=24:a2:e1:eb:18:c3 RSSI=-65 "InoveOnline_5Ghz"WPA_PSK
05-17 15:11:17.267 21634-21726/com.aquarismo I/FirebaseCrash: Sending crashes
05-17 15:11:17.272 21634-21726/com.aquarismo E/SQLiteLog: (1032)
05-17 15:11:17.279 21634-21726/com.aquarismo E/UncaughtException: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (Sqlite code 1032), (OS error - 2:No such file or directory)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:790)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1507)
at bbi.a(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):87)
at bbz.run(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):30)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:833)
05-17 15:11:17.302 21634-21661/com.aquarismo I/DynamiteModule: Considering local module com.google.android.gms.tagmanager:9 and remote module com.google.android.gms.tagmanager:213
Selected remote version of com.google.android.gms.tagmanager, version >= 213
05-17 15:11:17.519 21634-21725/com.aquarismo E/SQLiteLog: (1032)
05-17 15:11:17.521 21634-21725/com.aquarismo E/SQLiteDatabase: Error inserting ts=1526566277482 fatal=1 payload=[B@56ab04 size=1370
android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (Sqlite code 1032), (OS error - 2:No such file or directory)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:790)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1507)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1372)
at bbx.run(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):45)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:833)
05-17 15:11:17.540 21634-21727/com.aquarismo I/FirebaseCrash: Sending crashes
05-17 15:11:17.546 21634-21727/com.aquarismo E/SQLiteLog: (1032)
05-17 15:11:17.549 21634-21727/com.aquarismo E/UncaughtException: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (Sqlite code 1032), (OS error - 2:No such file or directory)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:790)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1507)
at bbi.a(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):87)
at bbz.run(:com.google.android.gms.dynamite_dynamitemodulesc@12673022@12.6.73 (040308-194189626):30)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:833)
我保存数据和删除旧数据库的代码:
String destDir = "/data/data/" + getPackageName() + "/databases/";
String destPath = destDir + "AquaInfo37";
File f = new File(destPath);
// se nao existir cria DB
if (!f.exists()) {
File directory = new File(destDir);
directory.mkdirs();
// apaga todos os ficheiros (BD antigas) que possam existir na
// diretoria se existir alguma bd, guarda os favoritos. Nome da Bd
// antiga, se existir, esta em nome[0]
if (directory.isDirectory()) {
String[] array = directory.list();
// guarda os favoritos da base de dados anterior
for (int i = 0; i < array.length; i++) {
String nome = array[i];
if (nome.contains("AquaInfo") && nome.length() < 11) {
// here I save my data to my variables saveRestoreDadosBaseDadosTemporario.getMeusDadosBaseDados(this, nome);
new_db = true;
break;
}
}
// apaga todas as bd antigas que possam existir
for (int i = 0; i < array.length; i++) {
new File(directory, array[i]).delete();
}
}
// volta a criar a directoria
directory.mkdirs();
// copia para a BD para a nova directoria
try {
CopyDB(getBaseContext().getAssets().open("aquarismo"), new FileOutputStream(destPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}