データベース条件クエリー文

5650 ワード

需要は以下の通りです:1枚のAppは情報のデータベースの表をクリックして、Appはクリックするたびに、情報を表に挿入します.指定された時間内の異なるパッケージ名を検索するAppのクリック回数を要求する
検索文は以下の通り:SELECT DISTINCT PKG,COUNT(*),*from APP_USAGE_TB_BEAN where DATE > 0 group by PKG
キーワード:distinct脱重
countカウント
グループbyグループ
具体的な方法は以下の通りである.
    /**
     *         App    
     * @param millTimes     s , day=0 ,    App    
     * @return
     */
    protected static Map, Float> getAppClickMap(int millTimes) {
        Map, Float> result = new HashMap<>();
        long time = 0;
        if (millTimes != 0) {
            time = System.currentTimeMillis() - millTimes;
        }
        Cursor c = null;
        try {
            String query = "SELECT DISTINCT "+AppUsageTbBeanDao.Properties.Pkg.columnName +" ,COUNT(*),* from "+AppUsageTbBeanDao.TABLENAME
                    +" where "+AppUsageTbBeanDao.Properties.Date.columnName +" > "+time
                    +" group by "+AppUsageTbBeanDao.Properties.Pkg.columnName;
//            CbLog.v(TAG,"query sql = "+query);
            c = DaoManager.getAppUsageTbBeanBaseDaoInstance().rawQuery(query);

            if (c != null) {
                c.moveToFirst();
                for (int i = 0; i < c.getCount(); i++) {

                    String s = "app_click|" + c.getString(c
                            .getColumnIndexOrThrow(AppUsageTbBeanDao.Properties.Pkg.columnName));
                    Float n = c.getFloat(1);
                    result.put(s, n);
                    c.moveToNext();
                }
            }

            c.close();
        } catch (Throwable e) {
            CbLog.e("", e.toString());
        } finally {
            if (c != null) {
                c.close();
            }
        }
//        StringBuffer sb = new StringBuffer("query result: \r
");
// Set keySet = result.keySet(); // for (String key : keySet) { // sb.append(key + " : " + result.get(key) + "\r
");
// } // CbLog.v(TAG, sb.toString()); return result; }