我需要在单个.apk中运行时绑定.db和.apk,以便用户可以下载并运行应用程序。我不需要。 db在资产,原始,URL路径中,因为我们需要在运行时更改.db并且我们有这么多用户,对于不同的用户我们有不同的.db。它是基于ERP的应用程序。提前谢谢
答案 0 :(得分:1)
将.db放入/ raw /目录。
然后在运行时将其复制到位于应用程序内存中的新数据库中,然后可以对其进行修改。
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
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();
}
代码不是我的,并且是完整教程的一部分:Using your own DB in Android
如果您希望在/ raw /中放置不同的数据库文件,您可能希望现在使用多个APK,或者在构建APK时使用ant脚本来选择不同的数据库。