SQLite는 MySQL과 같은 데이터베이스 관리 시스템으로 안드로이드 운영체제에서 기본적으로 제공한다. 그러나 DB를 위한 서버를 따로 두지않고 애플리케이션에 파일 구조로 넣어서 사용하며, 무게가 가벼워 간편하게 사용하기 좋다. 또한 SQLite API의 메소드 호출을 통해 비교적 간단하게 사용할 수 있다.


1. DB생성, Table생성 및 Data 추가, 제거


  • DB 생성 및 삭제 메소드
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory)
public boolean deleteDatabase(String name)

DB생성 메소드의 첫 번째 파라미터는 DB의 이름, 두 번째는 사용 모드이다. 사용 모드에 따라 앱에서 데이터베이스로의 접근 권한이나 읽기,쓰기 권한 등을 설정할 수 있다. 세 번째 파라미터는 데이터베이스 커서를 만들어낼 객체를 전달할 수 있는데, 일반적인 안드로이드 개발에서 커서 팩토리를 새로 작성해서 사용할 일은 거의 없으므로 대부분 null을 전달한다.


  • Table 생성 query문 실행
String sql = "create table employee(_id integer PRIMARY KEY autoincrement, name text, age integer);";
db.execSQL(sql); //sql문 실행

테이블을 생성하기 위한 SQL문을 문자열 형태로 작성하고 데이터베이스의 execSQL() 메소드에 전달해서 실행하면 된다. 위 예시에서는 employee라는 테이블을 만들었는데 이 테이블은 id, 이름, 나이 3개의 column을 가지고 있다. 또한 id값은 데이터가 추가될 때 마다 자동으로 1씩 증가하도록 한 것이다.


  • 데이터 추가 및 삭제
//데이터 추가
String sql = "insert into employee(name, age) values('John', 20);";
db.execSQL(sql);

//데이터 삭제
String[] whereArgs = {"John", "Kelly"};
int rowAffected = db.delete(tableName, "name = ?", whereArgs);

데이터 추가는 일반 SQL문을 작성하고 데이터베이스의 execSQL() 메소드 호출을 통해 실행해주면 된다. 또한 데이터 삭제는 데이터베이스의 'delete()` 메소드 호출을 통해 실행하는데, 이 때 2번째 파라미터의 ?를 대체할 문자열로 3번째 파라미터인 whereArgs배열을 같이 전달해줄 수 있다. 마지막으로 영향을 받은, 즉 삭제된 데이터의 갯수를 return한다.


2. 예제


먼저 앱의 전체적인 화면은 아래의 구조로 만들 것이다.

1


이를 위해 아래와 같이 XML 코드를 작성한다.

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

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

        <EditText
            android:id="@+id/dbNameInput"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="데이터베이스 이름"
            android:textSize="16dp"
            android:layout_weight="3"
            android:layout_margin="5dp"/>

        <Button
            android:id="@+id/createDbBtn"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="DB 생성"
            android:layout_margin="5dp"/>

    </LinearLayout>

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

        <EditText
            android:id="@+id/tableNameInput"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="테이블 이름"
            android:textSize="16dp"
            android:layout_weight="3"
            android:layout_margin="5dp"/>

        <Button
            android:id="@+id/createTableBtn"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="테이블 생성"
            android:layout_margin="5dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/nameToAddInput"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="14dp"
            android:layout_marginHorizontal="3dp"
            android:hint="이름"/>

        <EditText
            android:id="@+id/ageToAddInput"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="14dp"
            android:layout_marginHorizontal="3dp"
            android:hint="나이"/>

        <Button
            android:id="@+id/addBtn"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:text="추가"
            android:layout_margin="5dp"/>

    </LinearLayout>

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

        <EditText
            android:id="@+id/nameToDelInput"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="삭제할 사람 이름"
            android:textSize="16dp"
            android:layout_weight="3"
            android:layout_margin="5dp"/>

        <Button
            android:id="@+id/delBtn"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="삭제"
            android:layout_margin="5dp"/>

    </LinearLayout>

    <Button
        android:id="@+id/dataViewBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="조회"
        android:textSize="14dp"
        android:layout_gravity="center_horizontal"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/status"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:textSize="16dp"
            android:background="#33000000"/>

    </ScrollView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/dataView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:textSize="16dp"
            android:background="#33000000"/>

    </ScrollView>

</LinearLayout>


실행코드를 위한 코드를 아래와 같이 작성한다.

