@SuppressWarnings("unchecked") public <T> T findFirst(Selector selector) throws DbException { if (!tableIsExist(selector.getEntityType())) return null; String sql = selector.limit(1).toString(); long seq = CursorUtils.FindCacheSequence.getSeq(); findTempCache.setSeq(seq); Object obj = findTempCache.get(sql); if (obj != null) { return (T) obj; } Cursor cursor = execQuery(sql); if (cursor != null) { try { if (cursor.moveToNext()) { T entity = (T) CursorUtils.getEntity(this, cursor, selector.getEntityType(), seq); findTempCache.put(sql, entity); return entity; } } catch (Throwable e) { throw new DbException(e); } finally { IOUtils.closeQuietly(cursor); } } return null; }
@SuppressWarnings("unchecked") public <T> T findById(Class<T> entityType, Object idValue) throws DbException { if (!tableIsExist(entityType)) return null; Table table = Table.get(this, entityType); Selector selector = Selector.from(entityType).where(table.id.getColumnName(), "=", idValue); String sql = selector.limit(1).toString(); long seq = CursorUtils.FindCacheSequence.getSeq(); findTempCache.setSeq(seq); Object obj = findTempCache.get(sql); if (obj != null) { return (T) obj; } Cursor cursor = execQuery(sql); if (cursor != null) { try { if (cursor.moveToNext()) { T entity = (T) CursorUtils.getEntity(this, cursor, entityType, seq); findTempCache.put(sql, entity); return entity; } } catch (Throwable e) { throw new DbException(e); } finally { IOUtils.closeQuietly(cursor); } } return null; }
@SuppressWarnings("unchecked") public <T> List<T> findAll(Selector selector) throws DbException { if (!tableIsExist(selector.getEntityType())) return null; String sql = selector.toString(); long seq = CursorUtils.FindCacheSequence.getSeq(); findTempCache.setSeq(seq); Object obj = findTempCache.get(sql); if (obj != null) { return (List<T>) obj; } List<T> result = new ArrayList<T>(); Cursor cursor = execQuery(sql); if (cursor != null) { try { while (cursor.moveToNext()) { T entity = (T) CursorUtils.getEntity(this, cursor, selector.getEntityType(), seq); result.add(entity); } findTempCache.put(sql, result); } catch (Throwable e) { throw new DbException(e); } finally { IOUtils.closeQuietly(cursor); } } return result; }
public long count(Selector selector) throws DbException { Class<?> entityType = selector.getEntityType(); if (!tableIsExist(entityType)) return 0; Table table = Table.get(this, entityType); DbModelSelector dmSelector = selector.select("count(" + table.id.getColumnName() + ") as count"); return findDbModelFirst(dmSelector).getLong("count"); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); action_bar_title_txtv.setText(getString(R.string.payment)); List<StoreCoinItemEntity> storeItemEntities = new ArrayList<StoreCoinItemEntity>(); // StoreCoinItemDomain storeCoinItemDomain = new StoreCoinItemDomain(); // storeCoinItemDomain.setImageResId(R.drawable.store_coin); // storeCoinItemDomain.setHowManyCoins("1000"); // storeCoinItemDomain.setHowMuchRmb("1"); // storeCoinItemDomain.setName("金幣"); // storeCoinItemDomain.setDesc("可以用來購買道具"); // storeItemEntities.add(storeCoinItemDomain); try { storeItemEntities = BeginApplication.dbUtils.findAll( Selector.from(StoreCoinItemEntity.class).where("PD_key", "=", "Money")); } catch (DbException e) { e.printStackTrace(); } // try { // storeItemEntities = BeginApplication.dbUtils.findAll(Selector.from(StoreItemEntity.class)); // } catch (DbException e) { // e.printStackTrace(); // } storeCoinListAdapter = new ExchangeCoinListAdapter(this); storeCoinListAdapter.setStoreCoinItemList(storeItemEntities); store_coin_lstv.setAdapter(storeCoinListAdapter); }
public static NotificationEO queryLastNotify() { try { return FinanceFundsApp.getDB() .findFirst(Selector.from(NotificationEO.class).orderBy("CreateTime", true)); } catch (DbException e) { e.printStackTrace(); } return null; }
public static List<Banquet> getBanquetsByHotelId(Context context, String hotelId) { DbUtils db = DbUtils.create(context); try { return db.findAll(Selector.from(Banquet.class).where("hotelId", "=", hotelId)); } catch (DbException e) { e.printStackTrace(); return null; } }
public List<Comment> query(int page) { try { List<Comment> list = db.findAll(Selector.from(Comment.class).limit(20 * page)); return list; } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
// 获取商品的第一页数据 public List<Product> getIndexProduct(int index) { List<Product> list = new ArrayList<>(); int startIndex = (index - 1) * 20; int endIndex = index * 20 + 1; try { list = db.findAll( Selector.from(Product.class).where("id", ">", startIndex).and("id", "<", endIndex)); } catch (DbException e) { e.printStackTrace(); } return list == null ? new ArrayList<Product>() : list; }
// 搜索数据 public List<Product> getSearchData(String codeOrName, int cateId) { List<Product> list = new ArrayList<>(); try { list = db.findAll( Selector.from(Product.class) .where("ProductId", "=", codeOrName) .or("ProductName", "=", codeOrName) .or("CategoryId", "=", cateId)); } catch (DbException e) { e.printStackTrace(); } return list == null ? new ArrayList<Product>() : list; }
/*package*/ DownloadManager(Context appContext) { ColumnConverterFactory.registerColumnConverter( HttpHandler.State.class, new HttpHandlerStateConverter()); mContext = appContext; db = DbUtils.create(mContext); try { downloadInfoList = db.findAll(Selector.from(DownloadInfo.class)); } catch (DbException e) { LogUtils.e(e.getMessage(), e); } if (downloadInfoList == null) { downloadInfoList = new ArrayList<DownloadInfo>(); } }
public static List<NotificationEO> queryNotifyList(Integer pageNum, Integer pageSize) { Log.d(TAG, "queryNotifyList()"); try { return FinanceFundsApp.getDB() .findAll( Selector.from(NotificationEO.class) .orderBy("CreateTime", true) .limit(pageSize) .offset(pageSize * pageNum)); } catch (DbException e) { e.printStackTrace(); } return null; }
/** * 以一天缓存一次策略,判断当天是否缓存过 * * @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; } }
// 获取商品列表的数据 private List<Category> getSpData() { List<Category> list = new ArrayList<>(); Category category = new Category(); category.categoryid = 0; category.name = "未分类"; list.add(category); try { List<Category> categoryList = db.findAll(Selector.from(Category.class)); if (categoryList == null) { return new ArrayList<Category>(); } list.addAll(categoryList); } catch (DbException e) { e.printStackTrace(); } return list; }
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(); } }
private void initData() { try { List<Mp3Info> list = CrashAppliacation.dbUtils.findAll( Selector.from(Mp3Info.class) .where("playTime", "!=", 0) .orderBy("playTime", true) .limit(Constant.PLAY_RECORD_NUM)); if (list == null || list.size() == 0) { record_tv.setVisibility(View.VISIBLE); record_lv.setVisibility(View.GONE); } else { record_tv.setVisibility(View.GONE); record_lv.setVisibility(View.VISIBLE); mp3Infos = (ArrayList<Mp3Info>) list; adapter = new MyMusicListAdapter(this, mp3Infos); record_lv.setAdapter(adapter); } } catch (DbException e) { 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); } }
public long count(Class<?> entityType) throws DbException { return count(Selector.from(entityType)); }
public <T> List<T> findAll(Class<T> entityType) throws DbException { return findAll(Selector.from(entityType)); }
public <T> T findFirst(Class<T> entityType) throws DbException { return findFirst(Selector.from(entityType)); }