/** * 以一天缓存一次策略,判断当天是否缓存过 * * @param context * @param tag 页面名称 * @return true:已经缓存过, 不需要再缓存 false:未缓存过,需要缓存操作 */ public static boolean todayChecked(Context context, String tag) { DbUtils db = DbUtils.create(context); try { Cache cache = db.findFirst(Selector.from(Cache.class).where("pageName", "=", tag)); if (cache == null) return false; String todayStr = getToday(); if (todayStr.equals(cache.getDate())) { return true; } else { return false; } } catch (DbException e) { e.printStackTrace(); return false; } }
public void insert(List<Comment> list) { try { for (int i = 0; i < list.size(); i++) { Comment commentItem = list.get(i); Comment findItem = db.findFirst( Selector.from(Comment.class).where("commentId", "=", commentItem.getCommentId())); if (findItem != null) { db.update(commentItem, WhereBuilder.b("commentId", "=", commentItem.getCommentId())); } else { db.save(commentItem); } } } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chat_comment); dbUtils = DBUtilsHelper.getInstance().getDb(); Intent i = getIntent(); if (null != i) { subjectID = i.getExtras().getString("subjectId"); try { chatInfoBean = dbUtils.findFirst(Selector.from(ChatInfoBean.class).where("SubjectID", "=", subjectID)); if (null != chatInfoBean) { subjectType = chatInfoBean.getSubjectType(); } } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } } initView(); initListener(); listenET(); }
private void testDb() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Parent parent = new Parent(); parent.name = "测试"; parent.isVIP = false; parent.setAdmin(true); parent.setEmail("*****@*****.**"); /*Parent parent2 = new Parent(); parent2.name = "测试2"; parent2.isVIP = false;*/ try { DbUtils db = DbUtils.create(this); db.configAllowTransaction(true); Child child = new Child(); child.name = "child name"; // db.saveBindingId(parent); // child.parent = new SQLiteLazyLoader<Parent>(Child.class, "parentId", parent.getId()); child.parent = parent; try { Parent test = db.findFirst(parent); // 通过entity的属性查找 LogUtils.d("wyouflf :" + test); } catch (Exception e) { LogUtils.e(e.getMessage(), e); } parent.setTime(new Date()); parent.setTime2(new java.sql.Date(new Date().getTime())); db.saveBindingId(child); // 保存对象关联数据库生成的id List<Child> children = db.findAll(Selector.from(Child.class)); LogUtils.d("wyouflf size:" + children.size()); if (children.size() > 0) { LogUtils.d("wyouflf child:" + children.get(children.size() - 1).parent); } List<Parent> list = db.findAll( Selector.from(Parent.class) .where(WhereBuilder.b("id", "<", 54)) .orderBy("id") .limit(10)); LogUtils.d("wyouflf size:" + list.size()); if (list.size() > 0) { LogUtils.d("wyouflf parent:" + list.get(list.size() - 1).toString()); } // parent.name = "hahaha123"; // db.update(parent); Parent entity = db.findById(Parent.class, parent.getId()); LogUtils.d("wyouflf parent:" + entity.toString()); List<DbModel> dbModels = db.findDbModelAll( Selector.from(Parent.class).groupBy("name").select("name", "count(name)")); LogUtils.d("wyouflf:" + dbModels.size()); } catch (DbException e) { LogUtils.e(e.getMessage(), e); } }