๐Ÿ’ก  ๊ฐ„๋‹จํ•œ ๋ฉ”๋ชจ๋ฅผ ์ €์žฅํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ 

 

  ์ด ํ”„๋กœ์ ํŠธ์—์„œ ๋ฉ”๋ชจ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์‚ฌ์šฉํ•  ํ…Œ์ด๋ธ”์„ ๋‹ค์Œ์ฒ˜๋Ÿผ ์„ค๊ณ„

CREATE TABLE memo (
    num INTEGER PRIMARY KEY,
    content TEXT,
    datetime INTEGER
)

 

 -  SQLite ์—์„œ๋Š” INTEGER ํƒ€์ž…์ด primary key ์ด๋ฉด ์ˆซ์ž๊ฐ€ ์ž๋™ ์ฆ๊ฐ€๋จ


1.  SQLiteOpenHelper ์‚ฌ์šฉํ•˜๊ธฐ

  ๐Ÿ‘พ  SQLiteOpenHelper ํด๋ž˜์Šค๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์ฝ”ํ‹€๋ฆฐ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• 

 

1) ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ํ›„ [app] - [java] ๋””๋ ‰ํ† ๋ฆฌ ๋ฐ‘์— ์žˆ๋Š” ํŒจํ‚ค์ง€์— SQLiteHelper ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑ


2) ํด๋ž˜์Šค ์ •์˜ ๋ฐ ์ƒ์„ฑ์ž

 

-  SQLite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด SQLiteOpenHelper ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์•ผ ํ•จ

SQLiteOpenHelper๋Š” ์ƒ์„ฑ ์‹œ์— Context, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช…, ํŒฉํ† ๋ฆฌ, ๋ฒ„์ „ ์ •๋ณด๊ฐ€ ํ•„์š”. ํŒฉํ† ๋ฆฌ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜๋ฏ€๋กœ ๋‚˜๋จธ์ง€ ์„ธ ๊ฐ€์ง€ ์ •๋ณด๋ฅผ ๋‚ด๊ฐ€ ๋งŒ๋“  ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์— ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ •์˜ํ•œ ํ›„์— ์ƒ์†๋ฐ›์€ SQLiteOpenHelper์— ์ „๋‹ฌ.

class SQLiteHelper(context: Context, name: String, version: Int):
    SQLiteOpenHelper(context, name, null, version)  {

 

3) SQLiteOpenHelper ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„

class SQLiteHelper(context: Context, name: String, version: Int):
    SQLiteOpenHelper(context, name, null, version)  {
    override fun onCreate(p0: SQLiteDatabase?) {
        // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ฒ˜์Œ ์ƒ์„ฑ๋  ๋•Œ ํ˜ธ์ถœ
    }

    override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
        // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—…๊ทธ๋ ˆ์ด๋“œ ์‹œ ํ˜ธ์ถœ
    }

 

4) ์•„์ง ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— onCreate() ๋ฉ”์„œ๋“œ์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ

  -  ์ด ๋ฉ”์„œ๋“œ ์•ˆ์— ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹คํ–‰. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉด ๋” ์ด์ƒ ์‹คํ–‰๋˜์ง€ ์•Š์Œ. onCreate() ๋ฉ”์„œ๋“œ ์•ˆ์— ์•ž์—์„œ ๋งŒ๋“  ํ…Œ์ด๋ธ” ์ƒ์„ฑ ์ฟผ๋ฆฌ๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅํ•œ ํ›„, db์˜ execSQL() ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌํ•ด์„œ ์‹คํ–‰

override fun onCreate(p0: SQLiteDatabase?) {
    val sql = "CREATE TABLE `memo` (`num` INTEGER PRIMARY KEY," +
        "`content` TEXT, `datetime` INTEGER)"
        p0?.execSQL(sql)
}

 

