在android中访问.sqlite数据库(>1Mb大小)

发布于 2024-11-07 11:21:20 字数 244 浏览 0 评论 0原文

我正在开发一个图书馆应用程序,并将书籍存储为 .sqlite 文件。每本书的sqlite数据库文件大小约为10MB。我首先尝试将其放入资产文件夹中,然后将其复制到数据库/文件夹中,但由于文件> 1Mb,这给了我一个 IOException。然后我尝试从原始文件夹访问它,但它仍然给我 IOException。那么,访问此类文件的正确方法是什么?另外,将来应用程序可能需要从服务器下载此类文件,那么在这种情况下我应该将此类数据库文件存储在哪里?

谢谢!!

I am working on a library app and have books stored as .sqlite files. Each book's sqlite database file is about 10MB in size. I first tried to put it in assets folder and then copying it to database/ folder but since the file is >1Mb this gives me an IOException. Then I tried to access it from raw folder but its still giving me IOException. So, what is the correct way to access such a file. Also, in future the app might need to download such files from server, so in that case where should I store such database files?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚笙 2024-11-14 11:21:20

在 Android 3.0 之前,您不允许打开大于 1mb 的文件

来自链接:[P]在Android 2.3之前,任何未压缩大小超过1 MB的压缩资源文件都无法从APK中读取。

以下是一些解决方案:

  1. 也许您想考虑缩小数据库的大小,这可能是通过创建自己的数据库,或者删除一些您不需要/不想要的条目。

  2. 另一个解决方案是将数据库卸载到您有权访问的服务器上,并要求应用程序访问网络以从此服务器获取所需的数据。我可以想象一个很好的 RESTful API 来完成此操作。

Before Android 3.0 you are not allowed to open files larger than 1mb.

From the link: [P]rior to Android 2.3, any compressed asset file with an uncompressed size of over 1 MB cannot be read from the APK.

Here are some solutions:

  1. Perhaps you want to look into shrinking the size of the databases, and that might be by making your own, or by removing some of the entries that you do not need/want.

  2. Another solution would be to offload the databases to a server that you have access to and require the application to access the web to get the data that is necessary from this server. I can imagine a nice RESTful API to do this.

昇り龍 2024-11-14 11:21:20

此问题有一个解决方法。 1MB 的限制仅适用于 SQLite 文件,不适用于其他文件类型。将您的 SQLite 文件重命名为“db.mp3”之类的名称,然后当您的应用程序启动时,您可以将此文件复制到 SD 卡并将其用作 SD 卡中的普通 SQLite 文件。
我已经实现了这个解决方案,它在所有 Android 版本上都运行得很好。

There is a workaround to this issue. The limitation of 1MB is only for SQLite files and not for other file types. Rename your SQLite file as something like "db.mp3" and then when your app starts, you can copy this file to your SD Card and use it as a normal SQLite file from SD card.
I have implemented this solution and it works perfectly fine on all Android versions.

嗳卜坏 2024-11-14 11:21:20

让应用程序将数据库从 Web 服务器下载到 SD 卡 - 这样可以节省在 .apk 中存储压缩版本和在外部存储未压缩版本的浪费。而且它并不是真正的不安全,因为 .apk 只是任何真正想要阅读的人都可以阅读的 zip 文件。

Have the app download the database from a web server to the sdcard - this saves the waste of storing both a compressed version in the .apk and an uncompressed version outside. And it's not really any more insecure as an .apk is just a zip file anyone who really wants to can read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文