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

· 13년 전 · 9521
이거 고대로 사용하시면 됩니다. 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년 전 조회 3,457
13년 전 조회 3,426
13년 전 조회 4,180
13년 전 조회 2,181
13년 전 조회 3,279
13년 전 조회 2,113
13년 전 조회 3,341
13년 전 조회 4,253
13년 전 조회 2,562
13년 전 조회 5,472
13년 전 조회 1.2만
13년 전 조회 2,701
13년 전 조회 4,085
13년 전 조회 3,704
13년 전 조회 4,352
13년 전 조회 7,640
13년 전 조회 3,984
13년 전 조회 4,591
13년 전 조회 2,611
13년 전 조회 2,336
13년 전 조회 3,447
13년 전 조회 7,470
13년 전 조회 3,040
13년 전 조회 6,332
13년 전 조회 2,242
13년 전 조회 5,687
13년 전 조회 2,843
13년 전 조회 2,677
13년 전 조회 2,939
13년 전 조회 2,877
13년 전 조회 2,126
13년 전 조회 2,098
13년 전 조회 2.2만
13년 전 조회 2,058
13년 전 조회 2,653
13년 전 조회 2,633
13년 전 조회 2,888
13년 전 조회 5,064
13년 전 조회 2,281
13년 전 조회 3,770
13년 전 조회 6,400
13년 전 조회 2,239
13년 전 조회 5,432
13년 전 조회 2,607
13년 전 조회 5,373
13년 전 조회 2,478
13년 전 조회 3,358
13년 전 조회 2,463
13년 전 조회 3,718
13년 전 조회 9,522
13년 전 조회 3,522
13년 전 조회 3,306
13년 전 조회 3,148
13년 전 조회 2,153
13년 전 조회 3,661
13년 전 조회 3,460
13년 전 조회 2,209
13년 전 조회 2,748
13년 전 조회 2,191
13년 전 조회 2,279
13년 전 조회 2,770
13년 전 조회 5,136
13년 전 조회 3,655
13년 전 조회 3,872
13년 전 조회 5,240
13년 전 조회 5,004
13년 전 조회 4,682
13년 전 조회 5,571
13년 전 조회 2,527
13년 전 조회 7,493
13년 전 조회 2,877
13년 전 조회 2,727
13년 전 조회 4,912
13년 전 조회 4,610
13년 전 조회 6,722
13년 전 조회 4,265
13년 전 조회 4,935
13년 전 조회 2,616
13년 전 조회 2,413
13년 전 조회 3,608
13년 전 조회 2,385
13년 전 조회 3,754
13년 전 조회 3,070
13년 전 조회 2,699
13년 전 조회 3,659
13년 전 조회 3,587
13년 전 조회 2,867
13년 전 조회 2,381
13년 전 조회 2,933
13년 전 조회 2,417
13년 전 조회 2,766
13년 전 조회 5,449
13년 전 조회 5,559
13년 전 조회 2,948
13년 전 조회 8,462
13년 전 조회 9,137
13년 전 조회 3,852
13년 전 조회 5,387
13년 전 조회 2,175
13년 전 조회 5,221