gpt4 book ai didi

java - 来自 Internet 的 SQLite 数据库

转载 作者:行者123 更新时间:2023-11-30 03:37:56 25 4
gpt4 key购买 nike

我的应用附带了一个相当大的数据库。我很难让它在本地设备上正确创建自己,所以我想出了问题,而且它太大了,将它托管在服务器上并从那里使用它可能更有意义。

数据库不超过 40 MB。将其托管在某处的最佳管理方式是什么?

最佳答案

问题出在数据库文件 大小上。您可以对 Database 文件制作一个 Zip,然后将其从 Assets 复制到您的本地路径。

这是链接:Copy Db File From Assets

现在,将该代码中的 copyDataBase() 函数替换为以下函数。

private void copyDataBase() throws IOException {
try {
InputStream mInputStream = mContext.getAssets().open(DB_NAME_ZIP);
String outFileName = DB_PATH + DB_NAME_ZIP;
OutputStream mOutputStream = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = mInputStream.read(buffer)) > 0) {
mOutputStream.write(buffer, 0, length);
}

ZipFile mZipFile = new ZipFile(DB_PATH + DB_NAME_ZIP);
InputStream nInputStream = mZipFile.getInputStream(mZipFile.getEntry(DB_NAME));
OutputStream nOutputStream = new FileOutputStream(DB_PATH + DB_NAME);
while ((length = nInputStream.read(buffer)) > 0) {
nOutputStream.write(buffer, 0, length);
}
nOutputStream.flush();
nOutputStream.close();
nInputStream.close();

// Close the streams
mOutputStream.flush();
mOutputStream.close();
mInputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
//Delete Zip file to minimize memory usage
final String mPath = DB_PATH + DB_NAME_ZIP;
final File file = new File(mPath);
if (file.exists())
file.delete();
}
}
  • 这里的 DB_NAME_ZIP 是你放在 Assets 文件夹中的 Database Zip 文件,比如 Android.zip 但它实际上包含Android.sqliteAndroid.db

希望这篇文章能帮到你。

关于java - 来自 Internet 的 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16328785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com