我认为这是一个相当简单的问题。我对 Android 的东西还太年轻。我想准备将使用数据库的应用程序。在我展示的每个示例中,都有一个空数据库,其中首先启动应用程序,然后进行一些插入。我希望应用程序具有相当大的数据库,因此我希望在应用程序启动时填充数据库。如何准备数据库并将其附加到程序?
将您填写的数据库放入 Package 的 Asset 目录中,
在应用程序运行时只需将该数据库复制到应用程序的内部存储,例如data/data/<package name>/database
目录。
- 然后使用它。
编辑:这用于将数据库从资产目录复制到数据库目录,
private void copyDataBase() throws IOException {
try {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open("your Database file name");
// Path to the just created empty db
String outFileName = "/data/data/<package name>/databases/";
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("error", e.toString());
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)