greenDao:在Android上的sqlite文件中存储数据

时间:2014-09-10 17:38:38

标签: android sqlite persistence greendao greendao-generator

在Android智能手机的文件系统上的sqlite文件中存储数据的推荐方法是什么?我打算在设备重启后恢复数据。 “DaoMaster.createAllTables(db,true)”和“DaoMaster.createAllTables(db,false)”有什么区别?

目前我在MainActivity.java中使用此代码:

db = SQLiteDatabase.openOrCreateDatabase(new File(getExternalFilesDir(null).getPath(),
            "opendao.sqlite"), null);
daoMaster = new DaoMaster(db);
DaoMaster.createAllTables(db, true);
daoSession = daoMaster.newSession(db);
locationEntityDao = daoSession.getLocationEntityDao();
locationTraceEntityDao = daoSession.getLocationTraceEntityDao();
photoEntityDao = daoSession.getPhotoEntityDao();
photosEntityDao = daoSession.getPhotosEntityDao();
poiEntityDao = daoSession.getPoiEntityDao();
primitiveAttributesEntityDao = daoSession.getPrimitiveAttributesEntityDao();

我不得不修改geńeratedDaoMaster.java:

public DaoSession newSession(SQLiteDatabase db) {
    return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
}

1 个答案:

答案 0 :(得分:0)

我不太确定我是否理解你的问题。你对“设备重启”是什么意思?工厂重新设置?您是否关注了greenDao网站上的instructions?我一直在使用greenDao一段时间,我从未修改生成的DaoMaster,它应该在生成时工作。读取代码时,使用默认的daoMaster.newSession()时,使用的db是daoMaster创建中使用的db,因此不需要修改。

对于第二个问题,如果您按照生成的代码,您将看到createAllTables中设置true或false ifNotExist参数之间的差异(SQLiteDatabase db,boolean ifNotExists)。此布尔值确定是否应创建数据库中的每个表(如果已存在)。设置为true,如果表已在数据库中,则此CREATE命令将不起作用。