我有一个数据库,我想在下次更新我的应用程序时更改该数据库..但我不想丢失数据库中的当前数据(在应用程序目录中)..我必须将该数据复制到新数据库并删除旧数据库..我怎样才能做到这一点?有没有其他想法让我知道...
提前致谢..
这是我当前的数据库代码...
public class DbUtils {
public static String DB_PATH;
private String DB_NAME;
File dbDir;
private SQLiteDatabase dataBase;
public DbUtils(File fileDirectory, String sqliteFileName) {
this.DB_NAME = sqliteFileName;
dbDir = fileDirectory;
}
public void createDatabaseIfNotExists(Context context) throws IOException {
boolean createDb = false;
File dbFile = new File(dbDir.getAbsolutePath() + "/" + DB_NAME);
DB_PATH = dbFile.getAbsolutePath();
if (!dbDir.exists()) {
dbDir.mkdir();
createDb = true;
} else if (!dbFile.exists()) {
createDb = true;
} else {
boolean doUpgrade = false;
if (doUpgrade) {
dbFile.delete();
createDb = true;
}
}
if (createDb) {
InputStream myInput = context.getAssets().open(DB_NAME);
OutputStream myOutput = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
}
public SQLiteDatabase getStaticDb() {
return dataBase = SQLiteDatabase.openDatabase(DB_PATH, null,
SQLiteDatabase.OPEN_READWRITE);
}
public void closeDataBase(){
if(dataBase!=null && dataBase.isOpen()){
dataBase.close();
}
}
}