List例子:查询first name是"Joe"并且出生在1970年10月份或之后的人:items = userDao.queryBuilder() .where(Properties.FirstName.eq("Joe")) .orderAsc(Properties.LastName) .list();
QueryBuilder qb = userDao.queryBuilder(); qb.where(Properties.FirstName.eq("Joe"), qb.or(Properties.YearOfBirth.gt(1970), qb.and(Properties.YearOfBirth.eq(1970), Properties.MonthOfBirth.ge(10)))); List youngJoes = qb.list();请仔细观察括号的匹配。
Entity Cart = schema.addEntity("Cart"); Cart.addIdProperty(); Cart.addStringProperty("pid").notNull(); Cart.addStringProperty("pcount").notNull();每个商品的pid都是不同的。
Listitems = DbService.getInstance(getActivity()).queryCart(Properties.Pid.eq(prd)); if (items.size() == 0) { DbService.getInstance(getActivity()).addToCart(new Cart(null,prd,x)); Log.d("debug","success to add to cart, new item create"); } else if (items.size() == 1) { Cart item = items.get(0); int c1 = Integer.valueOf(item.getPcount()).intValue(); int c2 = Integer.valueOf(x).intValue(); item.setPcount(Integer.toString(c1+c2)); DbService.getInstance(getActivity()).updateCart(item); Log.d("debug","success to add to cart, item count modified"); } else { Log.d("error","error to add to cart"); }
Query query = userDao.queryBuilder().where(Properties.FirstName.eq("Joe"), Properties.YearOfBirth.eq(1970)).build(); List然后查询1977年出生的Maria:joesOf1970 = query.list();
query.setParameter(0, "Maria"); query.setParameter(1, 1977); ListmariasOf1977 = query.list();
GreenDao讲义3:带你了解查询生成器和更加复杂的查询,有需要的朋友可以参考下。
List例子:查询first name是"Joe"并且出生在1970年10月份或之后的人:items = userDao.queryBuilder() .where(Properties.FirstName.eq("Joe")) .orderAsc(Properties.LastName) .list();
QueryBuilder qb = userDao.queryBuilder(); qb.where(Properties.FirstName.eq("Joe"), qb.or(Properties.YearOfBirth.gt(1970), qb.and(Properties.YearOfBirth.eq(1970), Properties.MonthOfBirth.ge(10)))); List youngJoes = qb.list();请仔细观察括号的匹配。
Entity Cart = schema.addEntity("Cart"); Cart.addIdProperty(); Cart.addStringProperty("pid").notNull(); Cart.addStringProperty("pcount").notNull();每个商品的pid都是不同的。
Listitems = DbService.getInstance(getActivity()).queryCart(Properties.Pid.eq(prd)); if (items.size() == 0) { DbService.getInstance(getActivity()).addToCart(new Cart(null,prd,x)); Log.d("debug","success to add to cart, new item create"); } else if (items.size() == 1) { Cart item = items.get(0); int c1 = Integer.valueOf(item.getPcount()).intValue(); int c2 = Integer.valueOf(x).intValue(); item.setPcount(Integer.toString(c1+c2)); DbService.getInstance(getActivity()).updateCart(item); Log.d("debug","success to add to cart, item count modified"); } else { Log.d("error","error to add to cart"); }
Query query = userDao.queryBuilder().where(Properties.FirstName.eq("Joe"), Properties.YearOfBirth.eq(1970)).build(); List然后查询1977年出生的Maria:joesOf1970 = query.list();
query.setParameter(0, "Maria"); query.setParameter(1, 1977); ListmariasOf1977 = query.list();
queryRaw() queryRawCreate()