본문 바로가기

JAVA/Android

안드롤이드 SQLiteOpenHelper 예제 따라하기

- 안드로이드에서 제공하는 내부DB를 이용한 간단한 예제이다.
- DB를 이용해서 insert,delete,update,select를 해보도록 하자.

-Java 소스
public class Ex09_SQLiteActivity extends Activity implements OnClickListener {
 WordDBHelper mHelper;
 EditText mText;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Button btn1 = (Button) this.findViewById(R.id.button1);
  btn1.setOnClickListener(this);
  Button btn2 = (Button) this.findViewById(R.id.button2);
  btn2.setOnClickListener(this);
  Button btn3 = (Button) this.findViewById(R.id.button3);
  btn3.setOnClickListener(this);
  Button btn4 = (Button) this.findViewById(R.id.button4);
  btn4.setOnClickListener(this);

  mHelper = new WordDBHelper(this, "EngDic.db", null, 1);// EngDic로 저장

  mText = (EditText) this.findViewById(R.id.edittext);
 }

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  SQLiteDatabase db;
  ContentValues row; // lecord값
  switch (arg0.getId()) {
  case R.id.button1:// insert
   db = mHelper.getWritableDatabase();
   // SQL
   db.execSQL("INSERT INTO dic VALUES(null, 'boy' , '소녀');");

   /*
    * //insert method row = new ContentValues(); row.put("eng", "boy");
    * row.put("han", "소녀"); db.insert("dic",null,row);
    */
   mHelper.close();
   mText.setText("Insert Success");
   break;
  case R.id.button2:// delete
   db = mHelper.getWritableDatabase();
   db.execSQL("DELETE FROM dic");

   mHelper.close();
   mText.setText("Delete Success");
   break;
  case R.id.button3: // update
   db = mHelper.getWritableDatabase();
   db.execSQL("UPDATE dic SET han='소년' WHERE eng='boy';");

   mHelper.close();
   mText.setText("Upgrade Success");
   break;
  case R.id.button4: // select
   db = mHelper.getReadableDatabase();
   Cursor cursor;

   cursor = db.rawQuery("SELECT eng, hanFROM dic", null); // return이
                 // 있어서
                 // rawQuery로

   String result = "";
   while (cursor.moveToNext()) {
    String eng = cursor.getString(0);
    String han = cursor.getString(1);
    result = result + eng + han + " = " + han + "\n";
   }

   if (result.length() == 0) {
    mText.setText("Empty Set");
   } else {
    mText.setText(result);
   }

   cursor.close();
   mHelper.close();
   break;

  }

 }

}
-------------------------------------------------------------------------------------------------------
public class WordDBHelper extends SQLiteOpenHelper {

 public WordDBHelper(Context context, String name, CursorFactory factory,
   int version) {
  super(context, name, factory, version);
  // TODO Auto-generated constructor stub

 }

 @Override
 public void onCreate(SQLiteDatabase db) {// table 생성
  // TODO Auto-generated method stub
  db.execSQL("CREATE TABLE dic(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
    + "eng TEXT, han TEXT);");

 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { // DB 갱신
  // TODO Auto-generated method stub
  db.execSQL("DROP TABLE");
  onCreate(db);

 }

}

-xml 소스
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

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

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Insert" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Delete" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Upgrade" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="Select" />
    </LinearLayout>

    <EditText
        android:id="@+id/edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

-실행 화면