public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase db;

    EditText dbNameInput;
    EditText tableNameInput;
    EditText nameToDelInput;
    EditText nameToAddInput;
    EditText ageToAddInput;

    TextView status;
    TextView dataView;

    String dbName;
    String tableName;

    StringBuilder sb_status;
    StringBuilder sb_data;

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

        /* ********** XML에 정의된 UI객체 참조 ************ */
        dbNameInput = findViewById(R.id.dbNameInput);
        tableNameInput = findViewById(R.id.tableNameInput);

        nameToAddInput = findViewById(R.id.nameToAddInput);
        ageToAddInput = findViewById(R.id.ageToAddInput);
        nameToDelInput = findViewById(R.id.nameToDelInput);

        status = findViewById(R.id.status);
        dataView = findViewById(R.id.dataView);

        Button createDbBtn = findViewById(R.id.createDbBtn);
        Button createTableBtn = findViewById(R.id.createTableBtn);
        Button delBtn = findViewById(R.id.delBtn);
        Button addBtn = findViewById(R.id.addBtn);
        Button dataViewBtn = findViewById(R.id.dataViewBtn);
        /* ************************************************* */

        sb_status = new StringBuilder(); // 상태 메시지를 나타내기 위한 스트링빌더 객체 생성

        /* 데이터베이스 생성 버튼 클릭 이벤트 */
        createDbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /* 데이터베이스 생성 */
                dbName = dbNameInput.getText().toString();
                db = openOrCreateDatabase(dbName, MODE_ENABLE_WRITE_AHEAD_LOGGING, null);

                sb_status.append("데이터베이스 " + dbName + "가 생성되었습니다.\n"); //상태 메세지 추가
                status.setText(sb_status.toString());
            }
        });

        /* 테이블 생성 버튼 클릭 이벤트 */
        createTableBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    tableName = tableNameInput.getText().toString();
                    /* 테이블 생성을 위한 SQL문 작성 */
                    String sql = "create table " + tableName + "(_id integer PRIMARY KEY autoincrement, name text, age integer);";
                    db.execSQL(sql); //sql문 실행
                }catch(Exception e){
                    e.printStackTrace();
                }

                sb_status.append("테이블 " + tableName + "가 생성되었습니다.\n");
                status.setText(sb_status.toString());
            }
        });

        /* 데이터 추가 버튼 클릭 이벤트 */
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameToAddInput.getText().toString();
                int age = Integer.parseInt(ageToAddInput.getText().toString());
                /* 테이블에 데이터 추가하기를 위한 SQL문 작성 */
                String sql = "insert into " + tableName + "(name, age) values(" + "'"+name+"'" + ", " + age + ");";
                db.execSQL(sql); //sql문 실행

                sb_status.append("데이터가 추가되었습니다.(이름: "+name+", 나이: "+age+"\n");
                status.setText(sb_status.toString());
            }
        });

        /* 데이터 삭제 버튼 클릭 이벤트 */
        delBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameToDelInput.getText().toString();
                String[] whereArgs = {name};
                int rowAffected = db.delete(tableName, "name = ?", whereArgs);

                sb_status.append("이름이 "+name+" 인 데이터가 모두 삭제되었습니다.(삭제된 데이터 수: "+rowAffected+")\n");
                status.setText(sb_status.toString());
            }
        });

        /* 데이터 조회 버튼 클릭 이벤트 */
        dataViewBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /* 테이블의 이름과 나이 column을 가르키는 커서 객체 생성 */
                Cursor cursor = db.rawQuery("SELECT name, age FROM " + tableName, null);
                int count = cursor.getCount(); //데이터의 갯수

                sb_data = new StringBuilder();
                sb_data.append("데이터 갯수 : "+ count+"\n");
                for(int i=0; i<count; i++){
                    cursor.moveToNext(); // 다음 데이터로 이동
                    String name = cursor.getString(0); // 커서가 가르키는 column0(이름) 받기
                    int age = cursor.getInt(1); // 커서가 가르키는 column1(나이) 받기
                    sb_data.append("data#"+i+") 이름= "+name+", 나이= "+age+"\n"); // 스트링빌더에 출력문 추가
                }
                dataView.setText(sb_data.toString()); // 텍스트뷰에 표시
            }
        });
    }

}

아까 설명한 메소드를 사용하여 간단하게 DB 및 테이블을 생성했고 데이터를 추가 및 삭제했다. 이 때, 순수 SQL문을 execSQL() 메소드로 실행한 부분도 있고, 데이터 삭제처럼 파라미터를 전달하여 SQL문을 실행한 경우도 있다. 실무에서는 SQL문을 execSQL() 메소드를 통해 실행하는것이 관리가 편해서 대부분 이 방법을 사용한다고 한다. 데이터를 추가 및 삭제 할 때마다 상태 표시창에 결과가 조회된다. 또한 조회 버튼을 클릭하면 현재 테이블의 모든 데이터가 조회된다. 앱을 실행화면 아래와 같은 화면을 볼 수 있다.

2
3


데이터베이스를 사용하기 위한 내용을 정리하면 아래와 같다.

  1. DB생성 : openOrCreateDatabase()
  2. 테이블 생성 : execSQL("create table …");
  3. 레코드 추가 : execSQL("insert into … ");
  4. 데이터 조회 : Cursor cursor = rawQuery("select … from …");

+ Recent posts