从互联网下载的SQLite数据库,并加载到Android应用 [英] Download SQLite database from Internet and load into Android application

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

问题描述

有关我的Andr​​oid应用程序,我想用一个大的数据库(约45 MB)。

一个解决办法是,包括在资产文件夹中(分裂)的数据库,并将其复制到数据库目录上的第一次启动。

但是,这会占用磁盘空间的两倍 - 一次在资产所在的文件夹的文件不能被删除,一次在数据库目录中已经被复制到

所以,我宁愿喜欢从第一个启动的互联网(Web服务器)下载数据库。我怎么能这样做?我可以下载一个完整的SQLite文件并将其保存到数据库目录?还是应该我宁愿与用于填充数据库的JSON数据文件去了?

解决方案
  

一个解决办法是,包括在资产文件夹中(分裂)的数据库,并将其复制到数据库目录上的第一次启动。

它也不会被分裂,只是压缩。请参见 SQLiteAssetHelper 一个例子。

  

我怎么能这样做呢?

使用的HttpURLConnection 。或者,使用的HttpClient

  

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

是的。使用<一个href="http://developer.android.com/reference/android/content/Context.html#getDatabasePath%28java.lang.String%29"><$c$c>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.

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

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