安卓:复制数据库的资源文件夹,但只获得一个空文件 [英] Android: copy database from asset folder, but only get an empty file

查看:124
本文介绍了安卓:复制数据库的资源文件夹,但只获得一个空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我复制数据库时,从本地资源文件夹/数据/数据​​/程序包/数据库目录有问题。由于我用的是<一href="http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/">http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/教程做到这一点,我只能得到一个空文件。

guys, I have the problem when copying database from local assets folder to /data/data/package_name/databases directory. As I use the http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ tutorial to do it, I can only get an empty file.

我引用copyDataBase()方法的一部分,并没有什么区别。每次应用程序启动时,将创建目录和空数据库。那么,有没有办法让copyDataBase()的工作?

I quoted the part of copyDataBase() method and there is no difference. Every time the app start, it will create the directory and empty database. So is there any way to make the copyDataBase() work?

非常感谢你!

推荐答案

<打击>我不会复制任何数据库形成资产 -folder。如果你需要一些标准条目在你的数据库,你可以用它们添加插入在你的onCreate() -method

I wouldn't copy any database form the assets-folder. If you need some standard entry's in your Database, you can add them using INSERTs in your onCreate()-method.

更新:由于这是越来越下降,投票支持自己的错误(这是有点右)和我不能删除它,这里是一个小更新

Update: Since this is getting down-voted for being wrong (which is kinda right) and I can't delete it, here is a little update.

我想说的这取决于当你要多少标准条目添加到您的数据库。如果它只是一个或两个,出货包装DB可能不值得的。

I'd say it depends upon how many standard entries you want to add to your database. If it's just one or two, shipping a packed DB might not be worth it.

不管怎么说,一些应用程序配备了相当大的数据库(例如,一个配方集合)。你可以明显不添加所有这些在code。

Anyways, some apps come with rather large databases (for example, a recipe collection). You can obviously not add all these in code.

  • 对于小型测试项目,我仍然preFER简单地将它们添加在的onCreate()
  • 对于更大的数据库,你应该pre-填充它们和船舶EM随着你的应用程序。

对于以后的工作,你需要从将数据库文件复制资产/ 来您的应用程序文件夹。有一个很好的库来处理,对于您: Android的sqlite的资产辅助

For the later to work, you'll need to copy the database file from assets/ to your app-folder. There is a nice library to handle that for you: android-sqlite-asset-helper

这篇关于安卓:复制数据库的资源文件夹,但只获得一个空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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