kerou / android-active-record

Automatically exported from code.google.com/p/android-active-record
0 stars 0 forks source link

hope to add the count function. #6

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

at ActiveRecordBase.java append: 

private static final String[] countProjection = new String[]{"count(*)"};
    /**
     * count table
     * @param <T>
     * @param type
     * @return
     * @throws ActiveRecordException
     */
    public <T extends ActiveRecordBase> long count(Class<T> type,String where,String[] whereArgs) throws ActiveRecordException {
        if (m_Database == null)
            throw new ActiveRecordException("Set database first");
        T entity;
        try {
            entity = type.newInstance();
        } catch (IllegalAccessException e) {
            throw new ActiveRecordException(e.getLocalizedMessage());
        } catch (InstantiationException e) {
            throw new ActiveRecordException(e.getLocalizedMessage());
        }

        Cursor c = m_Database.query(entity.getTableName(), countProjection, where, whereArgs);
        try {
            c.moveToFirst();
            return c.getLong(0);
        } finally {
            c.close();
        }
    }

Original issue reported on code.google.com by wangchu...@gmail.com on 1 Dec 2010 at 4:52

GoogleCodeExporter commented 9 years ago
Great addition. Will implement

Original comment by Vladimir.Kroz on 29 Mar 2011 at 8:38

GoogleCodeExporter commented 9 years ago

Original comment by Vladimir.Kroz on 29 Mar 2011 at 8:39