안드로이드 데이터베이스 실습


1. 레이아웃


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.progdv.chap09_sqlite.MainActivity">


<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름: " />

<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="이름 입력" />


<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="주소: " />

<EditText
android:id="@+id/addr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="주소 입력" />
</LinearLayout>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="저장"/>
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="검색"/>

</LinearLayout>

</LinearLayout>

</RelativeLayout>





2. DBHelper 클래스 생성


package com.progdv.chap09_sqlite;

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

public class DBHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "mydb.db"; // 사용할 DB
private static final int DATABASE_VERSION = 1; // DB 버전

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

@Override // 테이블 생성코드
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE myTable (name TEXT, addr TEXT);");
}

@Override // 테이블 삭제코드
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS myTable;");
onCreate(db);
}
}


3. MainActivity 클래스


package com.progdv.chap09_sqlite;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
DBHelper helper;
SQLiteDatabase db;
EditText edit_name, edit_addr;
Button add, search;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

helper= new DBHelper(this);
db= helper.getWritableDatabase();

edit_name= (EditText)findViewById(R.id.name);
edit_addr= (EditText)findViewById(R.id.addr);

add= (Button)findViewById(R.id.add);
search= (Button)findViewById(R.id.search);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name= edit_name.getText().toString();
String addr= edit_addr.getText().toString();

db.execSQL("INSERT INTO myTable VALUES ('"+name+"', '"+addr+"')");

edit_name.setText("");
edit_addr.setText("");
}
});

search = (Button)findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edit_name.getText().toString();
Cursor cursor = db.rawQuery("SELECT name, addr FROM myTable WHERE name='"+name+"';", null);
edit_addr.setText("");
while(cursor.moveToNext()){
String addr = cursor.getString(1);
edit_addr.setText(addr);
}
}
});

}
}



이름과 주소를 입력 후 저장, 이후 이름을 입력 후 검색을 누르면 주소도 자동으로 나온다.








만든 DB파일은 device monitor 창을 띄워 file explorer에서 찾을 수 있다.










Posted by netyhobby
,