在分布式应用程序的apk文件嵌入数据库[机器人]分布式、机器人、应用程序、数据库

2023-09-12 08:53:33 作者:Chihiro(千寻)

我的问题是我觉得很简单,但我不认为答案是... 我有相当多的内容在我的应用程序,使其正常运行,并且我想将所有的在数据库中,和在嵌入式数据库分发与应用程序市场。 唯一的麻烦是,我不知道该怎么做。 我知道,我可以提取一个文件名为.db从Eclipse的DDMS与我的数据库的内容,我想我需要把它放在我的应用程序的资源文件夹,但如何使应用程序中使用它来生成应用程序数据库? 如果您有任何链接到一些code或帮助,那将是巨大的。 谢谢

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

推荐答案

那么,实现你的要求,你可以把.db文件在您的资产/目录下,并将其复制到地方第一次启动您的应用程序......

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();
}

在哪里initialiseDatabase是一些标志,指示,如果应用程序已经启动的第一次。

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

尽管如此,如果你正在寻找存储大量的数据,正如你所说:我强烈建议你避免腹胀的APK文件,有了它,并使用后的互联网连接(从托管的服务器)下载该数据库记录应用程序已经启动。这样,您就可以显示一个进度条,表示为什么他们都在等待很长时间才能开始使用应用程序的用户。使得APK特别大,通常阻止人们在第一时间安装。

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.

 
精彩推荐
图片推荐