2014-11-08 182 views
1

我有一個在SQLite瀏覽器中創建的數據庫。目前爲止有34個表格,我也計劃使用ORMLite來做數據庫插入,刪除等操作。 我的問題是,由於我離線創建了數據庫,我將不得不使用應用程序發送數據庫。所以我把我的數據庫保存在資產文件夾中,然後試圖複製數據庫。 通過其他stackoverflow帖子使我使用this code。我從MainActivity調用這個類,以便每當用戶在我的應用程序中時,我都可以檢查我的數據庫是否存在。但事情並沒有按預期那樣發揮作用。這裏是logcat輸出link。它顯示它無法在資產中找到我的數據庫文件。或者我可能是錯的。請幫忙!謝謝!無法從Android中的資產文件夾複製數據庫

相關規範在MainActivity.java

在AssetDatabaseOpenHelper.java
AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(this); 
    SQLiteDatabase db = adb.openDatabase(); 
    if(db.isOpen()) 
    { 
     System.out.println("Database open, now closing"); 
     db.close(); 
    } 

相關規範。

private static final String DB_NAME = "mydb.db"; 
public SQLiteDatabase openDatabase() { 
    File dbFile = context.getDatabasePath(DB_NAME); 
    System.out.println(dbFile.toString()); 
    //File databasefile = new File(context.getCacheDir(),"databases"); 
    //System.out.println(databasefile.toString()); 
    if (!dbFile.exists()) { 
     try { 
      //databasefile.mkdir(); 
      copyDatabase(dbFile); 
     } catch (IOException e) { 
      throw new RuntimeException("Error creating source database", e); 
     } 
    } 

    return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY); 
} 

private void copyDatabase(File dbFile) throws IOException { 
    InputStream is = context.getAssets().open(DB_NAME); 
    OutputStream os = new FileOutputStream(dbFile); 

    byte[] buffer = new byte[1024]; 
    while (is.read(buffer) > 0) { 
     os.write(buffer); 
    } 

    os.flush(); 
    os.close(); 
    is.close(); 
} 

