Android Application
Kotlin 데이터 저장,관리 : SQLite 가수 그룹 관리
작성자 임베디드코리아
작성일 23-10-23 23:23
조회1,691회
댓글0건
Kotlin 데이터 저장,관리 : SQLite 가수 그룹 관리 하기
- 초기를 클릭하면 모든 데이터가 삭제 된다.
- 이름과 인원을 입력하고, 입력을 클릭하면 데이터가 삽인 된다.
- 이름과 인원을 입력하고, 수정을 클릭하면 데이터가 수정 된다.
- 이름을 입력하고, 삭제를 클릭하면, 데이터가 삭제 된다.
- 조회를 클릭하면 모든 데이터를 출력한다.
-------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름 : "
android:textSize="20dp"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/edtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="인원 : "
android:textSize="20dp" />
<EditText
android:id="@+id/edtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/btnInit"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="초기" />
<Button
android:id="@+id/btnInsert"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="입력" />
<Button
android:id="@+id/btnUpdate"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="수정" />
<Button
android:id="@+id/btnDelete"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="삭제" />
<Button
android:id="@+id/btnSelect"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="조회" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:orientation="horizontal" >
<TextView
android:id="@+id/TVNameResult"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#F3E494"
android:padding="20dp" />
<TextView
android:id="@+id/TVNumberResult"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#EDCD9E"
android:padding="20dp" />
</LinearLayout>
</LinearLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
lateinit var myHelper: myDBHelper
lateinit var edtName: EditText
lateinit var edtNumber: EditText
lateinit var edtNameResult: TextView
lateinit var edtNumberResult: TextView
lateinit var btnInit: Button
lateinit var btnInsert: Button
lateinit var btnUpdate: Button
lateinit var btnDelete: Button
lateinit var btnSelect: Button
lateinit var sqlDB: SQLiteDatabase
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar!!.setDisplayShowHomeEnabled(true)
supportActionBar!!.setIcon(R.drawable.firefox)
title = "가수 그룹 DB 관리"
edtName = findViewById<EditText>(R.id.edtName)
edtNumber = findViewById<EditText>(R.id.edtNumber)
edtNameResult = findViewById<TextView>(R.id.TVNameResult)
edtNumberResult = findViewById<TextView>(R.id.TVNumberResult)
btnInit = findViewById<Button>(R.id.btnInit)
btnInsert = findViewById<Button>(R.id.btnInsert)
btnUpdate = findViewById<Button>(R.id.btnUpdate)
btnDelete = findViewById<Button>(R.id.btnDelete)
btnSelect = findViewById<Button>(R.id.btnSelect)
myHelper = myDBHelper(this)
btnInit.setOnClickListener {
sqlDB = myHelper.writableDatabase
myHelper.onUpgrade(sqlDB, 1, 2)
sqlDB.close()
}
btnInsert.setOnClickListener {
sqlDB = myHelper.writableDatabase
sqlDB.execSQL("INSERT INTO groupTBL VALUES ( '"
+ edtName.text.toString() + "' , "
+ edtNumber.text.toString() + ");")
sqlDB.close()
Toast.makeText(applicationContext, "입력 완료 ", Toast.LENGTH_SHORT).show()
btnSelect.callOnClick()
}
btnUpdate.setOnClickListener {
sqlDB = myHelper.writableDatabase
if (edtName.text.toString() !== "") {
sqlDB.execSQL("UPDATE groupTBL SET gNumber ="
+ edtNumber.text + " WHERE gName = '"
+ edtName.text.toString() + "';")
}
sqlDB.close()
Toast.makeText(applicationContext, "수정 완료", Toast.LENGTH_SHORT).show()
btnSelect.callOnClick()
}
btnDelete.setOnClickListener {
sqlDB = myHelper.writableDatabase
if (edtName.text.toString() !== "") {
sqlDB.execSQL("DELETE FROM groupTBL WHERE gName = '"
+ edtName.text.toString() + "';")
}
sqlDB.close()
Toast.makeText(applicationContext, "데이터 삭제 완료.", Toast.LENGTH_SHORT).show()
btnSelect.callOnClick()
}
btnSelect.setOnClickListener {
sqlDB = myHelper.readableDatabase
var cursor: Cursor
cursor = sqlDB.rawQuery("SELECT * FROM groupTBL;", null)
var strNames = " 그룹이름" + "\r\n" + "----------------" + "\r\n"
var strNumbers = " 인원 " + "\r\n" + "--------" + "\r\n"
while (cursor.moveToNext()) {
strNames += cursor.getString(0) + "\r\n"
strNumbers += cursor.getString(1) + "\r\n"
}
edtNameResult.setText(strNames)
edtNumberResult.setText(strNumbers)
cursor.close()
sqlDB.close()
}
edtName.setOnClickListener {
edtName.setText(null)
}
edtNumber.setOnClickListener {
edtNumber.setText(null)
}
}
inner class myDBHelper(context: Context) : SQLiteOpenHelper(context, "groupDB", null, 1) {
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE groupTBL ( gName CHAR(20) PRIMARY KEY, gNumber INTEGER);")
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS groupTBL")
onCreate(db)
}
}
}
------------------------------------------------------------------------------------
themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.SQLiteGroupDB" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#9C27B0</item>
<item name="colorPrimaryDark">#2196F3</item>
</style>
<style name="Theme.SQLiteGroupDB" parent="Base.Theme.SQLiteGroupDB" />
</resources>
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.