[Android] SQLite 이미 만들어진 DB 정보 불러오기
Android

[Android] SQLite 이미 만들어진 DB 정보 불러오기

728x90

1. assets 폴더에 db파일 넣기

: db생성은 DB Browser 이용

 

 DB Browser로 db생성
assets 폴더에 db삽입

2. SQLiteOpenHelper 클래스를 상속한 DataBaseHelper클래스안에서, assets 안에서 그 db파일을 복사하기

 

[DataBaseHelper class]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.example.yummyfridge;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
import androidx.annotation.Nullable;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
public class DataBaseHelper extends SQLiteOpenHelper{
    private final static String TAG = "DataBaseHelper"// Logcat에 출력할 태그이름
    // database 의 파일 경로
    private static String DB_PATH = "";
    private static String DB_NAME = "recipe.db";
    private SQLiteDatabase mDataBase;
    private Context mContext;
 
    public DataBaseHelper(Context context) {
        super(context,DB_NAME,null,1);
 
 
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        this.mContext = context;
        dataBaseCheck();
    }
 
    private void dataBaseCheck() {
        File dbFile = new File(DB_PATH + DB_NAME);
        if (!dbFile.exists()) {
            dbCopy();
            Log.d(TAG,"Database is copied.");
        }
    }
 
 
    @Override
    public synchronized void close() {
        if (mDataBase != null) {
            mDataBase.close();
        }
        super.close();
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 테이블 구조 생성로직
        Log.d(TAG,"onCreate()");
    }
 
    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        //Toast.makeText(mContext,"onOpen()",Toast.LENGTH_SHORT).show();
        Log.d(TAG,"onOpen() : DB Opening!");
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 테이블 삭제하고 onCreate() 다시 로드시킨다.
        Log.d(TAG,"onUpgrade() : DB Schema Modified and Excuting onCreate()");
    }
 
    // db를 assets에서 복사해온다.
    private void dbCopy() {
 
        try {
            File folder = new File(DB_PATH);
            if (!folder.exists()) {
                folder.mkdir();
            }
 
            InputStream inputStream = mContext.getAssets().open(DB_NAME);
            String out_filename = DB_PATH + DB_NAME;
            OutputStream outputStream = new FileOutputStream(out_filename);
            byte[] mBuffer = new byte[1024];
            int mLength;
            while ((mLength = inputStream.read(mBuffer)) > 0) {
                outputStream.write(mBuffer,0,mLength);
            }
            outputStream.flush();;
            outputStream.close();
            inputStream.close();
 
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("dbCopy","IOException 발생함");
        }
    }
}
 
cs

 

3. DB 내용 불러오기

 

[MainActivity.java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void getVal() {
 
        DataBaseHelper dbHelper = new DataBaseHelper(this);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
 
        Cursor cursor = db.rawQuery("SELECT * FROM recipe_ingredient where RECIPE_ID = 195453",null);
        //" and name = ?",new String[]{"홍길동"});
        while (cursor.moveToNext())
        {
            val += cursor.getString(2)+", ";
 
        }
        sqlresult.setText("재료: "+val);
        cursor.close();
        dbHelper.close();
    }
cs

 

 

[참고]

wonpaper.tistory.com/196

728x90