5) Memo ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค

 

  -  SQLiteHelper ํด๋ž˜์Šค์˜ ๋ฐ”๊นฅ์— Memo ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ  ์ •์˜

        โžก๏ธ  ์ด ํด๋ž˜์Šค๋Š” SQLite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜์—ฌ ๋ฉ”๋ชจ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ CRUD(์ƒ์„ฑ, ์ฝ๊ธฐ, ์—…๋ฐ์ดํŠธ, ์‚ญ์ œ) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ

data class Memo(var num: Long?, var content: String, var datetime: Long)

 

num๊ณผ datetime์˜ ํƒ€์ž…์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ๋Š” INTEGER๋กœ ์ •์˜ํ–ˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” Long. ์ˆซ์ž์˜ ๋ฒ”์œ„๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ.
ํŠน๋ณ„ํ•œ ์ด์œ ๊ฐ€ ์—†๋‹ค๋ฉด SQLite์—์„œ INTEGER๋กœ ์„ ์–ธํ•œ ๊ฒƒ์€ ์†Œ์Šค ์ฝ”๋“œ์—์„œ๋Š” Long์œผ๋กœ ์‚ฌ์šฉ.
๊ทธ๋ฆฌ๊ณ  num๋งŒ null์„ ํ—ˆ์šฉํ•œ ๊ฒƒ์€ PRIMARY KEY ์˜ต์…˜์œผ๋กœ ๊ฐ’์ด ์ž๋™์œผ๋กœ ์ฆ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์‹œ์—๋Š” ํ•„์š”ํ•˜์ง€ ์•Š์Œ.

 


2. ์‚ฝ์ž… ๋ฉ”์„œ๋“œ

1) SQLiteOpenHelper๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ’์„ ์ž…๋ ฅํ•  ๋•Œ๋Š” ์ฝ”ํ‹€๋ฆฐ์˜ Map ํด๋ž˜์Šค์ฒ˜๋Ÿผ ํ‚ค, ๊ฐ’ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ๋˜๋Š” ContentValues ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉ

 

  ๐Ÿ‘พ  ContentValues์— put("์ปฌ๋Ÿผ๋ช…", ๊ฐ’)์œผ๋กœ ์ €์žฅ

fun insertMemo(memo: Memo) {
    val value = ContentValues()
    value.put("content", memo.content)
    value.put("datetime", memo.datetime)
}

 

2) ์ƒ์†๋ฐ›์€ SQLiteOpenHelper์— ์ด๋ฏธ ๊ตฌํ˜„๋œ writableDatabase์— ํ…Œ์ด๋ธ”๋ช…๊ณผ ํ•จ๊ป˜ ์•ž์—์„œ ์ž‘์„ฑํ•œ ๊ฐ’์„ ์ „๋‹ฌํ•ด์„œ insert()ํ•˜๊ณ , ์‚ฌ์šฉํ•œ ํ›„์—๋Š” close()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ผญ ๋‹ซ์•„์ค˜์•ผ ํ•จ

fun insertMemo(memo: Memo) {
    val value = ContentValues()
    value.put("content", memo.content)
    value.put("datetime", memo.datetime)

    writableDatabase.insert("memo", null, value)
    writableDatabase.close()
}

 

  -  ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ : ํ…Œ์ด๋ธ” ์ด๋ฆ„ / ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ : ๊ฐ’์ด null ์ผ ๋•Œ ์ž…๋ ฅ ๋ฐฉ์ง€ / ์„ธ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ : ์‚ฝ์ž…ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ContentValues ๊ฐ์ฒด


3.  ์กฐํšŒ ๋ฉ”์„œ๋“œ

1)  ์กฐํšŒ ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜ํ™˜๊ฐ’์ด ์žˆ์œผ๋ฏ€๋กœ ๋ฉ”์„œ๋“œ์˜ ๊ฐ€์žฅ ์œ—์ค„์— ๋ฐ˜ํ™˜ํ•  ๊ฐ’์„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๊ณ , ๊ฐ€์žฅ ์•„๋žซ์ค„์— ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ํ›„ ๊ทธ ์‚ฌ์ด์— ๊ตฌํ˜„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ

