从Internet下载SQLite数据库并加载到Android应用程序 [英] Download SQLite database from Internet and load into Android application

查看:183
本文介绍了从Internet下载SQLite数据库并加载到Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Android应用程序,我想使用一个大型数据库(大约45 MB)。



一个解决方案是将(分割)数据库包含在资源文件夹,并将其复制到第一次启动时的数据库目录。



但是这会占用两倍的磁盘空间 - 在资源文件夹中有一次不能删除并在数据库目录中一次被复制到。



所以我宁愿在第一次启动时从Internet(webserver)下载数据库。我该怎么做?可以下载一个完整的SQLite文件并将其保存到数据库目录中吗?或者我应该放弃用于填充数据库的JSON数据文件?

解决方案


一解决方案是将(分割)数据库包含在资产文件夹中,并将其复制到第一次启动时的数据库目录。


它不需要拆分,只是压缩。请参阅 SQLiteAssetHelper


我该怎么做?


使用 HttpUrlConnection 。或者,使用 HttpClient


我可以下载一个完整的SQLite文件并保存到数据库目录?


是的。使用 getDatabasePath( ) 以获取正确的本地路径。


或者我应该放弃JSON用于填充数据库的数据文件?


你可以,但是对于45MB,这将是非常缓慢的。

For my android application, I would like to use a large database (about 45 MB).

One solution would be to include the (splitted) database in the assets folder and copy it to the database directory on the first startup.

But this would consume disk space twice - one time in the assets folder where the file can't be deleted and one time in the database directory where it has been copied to.

So I would rather like to download the database from the Internet (webserver) on the first startup. How could I do this? Can I download a complete SQLite file and save it to the database directory? Or should I rather go with JSON data files that are used to populate the database?

解决方案

One solution would be to include the (splitted) database in the assets folder and copy it to the database directory on the first startup.

It would not have to be split, just ZIPped. See SQLiteAssetHelper for an example.

How could I do this?

Use HttpUrlConnection. Or, use HttpClient.

Can I download a complete SQLite file and save it to the database directory?

Yes. Use getDatabasePath() to get the correct local path to use.

Or should I rather go with JSON data files that are used to populate the database?

You could, but for 45MB, that would be ghastly slow.

这篇关于从Internet下载SQLite数据库并加载到Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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