标签:
----引用自:http://greenrobot.org/greendao/ ( <=详细进站看文档 )
IDE : AS 2.1.2
Module中build.gradle(修改):
compile ‘de.greenrobot:greendao-generator:1.3.1‘
产生dao:
public static void main(String[] args){ // 创建集合 Schema schema = new Schema(1,"jxj.wt.imqq.dao"); // 表的描述 Entity contactTable = schema.addEntity("QQContact"); contactTable.addIdProperty().primaryKey().autoincrement(); contactTable.addLongProperty("account"); contactTable.addStringProperty("nick"); contactTable.addIntProperty("avatar"); // 引擎 try { new DaoGenerator().generateAll(schema, "../IMQQ/app/src/main/java"); } catch (Exception e) { e.printStackTrace(); } }
操作dao db:
public void test() { QQContact contact01 = new QQContact(); contact01.setAccount(110L); contact01.setNick("Tom"); contact01.setAvatar(1); QQContact contact02 = new QQContact(); contact02.setAccount(120L); contact02.setNick("Jack"); contact02.setAvatar(2); SQLiteDatabase db = new DaoMaster.DevOpenHelper(mContext, "qq.db", null).getWritableDatabase(); // 获取DaoSession(集合) DaoMaster master = new DaoMaster(db); // 指明哪个数据库的增删改查 DaoSession session = master.newSession(); // 开始操作 QQContactDao dao = session.getQQContactDao(); // 增加 dao.insert(contact01); dao.insert(contact02); // 修改 contact02.setNick("Lucy"); dao.update(contact02); // 查询 List<QQContact> list = dao.loadAll(); for (QQContact item:list){ System.out.println(item.toString()); } // 删除 dao.deleteByKey(2L); }
查询结果:
删除:
2016-6 - GreenDao -1.3.0 beta generator1.3.1
标签:
原文地址:http://www.cnblogs.com/juzi-123/p/5643247.html