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语句,因为表已经存在了。