티스토리 뷰

안드로이는 SQLite database 를 모두 지원한다. 생성한 어떤 DB도 어플리케이션 안에서는 이름을 가지고 어떤 클래스에도 접근가능하지만, 어플리케이션 밖에서는 불가능하다.

Android provides full support for SQLite databases. Any databases you create will be accessible by name to any class in the application, but not outside the application.

새 SQLite database를 만드는 권장하는 방법은 SQLiteOpenHelper 의 서브 클래스를 생성하고 onCreate() 메소드를 오버라이드하고 그 곳에서 관련명령을 실행한다.

The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method, in which you can execute a SQLite command to create tables in the database.

  • SQLiteOpenHelper 의 Subclass 구현하기
  • database 쓰기는 getWritableDatabase() 사용
  • database 읽기는 getReadableDatabase() 사용
  • SQLiteDatabase query() methods를 이용하여 sQLite 쿼리 실행하기

SQLiteOpenHelper 의 Subclass 구현하기


public class MyDBHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "mytable";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("테이블 생성 쿼리문");
    }
}

댓글