1

sqlite ブラウザーを使用して sqlite データベースを作成しました。次に、それを資産フォルダーにコピーしました。以下は、DBHelper クラスです。パッケージ my.easymedi.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class DBHelper extends SQLiteOpenHelper {

/*
 * Database attributes - the Android's default system path for your
 * application database
 */
private static final String pkg = "my.easymedi.controller";
private static String DB_PATH = "/data/data/" + pkg + "/databases/";
private static String DB_NAME = "EasyMediInfo.jpeg";
private static final int DB_VERSION = 1;
private final Context myContext;
private SQLiteDatabase myDatabase;

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;

    try {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        if (dbfile.exists()) {
            Toast.makeText(context, "database exists", Toast.LENGTH_LONG)
                    .show();
        } else {
            Toast.makeText(context, "cant find database", Toast.LENGTH_LONG)
                    .show();
        }
    } catch (SQLiteException e) {
        System.out.println("Database doesn't exist");
    }
}

/*
 * Creates an empty database on the system and rewrite it with your own
 * database
 */
public void createDataBase() {
    boolean dbExist = checkDataBase();
    if (dbExist) {
        // do nothing - database already exist
    } else {
        /*
         * By calling this method an empty database will be created into the
         * default system path of your application
         * So e can be able to overwrite that database with our database
         */
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT)
                    .show();
            Log.d("CREATE_DB", e.getMessage());
        }
    }
}

/*
 * Copy your database from assets folder to the just created empty database
 * in the system folder from where it can be access and handle. This id done
 * by transferring byte stream
 */
private void copyDataBase() throws IOException {
    InputStream databaseInput = null;
    /* Path to copy the database */
    String outFileName = DB_PATH + DB_NAME;
    /* open the empty database as an output stream */
    OutputStream databaseOutput = new FileOutputStream(outFileName);
    /* open the local database as the input stream */
    databaseInput = myContext.getAssets().open(DB_NAME);

    /* Transfer byte from byte from input file to output file */
    byte[] buffer = new byte[1024];
    int length = databaseInput.read(buffer);
    while (length > 0) {
        databaseOutput.write(buffer, 0, length);
        databaseOutput.flush();
    }
    databaseOutput.flush();
    databaseInput.close();
    databaseOutput.close();
}

/*
 * Check if the database is already exist to avoid re-copying the file each
 * time you open the application This returns true if if db is exist, false
 * if it doesn't
 */
private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT)
                .show();
        Log.d("Check_DB", e.getMessage());
    }
    if (checkDB != null) {
        String str = "checked";
        System.out.println("====" + str + "====");
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

/* Open the database */
public boolean openDatabase() {
    String myPath = DB_PATH + DB_NAME;
    Toast.makeText(myContext, myPath, Toast.LENGTH_SHORT).show();
    myDatabase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
    if (myDatabase != null) {
        System.out.println("====database opened====");
    } else {
        System.out.println("====error opening database====");
    }
    return myDatabase != null ? true : false;
}

public void closeDatabase() {
    if(myDatabase != null){
        myDatabase.close();
    }
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

/**
 * @param table - name of the table
 * @param values - values to insert
 */
public void insertIntoDatabase(String table, ContentValues values) {
    myDatabase.insert(table, null, values);
}

メイン アクティビティ クラスでは、次のコード セグメントを使用してデータベースを初期化します。

DBHelper helper = new DBHelper(this);
helper.createDataBase();
helper.openDatabase();
helper.close();

しかし、次のエラーが発生しました。 ここに画像の説明を入力

このアプリケーションのエラーを説明できるほど親切な方がいらっしゃいましたら幸いです。

事前にサンクス。

4

0 に答える 0