美文网首页自定义views Android 开发技术分享
GreenDao 使用assets或者raw目录下数据库表

GreenDao 使用assets或者raw目录下数据库表

作者: palas猫 | 来源:发表于2016-05-30 17:40 被阅读902次

    1:自定义类,继承GreenDao中的DaoMaster.DevOpenHelper类

    public class DataBase OpenHelper extends DaoMaster.DevOpenHelper{

    privateContextcontext;

    privateSQLiteDatabasesqliteDatabase;

    private staticStringDB_PATH;

    private staticStringDB_NAME;

    publicDataBaseOpenHelper(Context context,Stringname,CursorFactory factory) {

    super(context,name,factory);

    this.context= context;

    this.DB_NAME= name;

    this.DB_PATH=DbUtils.getDbPath();

    try{

    createDataBase();

    }catch(Exceptionioe) {

    throw newError("Unable to create database");

    }

    }

    @Override

    public voidonUpgrade(SQLiteDatabasedb, intoldVersion, intnewVersion) {

    //TODO Auto-generated method stub

    }

    /**

    * Open Database for Use

    */

    public voidopenDatabase() {

    StringdatabasePath =DB_PATH+DB_NAME;

    sqliteDatabase=SQLiteDatabase.openDatabase(databasePath, null,

    (SQLiteDatabase.OPEN_READWRITE));

    }

    /**

    * Close Database after use

    */

    @Override

    public synchronized voidclose() {

    if((sqliteDatabase!=null) &&sqliteDatabase.isOpen()) {

    sqliteDatabase.close();

    }

    super.close();

    }

    /**

    * Get database instance for use

    */

    publicSQLiteDatabasegetSqliteDatabase() {

    returnsqliteDatabase;

    }

    /**

    * Create new database if not present

    */

    public voidcreateDataBase() {

    SQLiteDatabasesqliteDatabase =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 booleandatabaseExists() {

    SQLiteDatabasesqliteDatabase =null;

    try{

    StringdatabasePath =DB_PATH+DB_NAME;

    sqliteDatabase =SQLiteDatabase.openDatabase(databasePath, null,

    SQLiteDatabase.OPEN_READWRITE);

    }catch(SQLiteExceptione) {

    e.printStackTrace();

    }

    if(sqliteDatabase !=null) {

    sqliteDatabase.close();

    }

    returnsqliteDatabase !=null?true:false;

    }

    /**

    * Copy existing database file in system

    */

    public voidcopyDataBase() {

    intlength;

    byte[] buffer =new byte[1024];

    StringdatabasePath =DB_PATH+DB_NAME;

    try{

    InputStream databaseInputFile =context.getResources().openRawResource(R.raw.city);

    OutputStream databaseOutputFile =newFileOutputStream(databasePath);

    while((length = databaseInputFile.read(buffer)) >0) {

    databaseOutputFile.write(buffer,0,length);

    databaseOutputFile.flush();

    }

    databaseInputFile.close();

    databaseOutputFile.close();

    }catch(FileNotFoundExceptione) {

    e.printStackTrace();

    }catch(IOExceptione) {

    e.printStackTrace();

    }

    }

    }

    2:将新建的GreenDaoHelper对象换成上述对象,即可完成表的复制。

    3:在GreenDao的main函数中增加表。

    4:记得在DaoMater中删除复制过去的表的create语句,因为表已经存在了。

    相关文章

      网友评论

      • 9f3b92b355ff:能给个demo吗?
        woitaylor:新版的GreenDao使用本地:http://www.jianshu.com/p/8ffa1eb15ae3

      本文标题:GreenDao 使用assets或者raw目录下数据库表

      本文链接:https://www.haomeiwen.com/subject/srhcdttx.html