将数据库嵌入到分布式应用程序的 .apk 中 [Android]

发布于 2024-09-05 19:41:33 字数 244 浏览 2 评论 0原文

我的问题是我认为很简单,但我认为答案不会...... 我的应用程序中有相当多的内容要使其正常运行,我正在考虑将所有内容放入数据库中,并将其分发到嵌入式数据库中,并将应用程序放在市场。 唯一的麻烦是我不知道该怎么做。 我知道我可以从 Eclipse DDMS 中提取包含数据库内容的文件 .db,并且我想我需要将其放入应用程序的资产文件夹中,但是如何让应用程序使用它来重新生成应用程序数据库? 如果您有任何代码或帮助的链接,那就太好了。 谢谢

My question is I think quite simple but I don't think the answer will be...
I have quite a lot of content to have in my application to make it run properly, and I'm thinking of putting all of it in a database, and distribute it in an embeded database with the application in the market.
The only trouble is that I have no idea of how to do that.
I know that I can extract a file .db from Eclipse DDMS with the content of my database, and I suppose I need to put it in the asset folder of my application, but then how to make the application use it to regenerate the application database?
If you have any link to some code or help, that would be great.
Thanks

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

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

发布评论

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

评论(2

灯下孤影 2024-09-12 19:41:33

为了实现您的要求,您可以将 .db 文件放在 asset/ 目录中,并在第一次启动应用程序时将其复制到位......

final String DB_DESTINATION = "/data/data/YOUR_PACKAGE_NAME/databases/MyDatabaseFile.db";

// Check if the database exists before copying
boolean initialiseDatabase = (new File(DB_DESTINATION)).exists();
if (initialiseDatabase == false) {

    // Open the .db file in your assets directory
    InputStream is = getContext().getAssets().open("MyDatabaseFile.db");

    // Copy the database into the destination
    OutputStream os = new FileOutputStream(DB_DESTINATION);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) > 0){
        os.write(buffer, 0, length);
    }
    os.flush();

    os.close();
    is.close();
}

其中“initialiseDatabase”是一些标志,指示应用程序是否具有首次推出。

尽管如此,如果您正在考虑存储大量数据,正如您所提到的:我强烈建议您避免使用它来使 APK 文件膨胀,并在应用程序安装后使用互联网连接(从托管服务器)下载数据库记录。推出。通过这种方式,您可以显示一个进度条,向用户指示为什么他们需要等待很长时间才能开始使用该应用程序。让 APK 特别大通常会首先阻止人们安装它。

Well to achieve what you're asking, you could put the .db file in your assets/ directory and copy it into place the first time you start your app.....

final String DB_DESTINATION = "/data/data/YOUR_PACKAGE_NAME/databases/MyDatabaseFile.db";

// Check if the database exists before copying
boolean initialiseDatabase = (new File(DB_DESTINATION)).exists();
if (initialiseDatabase == false) {

    // Open the .db file in your assets directory
    InputStream is = getContext().getAssets().open("MyDatabaseFile.db");

    // Copy the database into the destination
    OutputStream os = new FileOutputStream(DB_DESTINATION);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) > 0){
        os.write(buffer, 0, length);
    }
    os.flush();

    os.close();
    is.close();
}

Where "initialiseDatabase" is some flag indicating if the app has been launched for the first time.

Although, if you are looking at storing a lot of data, as you mentioned: I strongly recommend you avoid bloating the APK file with it, and use the Internet connection to download the database records (from a hosted server) after the app has been launched. This way you can show a progress bar indicating to the user why they are waiting a long time to start using the app. Making the APK especially large usually deters people from installing it in the first place.

栀子花开つ 2024-09-12 19:41:33

您可以实现中间的解决方案:使用上面的方法,但不是分发数据库文件,而是将其放在服务器上的某个位置。根据我的经验,获取文件只占处理时间的 10%,剩下的就是解析图像并将其序列化到数据库

You can implement middle-of-the-road solution: Use approach above, but instead of distributing db file put it on server somewhere. In my experience getting files is only 10% of processing time, rest of it is parsing and serializing images to db

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