티스토리 뷰

ANDROID/android

DBHelper 사용

BAEKNAMU 2016. 10. 12. 17:16
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
 
public class DBHelper extends SQLiteOpenHelper {
 
    public static final String name = "DBHelper.sqlite";
    public static final int DB_VERSION = 2// 개발자가 버전관리
    ArrayList<Data> backUpDatas = new ArrayList<>();
    private static DBHelper dbHelper = null;
    // DB가 없을 때 처음에만 동작함.
    public static SQLiteDatabase openDataBase(Context context){ //생성자
 
        if( dbHelper == null) {
            dbHelper = new DBHelper(context);
        }
        return dbHelper.getWritableDatabase();
    }
    
    
    private DBHelper(Context context) {
        super(context, name, null, DB_VERSION);
    }
 
    @Override // DB version1 : 최초 등록
    public void onCreate(SQLiteDatabase db) {
    // assets 에 있는 파일을 복사해서 디스크로 옮긴다.
    
      // 1-1. DB version1 : 최초 등록
        // TODO 수정
        String scheme_01 = "create table memo(no integer primary key autoincrement not null" +
                ", contents text not null" +
                ", ndate integer not null)";
 
        db.execSQL(scheme_01);
    }
 
    @Override // DB_VERSION 가 상향될 때 마다 호출
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 변경사항이 있으면 디스크에 있는 db 에 덮어쓰기 한다.
        
        Cursor cursor;
        String query;
        Data data = new Data();
 
               if(oldVersion == 1){
             //  예) 컬럼이 3개
 
            query = " select no, contents, ndate from memo order by no";
            cursor = db.rawQuery(query, null);
            while(cursor.moveToNext()){
                data.no = Integer.parseInt(cursor.getString(0));
                data.contents = cursor.getString(1);
                data.ndate = cursor.getString(2);
                backUpDatas.add(data);
            }
        }else if(oldVersion == 2){
            // 예) 컬럼이 4개( image 컬럼이 추가됨)
            query = " select no, contents, ndate,image from memo order by no";
            cursor = db.rawQuery(query, null);
 
            while(cursor.moveToNext()){
                data.no = Integer.parseInt(cursor.getString(0));
                data.contents = cursor.getString(1);
                data.ndate = cursor.getString(2);
                data.image = cursor.getString(3);
                backUpDatas.add(data);
            }
 
        }
        // 테이블 삭제
         String scheme_02_drop = "drop table tableName";
        db.execSQL(scheme_02_drop);
        
        // 테이블 생성
        String scheme_02 = "create table tableName(no integer primary key autoincrement not null" +
                ", contents text not null" + ", image text "+
                ", ndate integer not null)";
 
        db.execSQL(scheme_02);
    }
 
 
   
}
 
cs


'ANDROID > android' 카테고리의 다른 글

이미지 불러오기  (0) 2016.10.13
TraceView를 활용한 Code 퍼포먼스 확인  (0) 2016.10.13
assets 이용한 DB 설정  (0) 2016.10.10
onFragmentInteraction (fragment 구분 확인)  (0) 2016.10.10
Fragment 사용  (0) 2016.10.05
Comments
최근에 올라온 글
최근에 달린 댓글
TAG
more
Total
Today
Yesterday