fun selectMemo(): MutableList<Memo> {
    val list = mutableListOf<Memo>()
    
    return list
}

2) ๋ฉ”๋ชจ์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑ

val sql = "SELECT * FROM memo"

3) ์ฝ๊ธฐ ์ „์šฉ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์Œ

val rd = readableDatabase

 

4) ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ rawQuery() ๋ฉ”์„œ๋“œ์— ์•ž์—์„œ ์ž‘์„ฑํ•ด๋‘” ์ฟผ๋ฆฌ๋ฅผ ๋‹ด์•„์„œ ์‹คํ–‰ํ•˜๋ฉด ์ปค์„œ cursor ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜

 val cursor = rd.rawQuery(sql, null)

 

์ปค์„œ Cursor


๋ฐ์ดํ„ฐ์…‹์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ํ˜„์žฌ ์œ„์น˜๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐ์ดํ„ฐ ์š”์†Œ. ์ปค์„œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜๋œ ๋ฐ์ดํ„ฐ์…‹์„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฐ˜๋ณตํ•˜๋ฉฐ ํ•˜๋‚˜์”ฉ ์ฒ˜
๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ. ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค ์ปค์„œ๊ฐ€ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ์–ด [๋ฐ์ดํ„ฐ ์ฝ๊ธฐ -> ๋‹ค์Œ ์ค„ ์ด๋™]์˜ ๋‹จ์ˆœ ๋กœ์ง์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ.


 

5) ์ปค์„œ์˜ moveToNext() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด ๋‹ค์Œ ์ค„์— ์‚ฌ์šฉํ•  ์ˆ˜์žˆ๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ํ•ด๋‹น ์ปค์„œ๋ฅผ ๋‹ค์Œ ์œ„์น˜๋กœ ์ด๋™. ๋ ˆ์ฝ”๋“œ๊ฐ€ ์—†์œผ๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ. ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ์ฝ์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต.

while(cursor.moveToNext()) { }

 

6) ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ํ…Œ์ด๋ธ”์— ์ •์˜๋œ 3๊ฐœ์˜ ์ปฌ๋Ÿผ์—์„œ ๊ฐ’์„ ๊บผ๋‚ธ ํ›„ ๊ฐ๊ฐ ๋ณ€์ˆ˜์— ๋‹ด์Œ

val num = cursor.getLong(0)
val content = cursor.getString(1)
val datetime = cursor.getLong(2)

 


 

7) ์•ž์—์„œ ๋ณ€์ˆ˜์— ์ €์žฅํ•ด๋‘์—ˆ๋˜ ๊ฐ’๋“ค๋กœ Memo ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•  ๋ชฉ๋ก์— ๋”ํ•จ

list.add(Memo(num, content, datetime))

 

8) while ๋ฌธ์˜ ๋ธ”๋ก ๋ฐ–์—์„œ ์ปค์„œ์™€ ์ฝ๊ธฐ ์ „์šฉ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋ชจ๋‘ ๋‹ซ์•„ ์คŒ

cursor.close()
rd.close()

    fun selectMemo(): MutableList<Memo> {
        val list = mutableListOf<Memo>()
        val sql = "SELECT * FROM memo"
        val rd = readableDatabase

        val cursor = rd.rawQuery(sql, null)
        while(cursor.moveToNext()) {
            val num = cursor.getLong(0)
            val content = cursor.getString(1)
            val datetime = cursor.getLong(2)
            list.add(Memo(num, content, datetime))
        }
        cursor.close()
        rd.close()
        return list
    }

 


4. ์ˆ˜์ • ๋ฉ”์„œ๋“œ

