这是关于我的应用程序的一些背景信息:
我正在开发一个 Android 应用程序,它会向用户显示随机引用或诗句。为此,我使用 SQLite 数据库。数据库的大小大约为 5K 到 10K 条记录,随着新引语和诗句的添加,在以后的版本中可能会增加到 1M。因此,当应用程序或数据库的新版本发布时,用户需要更新数据库。
在浏览了一些在线论坛后,似乎有两种可行的方法可以提供数据库: 1. 将其与应用程序的 .APK 文件 bundle 在一起,或者 2. 将它上传到我的应用程序的网站,用户必须从那里下载它
我想知道哪种方法更好(如果还有其他方法,请告诉我)。
在思考这个问题一段时间后,我对上述方法有以下想法:
方法一: 用户将随应用程序一起获得数据库,而不必单独下载。安装因此会更容易。但是,每次有新版本的数据库时,用户都必须重新安装应用程序。另外,如果数据库很大,安装起来会很麻烦。
方法二: 用户必须从网站下载完整的数据库(尽管我可以通过方法 1 提供一个小型的示例版本的数据库)。但是,安装程序会更简单,体积更小。此外,我将能够为那些可能不想要更新版本的应用程序的人轻松提供数据库的 future 版本。
您能否从技术和管理的角度告诉我哪种方法更好,为什么?
如果有第三种或第四种方法比这两种方法更好,请告诉我。
谢谢!
安卓
最佳答案
我为 Android 构建了一个类似的应用程序,它使用来自政府机构的数据定期更新。使用 perl 或类似工具从设备上构建一个与 Android 兼容的数据库并将其从网站下载到手机是相当容易的;这工作得很好,而且用户在下载应用程序时可以获得当前数据。如果您想避免使用主数据存储空间,也应该可以将数据放到 SD 卡上,这对于我的应用程序来说是一个更大的问题,它有一个 ~6Mb 的数据库。
为了让 Android 对数据库感到满意,我相信您必须执行以下操作(我使用 perl 构建我的数据库)。
$st = $db->prepare( "CREATE TABLE \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US')");
$st->execute();
$st = $db->prepare( "INSERT INTO \"android_metadata\" VALUES ('en_US')");
$st->execute();
我有一个更新 Activity ,它检查天气更新是否可用,如果是,则显示“立即更新”屏幕。下载过程如下所示,位于 DatabaseHelperClass 中。
public void downloadUpdate(final Handler handler, final UpdateActivity updateActivity) {
URL url;
try {
close();
File f = new File(getDatabasePath());
if (f.exists()) {
f.delete();
}
getReadableDatabase();
close();
url = new URL("http://yourserver.com/" + currentDbVersion + ".sqlite");
URLConnection urlconn = url.openConnection();
final int contentLength = urlconn.getContentLength();
Log.i(TAG, String.format("Download size %d", contentLength));
handler.post(new Runnable() {
public void run() {
updateActivity.setProgressMax(contentLength);
}
});
InputStream is = urlconn.getInputStream();
// Open the empty db as the output stream
OutputStream os = new FileOutputStream(f);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024 * 1000];
int written = 0;
int length = 0;
while (written < contentLength) {
length = is.read(buffer);
os.write(buffer, 0, length);
written += length;
final int currentprogress = written;
handler.post(new Runnable() {
public void run() {
Log.i(TAG, String.format("progress %d", currentprogress));
updateActivity.setCurrentProgress(currentprogress);
}
});
}
// Close the streams
os.flush();
os.close();
is.close();
Log.i(TAG, "Download complete");
openDatabase();
} catch (Exception e) {
Log.e(TAG, "bad things", e);
}
handler.post(new Runnable() {
public void run() {
updateActivity.refreshState(true);
}
});
}
另请注意,我在数据库文件的文件名中保留了版本号,并在服务器上的文本文件中保留了指向当前版本的指针。
关于database - 提供与 .APK 文件打包在一起的数据库,还是将其单独托管在网站上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1642142/