在资产数据库复制到Android的内部数据库失败,在模拟器第一次尝试 [英] Copying database in assets to Android's internal databases fails at first attempt in the emulator

查看:179
本文介绍了在资产数据库复制到Android的内部数据库失败,在模拟器第一次尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库(1.69 MB)的资产需要复制到内部数据库文件夹。我的问题是,对于第一次尝试数据库被复制,但不与具体的表。它成为在模拟器文件浏览器3072(3kB的)尺寸。

I have a database (1.69 MB) in assets to need copy to internal databases folder. My problem is that for the first try db is copied but not with exact tables. It becomes 3072 (3kB) size in the emulator file explorer.

当我看着那分贝sqlite的探险家,我不能看到我的表。唯一的表存在于数据库是android_metadata表与我的现场信息一栏。

When I look in that db with sqlite explorer I can't see my tables. The only table exists in db is android_metadata table with one column of my locale info.

但是,如果我清除DB从数据库中并重新运行应用程序,它似乎在为这个时间。为什么它的第一次尝试失败了呢?我怎样才能确保它不会发生在现实的设备? 是与模拟器中的错误?

But if I clear that db from databases and re-run the application, it seems working for this time. Why does it fail for the first try? How can I be sure it won't happen in real devices? Is that a bug with the emulator?

推荐答案

我终于想出来纳雷什的帮助下结论(谢谢)。在这里,简短的摘要: 在第一次运行我试图复制我的1.6MB data.db文件从资产/data.../databases其中一个根本不存在的文件夹。因此,

finally i've come up the conclusion with the help of Naresh (thank you). Here the short summary: at the first run i was trying to copy my 1.6mb data.db file from assets to /data.../databases folder which one never existed. So

OutputStream os = new FileOutputStream(destPath);

行了错误。不过,该行后调用它创建指定路径下的数据库文件夹getreadabledatabase我dbhelper实例。 Android的把一个数据库具有相同名称的,但它没有有用的数据。在我copyDatabase方法我更新,如下所示:

line gave error. But after that line my dbhelper instance called getreadabledatabase which created the databases folder under specified path. Android put a db with same name but no useful data in it. In my copyDatabase method i updated it as follows:

InputStream is = getBaseContext().getAssets().open(assetsDB);

            //when there is no databases folder fileoutputstream gives error,
            //we have to make sure databases folder exists
            DbAdapter temp = new DbAdapter(getApplicationContext());
            temp.open();        //gets readable database: creates databases folder containing DB_NAME db
            temp.close();       //since we don use this temp, we close

            //this wont give error: because path is now exists (databases folder exists)
            OutputStream os = new FileOutputStream(destPath);

            //copying 1K bytes at a time
            byte[] buff = new byte[1024];

这是解决方案,我想出来的。顺便说一句,前SDK的2.3资产数据库大小应小于1MB。这是anohter问题,并找到了解决方案的这里

this is the solution i've come up with. By the way, former sdk's of 2.3 assets database size should be less than 1mb. This is anohter issue and found the solution here

这篇关于在资产数据库复制到Android的内部数据库失败,在模拟器第一次尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