1) INSERT์™€ ๋™์ผํ•˜๊ฒŒ ContentValues๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆ˜์ •ํ•  ๊ฐ’์„ ์ €์žฅ

 fun updateMemo(memo: Memo) {
        val values = ContentValues()
        values.put("content", memo.content)
        values.put("datetime", memo.datetime)
 }

 

2) writableDatabase์˜ update() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์ •ํ•œ ๋‹ค์Œ close()๋ฅผ ํ˜ธ์ถœ


update() ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์ด 4๊ฐœ์ธ๋ฐ (ํ…Œ์ด๋ธ”๋ช…, ์ˆ˜์ •ํ•  ๊ฐ’, ์ˆ˜์ •ํ•  ์กฐ๊ฑด)
์ˆ˜์ •ํ•  ์กฐ๊ฑด์€ PRIMARY KEY๋กœ ์ง€์ •๋œ ์ปฌ๋Ÿผ์„ ์‚ฌ์šฉํ•˜๋ฉฐ ์—ฌ๊ธฐ์—์„œ๋Š” PRIMARY KEY์ธ ์ปฌ๋Ÿผ์ด num์ด๊ธฐ ๋•Œ๋ฌธ์— 'num = ์ˆซ์ž'๊ฐ€ ๋จ.
์„ธ ๋ฒˆ์งธ ๊ฐ’์„ 'num = ?'์˜ ํ˜•ํƒœ๋กœ ์ž…๋ ฅํ•˜๊ณ , ๋„ค ๋ฒˆ์งธ ?์— ๋งคํ•‘ํ•  ๊ฐ’์„ arrayOf("${memo.num}")์˜ ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Œ. ์—ฌ๊ธฐ์„œ๋Š” ์„ธ ๋ฒˆ์งธ์— ์กฐ๊ฑด๊ณผ ๊ฐ’์„ ๋ชจ๋‘ ํ• ๋‹นํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋„ค ๋ฒˆ์งธ์— null์„ ์‚ฌ์šฉ

fun updateMemo(memo: Memo) {
    val values = ContentValues()
    values.put("content", memo.content)
    values.put("datetime", memo.datetime)

    val wd = writableDatabase
    wd.update("memo", values, "num = ${memo.num}", null)
    wd.close()
}

 


5. ์‚ญ์ œ ๋ฉ”์„œ๋“œ

SQLiteOpenHelper ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด insert(), update() ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ๋ฒ•๋งŒ ์•Œ๋ฉด ์ฟผ๋ฆฌ๋ฅผ ๋ชฐ๋ผ๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. ํ•˜์ง€๋งŒ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋” ์ •๋ฐ€ํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฟผ๋ฆฌ๋ฅผ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”. ์‚ญ์ œ ๋ฉ”์„œ๋“œ (DELETE)๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ์ฝ”๋“œ๋กœ ์ž‘์„ฑ.


1) ๋ฐ์ดํ„ฐ ์‚ญ์ œ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑ

  -  ์กฐ๊ฑด์‹์€ '์ปฌ๋Ÿผ๋ช… = ๊ฐ’'์˜ ํ˜•ํƒœ๊ฐ€ ๋จ. ์‚ญ์ œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๋ณ€์ˆ˜์— ์ €์žฅ

 fun deleteMemo(memo: Memo) {
        val sql = "DELETE FROM memo WHERE num=${memo.num}"
 }

 

2) writableDatabase์˜ execSQL() ๋ฉ”์„œ๋“œ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•œ ํ›„ close()๋ฅผ ํ˜ธ์ถœ. execSQL() ๋ฉ”์„œ๋“œ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ

fun deleteMemo(memo: Memo) {
        val sql = "DELETE FROM memo WHERE num=${memo.num}"
        val wd = writableDatabase
        wd.execSQL(sql)
        wd.close()
}

 

 

 

 

 

[ ๋‚ด์šฉ ์ฐธ๊ณ  : IT ํ•™์› ๊ฐ•์˜ ]

+ Recent posts