无法从Android中的Asset文件夹复制数据库

时间:2014-11-08 17:06:15

标签: android database sqlite assets

我有一个我在SQLite浏览器中创建的数据库。它到目前为止有34个表,我还计划使用ORMLite来进行数据库插入,删除等。 我的问题是,由于我创建了脱机数据库,我将不得不使用该应用程序发送数据库。所以我将我的数据库保存在assets文件夹中,然后我尝试复制数据库。 浏览其他stackoverflow帖子让我使用this code。我从我的MainActivity中调用这个类,这样每当用户进入我的应用程序时,我都可以检查我的数据库是否存在。但事情并没有按预期发挥作用。这是logcat输出link。它表明它无法在资产中找到我的数据库文件。或者我可能错了。请帮忙!谢谢!

MainActivity.java中的相关代码

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

AssetDatabaseOpenHelper.java中的相关代码。

 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

1 个答案:

答案 0 :(得分:2)

您应该确保数据库路径存在。

我假设您正在尝试将数据库复制到不存在的目录中,因为在copyDatabase()中创建FileOutputStream的行上的错误消息是“No such file or directory”。