データベースを開く

広告

既に作成されているデータベースを開く方法を確認します。「ApplicationContext」クラスで用意されている「openDatabase」メソッドを使います。

Open an existing private SQLiteDatabase associated with this Context's 
application package. Throws FileNotFoundException if the database could 
not be opened.

Parameters:
  name  The name (unique in the application package) of the database.
  factory  An optional factory class that is called to instantiate a 
    cursor when query is called.
Throws:
  FileNotFoundException  java.io.FileNotFoundException

1番目の引数にはデータベース名を文字列で指定します。2番目の引数にはカーソルを指定するようですが詳細が不明ですので「null」を指定しておきます。データベースを開くことに成功すると、そのデータベースを表す「SQLiteDatabase」クラスのオブジェクトを取得できます。

このメソッドを実行する場合には例外として「java.io.FileNotFoundException」が発生する可能性があります。

具体的には次のように記述します。

private int DB_VERSION = 1;
private int DB_MODE = Context.MODE_PRIVATE;

SQLiteDatabase db;
try {
    db = openDatabase("db01_01", null);
} catch (FileNotFoundException e) {
    db = null;
}

サンプルプログラム

それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。

Test02_01.java

package jp.javadrive.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import java.io.FileNotFoundException;
import android.app.AlertDialog;

public class Test02_01 extends Activity implements OnClickListener{
    private final int FP = ViewGroup.LayoutParams.FILL_PARENT; 
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; 
    private Button button1;
    private Button button2;
    private SQLiteDatabase db;

    private int DB_VERSION = 1;
    private int DB_MODE = Context.MODE_PRIVATE;

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        db = null;

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        setContentView(linearLayout);

        button1 = new Button(this);
        button1.setText("Create Database");
        button1.setOnClickListener(this);
        linearLayout.addView(button1, createParam(WC, WC));

        button2 = new Button(this);
        button2.setText("Open Database");
        button2.setOnClickListener(this);
        linearLayout.addView(button2, createParam(WC, WC));
    }

    private LinearLayout.LayoutParams createParam(int w, int h){
        return new LinearLayout.LayoutParams(w, h);
    }

    public void onClick(View v) {
        if (v == button1){
            try {
                db = createDatabase("db02_01", DB_VERSION, DB_MODE, null);
            } catch (FileNotFoundException e) {
                db = null;
            }

            if (db != null){
                dispMsg("create database", "Success Create DB");
            }else{
                dispMsg("create database", "Error Create DB");
            }
        }else if (v == button2){
            try {
                db = openDatabase("db02_01", null);
            } catch (FileNotFoundException e) {
                db = null;
            }

            if (db != null){
                dispMsg("open database", "Success Open DB");
            }else{
                dispMsg("open database", "Error Open DB");
            }
        }
    }

    private void dispMsg(String title, String msg){
        AlertDialog.show(Test02_01.this, title, msg, "ok", false);
    }
}

ビルド後にエミュレーター上で実行します。

データベースを開く

「Open Database」ボタンをクリックします。この時点では開くデータベースが作成されていないためエラーとなります。

データベースを開く

次に「Create Database」ボタンをクリックして先にデータベースを作成します。

データベースを開く

改めて「Open Database」ボタンをクリックすると今度は対象となるデータベースが作成されていますのでデータベースを開くことに成功します。

データベースを開く

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。