안드로이드 내장 데이터 베이스
SQLite 데이터 베이스를 사용하기 위해서는 SQLiteOpenHelper를 상속받는 클래스 필요
class DBHelper(context:Context) : SQLiteOpenHelper(context, "Test.db", null, 1) { }
create table 테이블 이름
(컬럼이름 자료형 제약조건)
override fun onCreate(sqliteDatabase: SQLiteDatabase?) {
// 테이블 구조 정의
val sql = """create table TestTable
(idx integer primary key autoincrement,
textData text not null,
intData integer not null,
doubleData real not null,
dateData date not null)
""".trimIndent()
// 쿼리문 수행
sqliteDatabase?.execSQL(sql)
}
override fun onUpgrade(sqliteDatabase: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
TODO("Not yet implemented")
}
class DBHelper(context:Context) : SQLiteOpenHelper(context, "Test.db", null, 1) {
override fun onCreate(sqliteDatabase: SQLiteDatabase?) {
val sql = """create table TestTable
(idx integer primary key autoincrement,
textData text not null,
intData integer not null,
doubleData real not null,
dateData date not null)
""".trimIndent()
sqliteDatabase?.execSQL(sql)
}
override fun onUpgrade(sqliteDatabase: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
TODO("Not yet implemented")
}
}
fun insertData(context: Context, data:TestClass ){
val contentValues = ContentValues()
contentValues.put("textData", data.textData)
contentValues.put("intData", data.intData)
contentValues.put("doubleData", data.doubleData)
contentValues.put("dateData", data.dateData)
val dbHelper = DBHelper(context)
dbHelper.writableDatabase.insert("TestTable", null, contentValues)
dbHelper.close()
}
fun selectData(context: Context, idx:Int):TestClass{
val dbHelper = DBHelper(context)
val selection = "idx = ?"
val args = arrayOf("$idx")
val cursor = dbHelper.writableDatabase.query("TestTable", null, selection, args, null, null, null)
cursor.moveToNext()
// 컬럼의 이름을 지정하여 컬럼의 순서값을 가져오기
val idx1 = cursor.getColumnIndex("idx")
val idx2 = cursor.getColumnIndex("textData")
val idx3 = cursor.getColumnIndex("intData")
val idx4 = cursor.getColumnIndex("doubleData")
val idx5 = cursor.getColumnIndex("dateData")
val idx = cursor.getInt(idx1)
val textData = cursor.getString(idx2)
val intData = cursor.getInt(idx3)
val doubleData = cursor.getDouble(idx4)
val dateData = cursor.getString(idx5)
val testClass = TestClass(idx, textData, intData, doubleData, dateData)
dbHelper.close()
return testClass
}
fun selectAllData(context: Context):MutableList<TestClass>{
val dbHelper = DBHelper(context)
val cursor = dbHelper.writableDatabase.query("TestTable", null, null, null, null, null, null)
val dataList = mutableListOf<TestClass>()
while(cursor.moveToNext()){
// 컬럼의 이름을 지정하여 컬럼의 순서값을 가져오기
val idx1 = cursor.getColumnIndex("idx")
val idx2 = cursor.getColumnIndex("textData")
val idx3 = cursor.getColumnIndex("intData")
val idx4 = cursor.getColumnIndex("doubleData")
val idx5 = cursor.getColumnIndex("dateData")
val idx = cursor.getInt(idx1)
val textData = cursor.getString(idx2)
val intData = cursor.getInt(idx3)
val doubleData = cursor.getDouble(idx4)
val dateData = cursor.getString(idx5)
val testClass = TestClass(idx, textData, intData, doubleData, dateData)
dataList.add(testClass)
}
dbHelper.close()
return dataList
}
fun updateData(context:Context, obj:TestClass){
contentValues.put("textData", obj.textData)
contentValues.put("intData", obj.intData)
contentValues.put("doubleData", obj.doubleData)
contentValues.put("dateData", obj.dateData)
val condition = "idx = ?"
// ?에 들어갈 값
val args = arrayOf("${obj.idx}")
val dbHelper = DBHelper(context)
dbHelper.writableDatabase.update("TestTable", contentValues, condition, args)
dbHelper.close()
}
fun deleteData(context:Context, idx:Int){
val condition = "idx = ?"
// ?에 들어갈 값
val args = arrayOf("$idx")
val dbHelper = DBHelper(context)
dbHelper.writableDatabase.delete("TestTable", condition, args)
dbHelper.close()
}