的logcat:

 
11-08 16:24:03.773: E/Trace(5937): error opening trace file: No such file or directory (2) 
11-08 16:28:32.524: E/Copy Error(7319): Copy may have been aborted 
11-08 16:30:17.233: E/Trace(7890): error opening trace file: No such file or directory (2) 
11-08 16:30:18.213: E/Copy Error(7890): Copy may have been aborted 
11-08 16:32:57.994: E/Trace(8751): error opening trace file: No such file or directory (2) 
11-08 16:32:58.773: E/Copy Error(8751): Copy may have been aborted 
11-08 16:42:46.534: E/Trace(11771): error opening trace file: No such file or directory (2) 
11-08 16:42:47.164: E/AndroidRuntime(11771): FATAL EXCEPTION: main 
11-08 16:42:47.164: E/AndroidRuntime(11771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.[packagename ommitted]/com.[packagename ommitted].MainActivity}: java.lang.RuntimeException: Error creating source database 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.os.Looper.loop(Looper.java:137) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.app.ActivityThread.main(ActivityThread.java:5041) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at java.lang.reflect.Method.invokeNative(Native Method) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at java.lang.reflect.Method.invoke(Method.java:511) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at dalvik.system.NativeStart.main(Native Method) 
11-08 16:42:47.164: E/AndroidRuntime(11771): Caused by: java.lang.RuntimeException: Error creating source database 
11-08 16:42:47.164: E/AndroidRuntime(11771): at [packagename ommitted].db.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:29) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at [packagename ommitted].MainActivity.onCreate(MainActivity.java:65) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.app.Activity.performCreate(Activity.java:5104) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
11-08 16:42:47.164: E/AndroidRuntime(11771): ... 11 more 
11-08 16:42:47.164: E/AndroidRuntime(11771): Caused by: java.io.FileNotFoundException: /data/data/[packagename ommitted]/databases/mydb.db: open failed: ENOENT (No such file or directory) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at libcore.io.IoBridge.open(IoBridge.java:416) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at java.io.FileOutputStream.(FileOutputStream.java:88) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at java.io.FileOutputStream.(FileOutputStream.java:73) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at [packagename ommitted].db.AssetDatabaseOpenHelper.copyDatabase(AssetDatabaseOpenHelper.java:38) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at [packagename ommitted].db.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:27) 
11-08 16:42:47.164: E/AndroidRuntime(11771): ... 15 more 
11-08 16:42:47.164: E/AndroidRuntime(11771): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at libcore.io.Posix.open(Native Method) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
11-08 16:42:47.164: E/AndroidRuntime(11771): at libcore.io.IoBridge.open(IoBridge.java:400) 
11-08 16:42:47.164: E/AndroidRuntime(11771): ... 19 more 
11-08 16:45:36.693: E/Trace(12662): error opening trace file: No such file or directory (2) 
11-08 16:45:37.293: E/AndroidRuntime(12662): FATAL EXCEPTION: main 
11-08 16:45:37.293: E/AndroidRuntime(12662): java.lang.RuntimeException: Unable to start activity ComponentInfo{[packagename ommitted]/[packagename ommitted].MainActivity}: java.lang.RuntimeException: Error creating source database 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.os.Looper.loop(Looper.java:137) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.app.ActivityThread.main(ActivityThread.java:5041) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at java.lang.reflect.Method.invokeNative(Native Method) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at java.lang.reflect.Method.invoke(Method.java:511) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at dalvik.system.NativeStart.main(Native Method) 
11-08 16:45:37.293: E/AndroidRuntime(12662): Caused by: java.lang.RuntimeException: Error creating source database 
11-08 16:45:37.293: E/AndroidRuntime(12662): at [packagename ommitted].db.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:29) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at [packagename ommitted].MainActivity.onCreate(MainActivity.java:65) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.app.Activity.performCreate(Activity.java:5104) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
11-08 16:45:37.293: E/AndroidRuntime(12662): ... 11 more 
11-08 16:45:37.293: E/AndroidRuntime(12662): Caused by: java.io.FileNotFoundException: /data/data/[packagename ommitted]/databases/mydb.db: open failed: ENOENT (No such file or directory) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at libcore.io.IoBridge.open(IoBridge.java:416) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at java.io.FileOutputStream.(FileOutputStream.java:88) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at java.io.FileOutputStream.(FileOutputStream.java:73) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at [packagename ommitted].db.AssetDatabaseOpenHelper.copyDatabase(AssetDatabaseOpenHelper.java:38) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at [packagename ommitted].db.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:27) 
11-08 16:45:37.293: E/AndroidRuntime(12662): ... 15 more 
11-08 16:45:37.293: E/AndroidRuntime(12662): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at libcore.io.Posix.open(Native Method) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
11-08 16:45:37.293: E/AndroidRuntime(12662): at libcore.io.IoBridge.open(IoBridge.java:400) 
11-08 16:45:37.293: E/AndroidRuntime(12662): ... 19 more 
+0

您應該確保數據庫路徑存在。我假設你正試圖將數據庫複製到一個不存在的目錄中。 – Robert 2014-11-08 17:36:43

+1

請在這裏發佈使用的代碼。由於代碼量非常小,因此沒有必要提供鏈接。也發佈logcat在這裏。您沒有告訴您要將數據庫文件複製到哪裏。 – greenapps 2014-11-08 17:49:04

+0

@羅伯特你的假設似乎是真的。如果我在文件資源管理器中手動在data/data/package-name /中創建文件夾「databases」,那麼數據庫副本是成功的。但是,我不能從我的程序那樣做,那就是先創建目錄。試過mkdir()不起作用。 – Arefin 2014-11-08 18:37:21

回答

2

你應該確保數據庫的路徑存在。

我假設你正在嘗試將數據庫複製到一個不存在的目錄中,因爲錯誤消息是在你創建copyDatabase()中的FileOutputStream的行上「沒有這樣的文件或目錄」。

+0

這個錯誤已經出現在我身上了。當你「清除數據」和「卸載應用程序」時,android會刪除「/ data/data//databases」目錄。因此,你絕對必須通過if(!dbFile.getParentFile()。exists())dbFile.getParentFile()。mkdir();創建「database」dir,如@greenapps所說。感謝@Robert和@greenapps – 2017-01-04 12:58:25

相關問題