如何使用GreenDao ORM库在Android中从Assets创建数据库



我使用GreenDao orm库来创建数据库,它在代码中与创建数据库的第一个示例配合得很好。

现在的问题是,我找不到任何文档或任何与数据库相关的东西,从Assets Folder复制到内存中的数据库。

接受的答案提供了一个解决方案,但它只是部分正确的。看看我贴在下面的课。这是greenDao的OpenHelper类的扩展。下面是它的使用示例,以及如何使用它而不是OpenHelper或DevOpenHelper来管理sqlite数据库的副本。

package com.example;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import com.example.R;
import com.example.DaoMaster.OpenHelper;
public class DatabaseOpenHelper extends OpenHelper {
    private Context context;
    private SQLiteDatabase sqliteDatabase;
    private static String DB_PATH;
    private static String DB_NAME;
    public DatabaseOpenHelper(Context context, String name, CursorFactory factory) {
        super(context, name, factory);
        this.context = context;
        this.DB_NAME = name;
        this.DB_PATH = context.getString(R.string.DB_PATH);
        try {
            createDataBase();
        } catch (Exception ioe) {
            throw new Error("Unable to create database");
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }
    /** Open Database for Use */
    public void openDatabase() {
        String databasePath = DB_PATH + DB_NAME;
        sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
                (SQLiteDatabase.OPEN_READWRITE));
    }
    /** Close Database after use */
    @Override
    public synchronized void close() {
        if ((sqliteDatabase != null) && sqliteDatabase.isOpen()) {
            sqliteDatabase.close();
        }
        super.close();
    }
    /** Get database instance for use */
    public SQLiteDatabase getSqliteDatabase() {
        return sqliteDatabase;
    }
    /** Create new database if not present */
    public void createDataBase() {
        SQLiteDatabase sqliteDatabase = null;
        if (databaseExists()) {
            /* Check for Upgrade */
        } else {
            /* Database does not exists create blank database */
            sqliteDatabase = this.getReadableDatabase();
            sqliteDatabase.close();
            copyDataBase();
        }
    }
    /** Check Database if it exists */
    private boolean databaseExists() {
        SQLiteDatabase sqliteDatabase = null;
        try {
            String databasePath = DB_PATH + DB_NAME;
            sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
        if (sqliteDatabase != null) {
            sqliteDatabase.close();
        }
        return sqliteDatabase != null ? true : false;
    }
    /**
     * Copy existing database file in system
     */
    public void copyDataBase() {
        int length;
        byte[] buffer = new byte[1024];
        String databasePath = DB_PATH + DB_NAME;
        try {
            InputStream databaseInputFile = this.context.getAssets().open(DB_NAME+".sqlite");
            OutputStream databaseOutputFile = new FileOutputStream(databasePath);
            while ((length = databaseInputFile.read(buffer)) > 0) {
                databaseOutputFile.write(buffer, 0, length);
                databaseOutputFile.flush();
            }
            databaseInputFile.close();
            databaseOutputFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

下面是它的用法示例:

DatabaseOpenHelper helper = new DatabaseOpenHelper(this, "db", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();

相关内容

  • 没有找到相关文章

最新更新