Android

안드로이드 SQLite 사용을 위한 SQLiteOpenHelper 커스터마이징

■안드로이드 SQLite를 사용하기 위해  SQLiteOpenHelper를 커스터마이징 한다.

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class FcmSQLiteOpenHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "test_sample.db";
    private static final int DATABASE_VERSION = 7;

    private static final String SQL_DELETE_TB_LOTTO_WINNER_NUMBER =  "DROP TABLE IF EXISTS TB_LOTTO_WINNER_NUMBER";
    private static final String SQL_DELETE_TB_LOTTO_MY_NUMBER =  "DROP TABLE IF EXISTS TB_LOTTO_MY_NUMBER";

    public static final String TB_LOTTO_MY_NUMBER       = "TB_LOTTO_MY_NUMBER";
    public static final String COLUMN_ADD_TEST     = "_reg_type4";

    private static final String DATABASE_ALTER_TB_LOTTO_MY_NUMBER_1 = "ALTER TABLE "
            + TB_LOTTO_MY_NUMBER + " ADD COLUMN " + COLUMN_ADD_TEST + " integer;";

    private static final String DATABASE_ALTER_TB_LOTTO_MY_NUMBER_2 = "ALTER TABLE "
            + TB_LOTTO_MY_NUMBER + " ADD COLUMN " + COLUMN_ADD_TEST + " string;";

    public FcmSQLiteOpenHelper(@Nullable Context context, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, version);
    }

    public FcmSQLiteOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public FcmSQLiteOpenHelper(Context context, SQLiteDatabase.CursorFactory factory) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //테이블 생성 스크립트
        DataBaseUtil.createTablehistory(db); //DATABASE_CREATE
        DataBaseUtil.createTableMyNumber(db); //DATABASE_CREATE
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //https://thebhwgroup.com/blog/how-android-sqlite-onupgrade

        //기존 테이블 삭제 후 신규 처리방법 - 기존 데이터 다 삭제됨
        //db.execSQL(SQL_DELETE_TB_LOTTO_WINNER_NUMBER);
        //db.execSQL(SQL_DELETE_TB_LOTTO_MY_NUMBER);
        //onCreate(db);

        //칼럼을 추가하는 방법
//        if (oldVersion < newVersion) {
//            db.execSQL(DATABASE_ALTER_TB_LOTTO_MY_NUMBER_1);
//        }

    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        db.disableWriteAheadLogging();
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }

}

데이터 베이스 초기화 방법

public FcmSQLiteOpenHelper dbHelper;
private SQLiteDatabase database;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
	//데이터베이스 초기화
    dbHelper = new FcmSQLiteOpenHelper(getActivity());
    database = dbHelper.getWritableDatabase();
    
}

 

안드로이드 SQLite 테이블 생성 방법은 아래 링크를 참고하자
안드로이드 SQLite 테이블 생성 방법

 

 

Leave a Reply

error: Content is protected !!