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

· 13년 전 · 8103
이거 고대로 사용하시면 됩니다. 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);

 

 

}

}

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

프로그램

+
제목 글쓴이 날짜 조회
12년 전 조회 2,071
12년 전 조회 2,018
12년 전 조회 2,770
12년 전 조회 759
12년 전 조회 1,855
12년 전 조회 951
12년 전 조회 1,926
12년 전 조회 2,845
12년 전 조회 1,406
12년 전 조회 4,041
12년 전 조회 1만
12년 전 조회 1,279
12년 전 조회 2,605
12년 전 조회 2,292
12년 전 조회 3,151
12년 전 조회 6,182
12년 전 조회 2,801
12년 전 조회 3,139
12년 전 조회 1,137
12년 전 조회 922
12년 전 조회 2,020
12년 전 조회 6,032
12년 전 조회 1,635
12년 전 조회 4,864
12년 전 조회 1,101
12년 전 조회 4,269
12년 전 조회 1,412
12년 전 조회 1,254
12년 전 조회 1,842
12년 전 조회 1,467
12년 전 조회 739
12년 전 조회 702
13년 전 조회 2.1만
13년 전 조회 669
13년 전 조회 1,235
13년 전 조회 1,243
13년 전 조회 1,479
13년 전 조회 3,642
13년 전 조회 859
13년 전 조회 2,369
13년 전 조회 5,057
13년 전 조회 822
13년 전 조회 3,996
13년 전 조회 1,193
13년 전 조회 3,928
13년 전 조회 1,064
13년 전 조회 1,910
13년 전 조회 1,075
13년 전 조회 2,306
13년 전 조회 8,104
13년 전 조회 2,081
13년 전 조회 1,873
13년 전 조회 1,698
13년 전 조회 756
13년 전 조회 2,266
13년 전 조회 2,004
13년 전 조회 788
13년 전 조회 1,361
13년 전 조회 782
13년 전 조회 1,070
13년 전 조회 1,351
13년 전 조회 3,735
13년 전 조회 2,273
13년 전 조회 2,453
13년 전 조회 3,824
13년 전 조회 3,634
13년 전 조회 3,295
13년 전 조회 4,166
13년 전 조회 1,183
13년 전 조회 6,136
13년 전 조회 1,530
13년 전 조회 1,382
13년 전 조회 3,566
13년 전 조회 3,185
13년 전 조회 5,342
13년 전 조회 2,857
13년 전 조회 3,492
13년 전 조회 1,193
13년 전 조회 998
13년 전 조회 2,179
13년 전 조회 963
13년 전 조회 2,372
13년 전 조회 1,673
13년 전 조회 1,273
13년 전 조회 2,261
13년 전 조회 2,465
13년 전 조회 1,477
13년 전 조회 967
13년 전 조회 1,496
13년 전 조회 991
13년 전 조회 1,373
13년 전 조회 4,022
13년 전 조회 4,107
13년 전 조회 1,528
13년 전 조회 7,100
13년 전 조회 7,697
13년 전 조회 2,454
13년 전 조회 4,018
13년 전 조회 790
13년 전 조회 3,813
🐛 버그신고