'안드로이드 SQLiteOpenHelper'에 해당되는 글 2건

  1. 2012.01.17 안드로이드 SQLiteOpenHelper 예제 따라하기 두번째 (1)
  2. 2012.01.16 안드롤이드 SQLiteOpenHelper 예제 따라하기
JAVA/Android2012.01.17 14:09
- DB에 저장된 값을 원하는것만 List로 출력하는 간단한 예제이다.
- SimpleCursorAdapter를 이용해서 listview로 보여지게 된다.

-Java 소스
public class Ex10_SQLite2Activity extends Activity {
    MyDBHelper mDBHelper;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mDBHelper = new MyDBHelper(this, "Today.db", null, 1);
        SQLiteDatabase db = mDBHelper.getWritableDatabase(); //write 다할수있음
        db.execSQL("INSERT INTO today VALUES(null, '점심시간', '2011/07/15', '12:00', '여친과 식사', '1')");
        db.execSQL("INSERT INTO today VALUES(null, '저녁시간', '2011/07/16', '18:00', '여친과 식사', '2')");
        db.execSQL("INSERT INTO today VALUES(null, '아침시간', '2011/07/17', '08:00', '여친과 식사', '3')");
        
        Cursor cursor = db.rawQuery("SELECT _id,title, time, memo FROM today WHERE date = '2011/07/15'", null); 
        // SELECT 에 맞는건만 리스트로 출력
        
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
        R.layout.itemlayout, 
        cursor, 
        new String[] {"title","time","memo"}, 
        new int[] {R.id.text1,R.id.text2,R.id.text3});
        
        ListView list = (ListView)this.findViewById(R.id.listview);
        list.setAdapter(adapter);
        mDBHelper.close();
    }
}
-------------------------------------------------------------------------------------------------------
public class MyDBHelper extends SQLiteOpenHelper {

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

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE today(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "title TEXT, " + "date TEXT , " + "time TEXT, "
+ "memo TEXT, " + "priority INTEGER);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 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:gravity="center"
        android:text="일정관리" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout>
 -------------------------------------------------------------------------------------------------------
<?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:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout> 

-실행 화면

신고
Posted by Namseungil
JAVA/Android2012.01.16 14:44

- 안드로이드에서 제공하는 내부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>

-실행 화면

신고
Posted by Namseungil

티스토리 툴바