-
[android] android 데이터 저장과 관리app/android 2022. 1. 17. 18:50반응형
데이터베이스란?
대용량의 데이터 집합을 체계적으로 구성해놓은 것
데이터베이스 관리 시스템
데이터베이스는 여러 사용자나 시스템이 서로 공유할 수 있어야 한다
데이터베이스 관리 시스템(DBMS)
이러한 데이터베이스를 관리해주는 시스템 또는 소프트웨어
관계형 데이터베이스
계층형, 망형, 관계형, 객체지향형, 객체관계형 DBMS 등의 유형 중 실질적으로 가장 많이 사용됨
관계형 데이터베이스의 장단점
장점
업무가 변화할 경우에 다른 DBMS에 비해 변화에 쉽게 순응할 수 있는 구조
유지 및 보수 측면에서도 편리
대용량 데이터 관리와 데이터 무결성을 잘 보장
단점
시스템 자원을 많이 차지해서 시스템이 전방적으로 느려짐
데이터베이스의 기본 개념
데이터 : 하나하나의 단편적인 정보를 뜻함
테이블 : 회원 데이터가 표 형태로 표현된 것
데이터베이스(DB) : 테이블이 저장되는 장소로 주로 원통 모양으로 표현
• 각 데이터베이스는 서로 다른 고유한 이름이 있어야 함
DBMS : 데이터베이스를 관리하는 시스템 또는 소프트웨어를 말함
• 안드로이드에 포함된 SQLite 소프트웨어가 이에 해당
열(칼럼 또는 필드) : 각 테이블은 1개 이상의 열로 구성됨
열 이름 : 각 열을 구분하는 이름, 열 이름은 각 테이블 안에서는 중복되지 않아야 함
데이터 형식 : 열의 데이터 형식을 뜻함
• 테이블을 생성할 때 열 이름과 함께 지정해야 함
행(로우) : 실제 데이터
SQL : 사용자와 DBMS가 소통하기 위한 언어
<실습> 가수 그룹 관리 DB 만들기
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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="fill_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/edtName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="가수 이름을 입력하세요" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="fill_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:hint="가수의 인원을 입력하시오" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/btnInit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="초기화" /> <Button android:id="@+id/btnInsert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="입력" /> <Button android:id="@+id/btnSelect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="조회" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="8" android:orientation="horizontal" > <EditText android:id="@+id/edtNameResult" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="#00FF00" android:padding="20dp" /> <EditText android:id="@+id/edtNumberResult" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="#00FF00" android:padding="20dp" /> </LinearLayout> </LinearLayout>
MainActivity.kt
package com.cookandroid.project12_2 import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) this.title = "가수 그룹 관리 DB" var sqlDB : SQLiteDatabase var myDBHelper = MyDBHelper(this) btnInit.setOnClickListener { sqlDB = myDBHelper.writableDatabase myDBHelper.onUpgrade(sqlDB, 1,2) sqlDB.close() } btnInsert.setOnClickListener { sqlDB = myDBHelper.writableDatabase var name = edtName.text.toString() var number = edtNumber.text.toString().toInt() sqlDB.execSQL("insert into groupTBL values('" + name + "', " + number + ");") sqlDB.close() Toast.makeText(applicationContext, "입력됌", Toast.LENGTH_SHORT).show() edtName.setText("") edtNumber.setText("") } btnSelect.setOnClickListener { sqlDB = myDBHelper.readableDatabase var cursor = sqlDB.rawQuery("select * from groupTBL;", null) var strName = "그룹이름" + "\r\n" + "----------" + "\r\n" var strNumber = "인 원" + "\r\n" + "---------" + "\r\n" while(cursor.moveToNext()) { strName += cursor.getString(0) + "\r\n" strNumber += cursor.getString(1) + "\r\n" } edtNameResult.setText(strName) edtNumberResult.setText(strNumber) cursor.close() sqlDB.close() } } }
MyDBHelper.kt
package com.cookandroid.project12_2 import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper class MyDBHelper(var context: Context) : SQLiteOpenHelper(context, "groupDB", null, 1) { override fun onCreate(p0: SQLiteDatabase?) { var tableCreSql = "create table groupTBL( gName char(20) primary key, gNumber integer);" p0!!.execSQL(tableCreSql) } override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) { p0!!.execSQL("drop table if exists groupTBL") onCreate(p0) } }
실행결과 >>>
반응형'app > android' 카테고리의 다른 글
[android] android 데이터 관리 +CustomerManagement 최종구현 (0) 2022.01.17 [android] android 커스텀 리스트뷰 (custom listView) (0) 2022.01.17 [android] android 어뎁터 뷰 (0) 2022.01.13 [android] android 액티비티와 인텐트 (0) 2022.01.13 [android] android 파일 처리 , 실습 간단 일기장 (0) 2022.01.13