简体   繁体   中英

greenDao: store data in sqlite file on Android

What is the recommended way to store data in a sqlite file located on the filesystem of the Android smartphone? I intend to restore the data even after a device reboot. What is the difference between "DaoMaster.createAllTables(db, true)" and "DaoMaster.createAllTables(db, false)"?

Currently I am using this code in the 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();

I had to modify the geńerated DaoMaster.java:

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

I'm not quite sure if I understand your question. What do you mean for "device reboot"? A factory resset? Did you follow the instructions on greenDao web? I've been working with greenDao for a while and I never modified the generated DaoMaster, it should work as it's generated. Reading the code, when you use the default daoMaster.newSession(), the db used is the one used in the daoMaster creation, so your modification should not be necessary.

For your second question, if you follow the generated code, you'll see that the diference between setting true or false ifNotExist param in createAllTables(SQLiteDatabase db, boolean ifNotExists). This boolean determines if each table in the database should be created if already exists. Setted to true, if a table is already in the database, this CREATE command will have no effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM