현재 assets 폴더에 sqlite 파일을 databases폴더로 복사하여 DB를 사용하고 있습니다.
어플버전을 업데이트하면서 DB파일도 업데이트가 필요 했습니다.
그래서 업데이트버전에 DB파일이름을 다른이름으로 변경해서 복사하여 사용하는 방법으로 했는데 문제가 생겼습니다.
디버그모드로 업데이트하면 이상없이 작동하는데 apk파일로 축출해서 업데이트를 하면 DB가 복사가 안됩니다...
이상하게 구버전을 삭제하고 apk파일로 새로 설치하면 DB복사는 이상없이 됩니다..
덮어쓰기(apk로 업데이트)하면 DB가 복사가 안되고, 새로 설치시는 DB가 복사가 되는거같습니다...
관련 소스 입니다.
public String DBName = "test.sqlite";
private void copyDataBase(Activity act){
AssetManager am = act.getAssets();
File f = new File(
"/data/data/com.test/databases/"+DBName);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try{
InputStream is = am.open(DBName);
BufferedInputStream bis = new BufferedInputStream(is);
if(f.exists()){
is.close();
bis.close();
}else{
File dir = new File("/data/data/com.test/databases/");
if(!dir.mkdirs()){
Log.e("DEBUG","Error..");
}else{
f.createNewFile();
fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos);
int read = -1;
byte[] buffer = new byte[1024];
while((read = bis.read(buffer,0,1024))!=-1){
bos.write(buffer,0,read);
}
bos.flush();
fos.close();
bos.close();
is.close();
bis.close();
}
}
}catch(IOException e){
e.printStackTrace();
}
}
public class DBManager extends SQLiteOpenHelper {
public DBManager(Context context) {
//
super(context, "test.sqlite" ,null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
EX)
버전1 - DBName = test1
버전1.1 - DBName = test1.1
이런식으로 DB파일 이름을 수정했습니다. DBManager부분의 이름도 같이 수정했습니다..
copyDataBase()함수는 메인 Activity onCreate()에서 호출하고 있습니다.
덮어쓰기시 생기는 오류입니다...