コンピュータクワガタ

かっぱのかっぱによるコンピュータ関連のサイトです

Androidアプリ入門 No.75 SQLiteの使用 トランザクション

SQLiteの使用

トランザクション

SQLiteでもトランザクション処理が可能である。今までの例であると明示してトランザクションを示していなかったため、自動的にコミットされている格好となっていた。DbTest.javaを以下のように変更して、明示的にトランザクション処理をする。今回は明示的に例外を起こしてロールバックされることを確認する。

package sample.dt;

import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DbTest extends Activity {
    private EditText searchEditText;
    private Button readButton;
    private TextView resultTextView;

    private EditText nameEditText;
    private EditText ageEditText;
    private Button insertButton;

    private DatabaseHelper databaseHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // DB作成
        databaseHelper = new DatabaseHelper(getApplicationContext());

        searchEditText = (EditText) findViewById(R.id.searchEditText);
        readButton = (Button) findViewById(R.id.readButton);
        readButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                read();
            }
        });
        resultTextView = (TextView) findViewById(R.id.resultTextView);

        nameEditText = (EditText) findViewById(R.id.nameEditText);
        ageEditText = (EditText) findViewById(R.id.ageEditText);
        insertButton = (Button) findViewById(R.id.insertButton);
        insertButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
    }

    private void read() {
        String search = searchEditText.getText().toString();
        if (search.equals("")) {
            Toast.makeText(getApplicationContext(), "検索条件を入力してください。",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        Cursor cursor = db.query("emp", new String[] { "name", "age" },
                "name LIKE '%' || ? || '%'", new String[] { search }, null, null, "age DESC");
        cursor.moveToFirst();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < cursor.getCount(); i++) {
            sb.append(cursor.getString(0)).append(",");
            sb.append(cursor.getInt(1)).append("\n");
            cursor.moveToNext();
        }
        cursor.close();
        resultTextView.setText(sb.toString());
    }

    private void insert() {
        String name = nameEditText.getText().toString();
        if (name.equals("")) {
            Toast.makeText(getApplicationContext(), "登録条件(名前)を入力してください。",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        String age = ageEditText.getText().toString();
        if (age.equals("")) {
            Toast.makeText(getApplicationContext(), "登録条件(年齢)を入力してください。",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        db.beginTransaction();
        try {
            SQLiteStatement stmt = db.compileStatement("INSERT INTO emp(name, age) VALUES(?, ?)");
            stmt.bindString(1, name);
            stmt.bindLong(2, Long.parseLong(age));
            stmt.executeInsert();
            if (true) {
                throw new SQLException();
            }
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "エラーが発生しました。",
                    Toast.LENGTH_SHORT).show();
            return;
        } finally {
            db.endTransaction();
        }

        nameEditText.setText("");
        ageEditText.setText("");
        Toast.makeText(getApplicationContext(), "データを登録しました。",
                Toast.LENGTH_SHORT).show();
    }
}

実行結果は以下。登録しようとしても必ずエラーが発生してデータがロールバックされる。正常系の動作を確認したい場合には、例外をthrowしている部分を削除するといい。

今回追加した部分のコードは以下となる。

db.beginTransaction();
try {
    SQLiteStatement stmt = db.compileStatement("INSERT INTO emp(name, age) VALUES(?, ?)");
    stmt.bindString(1, name);
    stmt.bindLong(2, Long.parseLong(age));
    stmt.executeInsert();
    if (true) {
        throw new SQLException();
    }
    db.setTransactionSuccessful();
} catch (SQLException e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), "エラーが発生しました。",
            Toast.LENGTH_SHORT).show();
    return;
} finally {
    db.endTransaction();
}

トランザクションは、SQLiteDatabase#begenTransactionから始まりSQLiteDatabase#endTransactionで終わる。その中で、SQLiteDatabase#setTransactionSuccessfulメソッドが呼び出されていればトランザクションがコミットされる。そうでない場合にはトランザクションロールバックされる。