[안드로이드] assets 폴더안에 db 파일 자신 앱에 복사하기

· 13년 전 · 8751
이거 고대로 사용하시면 됩니다. main oncreate 에서 저 클래스 한번만 불러와서 쓰시면 알아서 복사가 됩니다.

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

 

static String NAME = "testdb.sqlite";

static CursorFactory FACTORY = null;

static String PACKEGE = "com.example.isbnapp";

static String DB = "isbn.db";

static int VERSION  = 1;

public MySQLiteOpenHelper(Context context) {

super(context, NAME, FACTORY, VERSION);

// TODO Auto-generated constructor stub

try {

boolean bResult = isCheckDB(context);  // DB가 있는지?

Log.i("MiniApp", "DB Check="+bResult);

if(!bResult){   // DB가 없으면 복사

copyDB(context);

}else{

 

}

} catch (Exception e) {

 

}

}

// DB가 있나 체크하기

public boolean isCheckDB(Context mContext){

String filePath = "/data/data/" + PACKEGE + "/databases/" + DB;

File file = new File(filePath);

 

if (file.exists()) {

return true;

}

 

return false;

 

}

// DB를 복사하기

// assets의 /db/xxxx.db 파일을 설치된 프로그램의 내부 DB공간으로 복사하기

public void copyDB(Context mContext){

Log.d("MiniApp", "copyDB");

AssetManager manager = mContext.getAssets();

String folderPath = "/data/data/" + PACKEGE + "/databases";

String filePath = "/data/data/" + PACKEGE + "/databases/" +DB;

File folder = new File(folderPath);

File file = new File(filePath);

 

FileOutputStream fos = null;

BufferedOutputStream bos = null;

try {

InputStream is = manager.open("db/" + DB);

BufferedInputStream bis = new BufferedInputStream(is);

 

if (folder.exists()) {

}else{

folder.mkdirs();

}

 

 

if (file.exists()) {

file.delete();

file.createNewFile();

}

 

fos = new FileOutputStream(file);

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();

 

bos.close();

fos.close();

bis.close();

is.close();

 

} catch (IOException e) {

Log.e("ErrorMessage : ", e.getMessage());

} 

}

/** Called when the activity is first created. */

@Override

public void onCreate(SQLiteDatabase db) {

// String QUERY = "CREATE TABLE word (_id INTEGER PRIMARY KEY autoincrement, word_e TEXT , word_k TEXT)";

// db.execSQL(QUERY);

Log.e("ehsk", "eee");

 

//        String QUERY1 = "INSERT INTO word (word_e, word_k ) VALUES(apple , 사과)";

//        db.execSQL(QUERY1);

}

 

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// TODO Auto-generated method stub

String QUERY = "DROP TABLE IF EXISTS word";

db.execSQL(QUERY);

onCreate(db);

 

 

}

}

|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 2,713
13년 전 조회 2,686
13년 전 조회 3,431
13년 전 조회 1,405
13년 전 조회 2,497
13년 전 조회 1,569
13년 전 조회 2,585
13년 전 조회 3,486
13년 전 조회 2,054
13년 전 조회 4,689
13년 전 조회 1.1만
13년 전 조회 1,950
13년 전 조회 3,321
13년 전 조회 2,943
13년 전 조회 3,867
13년 전 조회 6,856
13년 전 조회 3,512
13년 전 조회 3,831
13년 전 조회 1,792
13년 전 조회 1,568
13년 전 조회 2,673
13년 전 조회 6,673
13년 전 조회 2,291
13년 전 조회 5,530
13년 전 조회 1,712
13년 전 조회 4,929
13년 전 조회 2,095
13년 전 조회 1,899
13년 전 조회 2,453
13년 전 조회 2,085
13년 전 조회 1,351
13년 전 조회 1,327
13년 전 조회 2.1만
13년 전 조회 1,296
13년 전 조회 1,860
13년 전 조회 1,886
13년 전 조회 2,108
13년 전 조회 4,253
13년 전 조회 1,473
13년 전 조회 3,007
13년 전 조회 5,670
13년 전 조회 1,452
13년 전 조회 4,644
13년 전 조회 1,847
13년 전 조회 4,564
13년 전 조회 1,677
13년 전 조회 2,537
13년 전 조회 1,736
13년 전 조회 2,955
13년 전 조회 8,752
13년 전 조회 2,739
13년 전 조회 2,532
13년 전 조회 2,338
13년 전 조회 1,359
13년 전 조회 2,903
13년 전 조회 2,654
13년 전 조회 1,401
13년 전 조회 1,984
13년 전 조회 1,385
13년 전 조회 1,806
13년 전 조회 2,000
13년 전 조회 4,376
13년 전 조회 2,909
13년 전 조회 3,078
13년 전 조회 4,449
13년 전 조회 4,204
13년 전 조회 3,872
13년 전 조회 4,731
13년 전 조회 1,772
13년 전 조회 6,718
13년 전 조회 2,078
13년 전 조회 1,945
13년 전 조회 4,155
13년 전 조회 3,796
13년 전 조회 5,963
13년 전 조회 3,495
13년 전 조회 4,142
13년 전 조회 1,827
13년 전 조회 1,630
13년 전 조회 2,840
13년 전 조회 1,594
13년 전 조회 2,976
13년 전 조회 2,312
13년 전 조회 1,892
13년 전 조회 2,907
13년 전 조회 3,044
13년 전 조회 2,125
13년 전 조회 1,615
13년 전 조회 2,145
13년 전 조회 1,645
13년 전 조회 2,044
13년 전 조회 4,654
13년 전 조회 4,735
13년 전 조회 2,173
13년 전 조회 7,690
13년 전 조회 8,357
13년 전 조회 3,052
13년 전 조회 4,619
13년 전 조회 1,419
13년 전 조회 4,467