안드로이드 개발 질문/답변
(글 수 45,052)
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.database.sqlite.SQLiteDatabase; public class MySQLiteOpenHelper { String savePath = "/mnt/sdcard/firstaid.db"; // 에뮬, 폰 가능하나 추가된 db 검색이 안됨 //String savePath = "/data/data/com.same/databases/firstaid.db"; // 에뮬가능, 폰에서는 강제종료 Context ctx; public SQLiteDatabase createDatabase(){ SQLiteDatabase db; db = SQLiteDatabase.openDatabase(savePath, null, 0 ); if( db == null ){ try{ InputStream myInput = ctx.getAssets().open("firstaid.db"); String outFileName = "first.sqlite"; OutputStream myOutput = new FileOutputStream(savePath); byte[] buffer = new byte[1024]; int length; while((length = myInput.read(buffer)) >0){ myOutput.write(buffer,0,length); } myOutput.flush(); myOutput.close(); myInput.close(); }catch(IOException e){ //throw new Error("Error copying database"); e.printStackTrace(); } db = SQLiteDatabase.openDatabase(savePath, null, 0 ); }//end if return db; }//end createDatabase }//end class
import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class MySQLiteHandler { private static String TABLE_NAME = "first_aid"; Context ctx; MySQLiteOpenHelper helper; SQLiteDatabase db; public MySQLiteHandler(Context ctx) { //helper = new MySQLiteOpenHelper(ctx); //db = helper.createDataBase(); db = new MySQLiteOpenHelper().createDatabase(); } //open public static MySQLiteHandler open(Context ctx) { return new MySQLiteHandler(ctx); } public static void close() { } public long insert(String name, String address, String tel) { return 0; } public void delete() { } public int update(String _id, String name, String address, String tel) { return 0; } //selcet all public Cursor select() { Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null); return c; } //_id 일치 데이터 얻기 public Cursor select(String _child) { Cursor c = db.query(TABLE_NAME, null, "type = ?", new String[]{ _child }, null, null, null); return c; } public Cursor select2(String str) { Cursor c = db.query(TABLE_NAME, new String[]{ "distinct"+ " " + str }, null, null, null, null, null); return c; } public Cursor select_type(String num) { Cursor c = db.query(TABLE_NAME, new String[]{ "type" }, "title_num=?", new String[]{ num }, null, null, null); return c; } public Cursor selcet_double(String str1, String str2) { //Cursor c = db.query("first_aid", null, "title_num=?, type_num=?", new String[]{ str1, str2 }, null, null, null); Cursor c = db.query(TABLE_NAME, null, "title_num=" + "'" + str1 + "'" + "and" + " " + "type_num=" + "'" + str2 + "'", null, null, null, null); return c; } public Cursor select_test(String _id) { Cursor c = db.query(TABLE_NAME, null, "key = ?", new String[]{ _id }, null, null, null); return c; } }
assets 폴더에 db 파일을 넣고
경로를
String savePath = "/mnt/sdcard/firstaid.db"; 로 잡았는데
실행시키면 db를 제대로 불러오지 못해서
혹시나해서 폰 기종을 바꿔 구글 넥서스(맞나..?)로 해보니 제대로 불러왔습니다.
제 핸드폰이 아트릭스인데
아트릭스는 db 저장경로가 다른가요...?
기기마다 SD카드 경로가 조금씩 다릅니다.
http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
를 이용하시면 도움이 될 듯 하네요.