合并两个Sqlite数据库&从附加数据库转储数据(无法打开数据库)

时间:2014-09-12 10:06:58

标签: android performance android-sqlite

我有两个数据库Mydb.dbUpdatedb.db。它们具有相同的结构,并且都具有相同的Table1

我想将Updatedb.db中的所有记录添加到Table1

有大约25000条记录需要更新。

我用过:

mDataBase.openDataBaseForWrite();
    SQLiteDatabase myInternalDatabase = mDataBase.getDb();
    myInternalDatabase.execSQL("ATTACH DATABASE '" + ATTACH_DB_PATH
            + File.separator + ATTACH_DB_NAME + "' AS New_DB");

    myInternalDatabase
            .execSQL("INSERT INTO ResultMaster SELECT * FROM New_DB.ResultMaster;");

我得到了这个例外:

09-16 14:44:32.584: E/AndroidRuntime(2766): FATAL EXCEPTION: main
09-16 14:44:32.584: E/AndroidRuntime(2766): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database: /mnt/sdcard/AttachDb/backupname.db (code 14)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:727)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at com.attachtwodatabase.MainActivity.attachDatabase(MainActivity.java:62)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at com.attachtwodatabase.MainActivity.access$0(MainActivity.java:59)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at com.attachtwodatabase.MainActivity$1.onClick(MainActivity.java:49)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.view.View.performClick(View.java:4207)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.view.View$PerformClick.run(View.java:17372)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.os.Handler.handleCallback(Handler.java:725)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.os.Looper.loop(Looper.java:137)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at java.lang.reflect.Method.invokeNative(Native Method)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at java.lang.reflect.Method.invoke(Method.java:511)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-16 14:44:32.584: E/AndroidRuntime(2766):     at dalvik.system.NativeStart.main(Native Method)

我做错了什么?

1 个答案:

答案 0 :(得分:5)

ATTACH要合并到目标数据库的数据库将附加数据库中的记录插入目标:

INSERT INTO table1 SELECT * FROM attacheddb.table1;

这样做,例如在asynctask中防止ANR。

  

还需要更新已存在的条目

为此,您可以指定冲突解决策略,例如

INSERT ... ON CONFLICT REPLACE;