In my last project i used the framework greenDao (http://greendao-orm.com/) for persisting data. this post will show you how to use it.
1. At first you have to create a simple java project to define and generating the entities
2. include DaoGenerator.jar to your buildpath
3. create a starter class like this
public class Start { private static final int SCHEMA_VERSION = 1; public static void main(final String[] args) throws Exception { final Schema schema = new Schema(SCHEMA_VERSION, "<MODEL_NAMESPACE>"); new DaoGenerator().generateAll(schema, "<OUTPUT_PATH>"); } }
4. now you can define your entity
final Entity entityArea = schema.addEntity("Area"); entityArea.addStringProperty("areaId").primaryKey(); entityArea.addStringProperty("country").notNull(); entityArea.addDateProperty("lastSync").notNull(); entityArea.addStringProperty("name").notNull(); entityArea.addStringProperty("subscribed").notNull();
5. after executing the main method the result looks like this
public class Area { private String areaId; private String country; private java.util.Date lastSync; private String name; private String subscribed; private transient DaoSession daoSession; private transient AreaDao myDao; // KEEP FIELDS - put your custom fields here // KEEP FIELDS END public Area() { } public Area(String areaId) { this.areaId = areaId; } public Area(String areaId, String country, java.util.Date lastSync, String name, String subscribed) { this.areaId = areaId; this.country = country; this.lastSync = lastSync; this.name = name; this.subscribed = subscribed; } // some getter, setter and some other stuff }
6. but thats not all! look at the AreaDao class. inside of this class is all the sql stuff i dont want to write by my own. and the class extends from AbstractDao. so you have all the CRUD operations for free! awesome
7. now you can use this classes in your android-project and you have do add the greendao.jar to your android project
8. in the next snipped i show you how easy it is to save an entity in your android project
public class DataProvider { private static final String DB_NAME = "my-db"; private final SQLiteDatabase db; private AreaDao areaDao; public DataProvider(final Context context) { SQLiteDatabase.CursorFactory cursorFactory = null; final DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, cursorFactory); db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); areaDao = daoSession.getAreaDao(); } public void createArea(final Area area) { areaDao.insert(area); } }
the setup looks a little bit like overhead. but remember – you get CRUD for free!
9. the last think i want to show you is the query api. here is a short example how you can use ist
areaDao.queryBuilder().where(AreaDao.Properties.Country.like("%island%")).list(); // that means give me all areas from countries that contains "island"
the query api has also methods like equals, greater than, between and all the other stuff we know from sql like ordering…
in addition it is possible to create relations between entities. for more infomation click here