1
public GenericRawResults<Object[]> getCountByStatus(Date date,int status){
        Log.info("CallDayPlanningDao",date.toString());
        GenericRawResults<Object[]> rawResults=null;
        Dao callDayPlanningDao = getDao(CallDayPlanning.class);
        QueryBuilder query = callDayPlanningDao.queryBuilder();
        int year = date.getYear();
        int month = date.getMonth();
        Date date1 = new Date(year, month,1);
        Date date2 = new Date(year, month+1,1);

        Date startDate = new Date(date1.getTime()-5);
        Date endDate = new Date(date2.getTime()-5);
        try {
            **query.where().between("calldate", startDate, endDate);**//This line is not working
            if(status==Constant.cnStatus){
                query.where().in("callstatus", status,Constant.ccStatus);
            }else{
                query.where().eq("callstatus", status);
            }
            query.groupBy("calldate");
            query.selectRaw("calldate,count(*)");
            rawResults = callDayPlanningDao.queryRaw(query.prepareStatementString(), new DataType[] {
                            DataType.DATE_STRING, DataType.INTEGER });
            // page through the results

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return rawResults;
    }

好吧,我想获取对象的计数,但是日期条件无效,我从数据库中获取所有数据。有人可以帮助我吗?谢谢。

4

2 回答 2

3

我是 ORMLite 的新手,在访问 SQLite 数据库时遇到了同样的问题。

今天花了一整天的时间才弄明白,总结如下:

  1. 我发现格式“yyyy-Md H:m:s”在 ORMLite 中可以很好地处理 SQLite DateTime 数据类型,而不是 ORMLite 的默认格式“yyyy-MM-dd HH:mm:ss.SSSSS”。

  2. 为了让 ORMLite 在“Java Date”和“SQLite DateTime”之间进行转换,需要一个持久化类。

  3. 这里显示了我使用的持久类的代码,它覆盖了 DateStringType 的公共函数并使用 "dateFormatConfig" 而不是 defaultDateFormatConfig" :

`

public class DateStringSQLiteType extends DateStringType {

protected static final DateStringFormatConfig dateFormatConfig = new DateStringFormatConfig(
        "yyyy-M-d H:m:s");

private static final DateStringSQLiteType singleTon = new DateStringSQLiteType();

public static DateStringSQLiteType getSingleton() {
    return singleTon;
}

private DateStringSQLiteType() {
    super(SqlType.STRING, new Class<?>[0]);
}

/**
 * Convert a default string object and return the appropriate argument to a
 * SQL insert or update statement.
 */
@Override
public Object parseDefaultString(FieldType fieldType, String defaultStr)
        throws SQLException {
    DateStringFormatConfig formatConfig = convertDateStringConfig(
            fieldType, dateFormatConfig);
    try {
        // we parse to make sure it works and then format it again
        return normalizeDateString(formatConfig, defaultStr);
    } catch (ParseException e) {
        throw SqlExceptionUtil.create("Problems with field " + fieldType
                + " parsing default date-string '" + defaultStr
                + "' using '" + formatConfig + "'", e);
    }
}

/**
 * Return the SQL argument object extracted from the results associated with
 * column in position columnPos. For example, if the type is a date-long
 * then this will return a long value or null.
 * 
 * @throws SQLException
 *             If there is a problem accessing the results data.
 * @param fieldType
 *            Associated FieldType which may be null.
 */
@Override
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results,
        int columnPos) throws SQLException {
    return results.getString(columnPos);
}

/**
 * Return the object converted from the SQL arg to java. This takes the
 * database representation and converts it into a Java object. For example,
 * if the type is a date-long then this will take a long which is stored in
 * the database and return a Date.
 * 
 * @param fieldType
 *            Associated FieldType which may be null.
 * @param sqlArg
 *            SQL argument converted with
 *            {@link #resultToSqlArg(FieldType, DatabaseResults, int)} which
 *            will not be null.
 */
@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos)
        throws SQLException {
    String value = (String) sqlArg;
    DateStringFormatConfig formatConfig = convertDateStringConfig(
            fieldType, dateFormatConfig);
    try {
        return parseDateString(formatConfig, value);
    } catch (ParseException e) {
        throw SqlExceptionUtil.create("Problems with column " + columnPos
                + " parsing date-string '" + value + "' using '"
                + formatConfig + "'", e);
    }
}

/**
 * Convert a Java object and return the appropriate argument to a SQL insert
 * or update statement.
 */
@Override
public Object javaToSqlArg(FieldType fieldType, Object obj) {
    DateFormat dateFormat = convertDateStringConfig(fieldType,
            dateFormatConfig).getDateFormat();
    return dateFormat.format((Date) obj);
}

/**
 * @throws SQLException
 *             If there are problems creating the config object. Needed for
 *             subclasses.
 */
@Override
public Object makeConfigObject(FieldType fieldType) {
    String format = fieldType.getFormat();
    if (format == null) {
        return dateFormatConfig;
    } else {
        return new DateStringFormatConfig(format);
    }
}

}

`

  1. 用符号定义你的数据类: @DatabaseField(..., persisterClass = DateStringSQLiteType.class) private Date date;

  2. 它对我来说效果很好,可以执行“Between”查询,例如:

    list = foo.getDao().queryBuilder().where().between(HistoryStandardView.DATE_FIELD_NAME, new Date(98,1,1), new Date(115,1,1)).query();
    

ORMLite 的记录器显示结果语句:

[DEBUG] StatementExecutor 查询 'SELECT * FROM `HistoryStandardView` WHERE `date` BETWEEN '1998-2-1 0:0:0' AND '2015-2-1 0:0:0' ' 返回 2 个结果

于 2014-10-21T15:16:52.943 回答
1

如果我错了,请纠正我是您的calldate专栏类型DataType.DATE_STRING吗?如果是这种情况,则意味着持久数据类型是 VARCHAR,因此当您执行查询时,您会进行字符串比较而不是日期比较。因此,要解决您的问题,您可以:

  1. calldate将列的类型更改DataType.DATE为以TIMESTAMP.
  2. calldate将列的类型更改为DataType.DATE_LONG
  3. 找到一种方法来进行符合您需要的字符串比较(例如,date(calldate)如果您的calldate值与时间字符串格式匹配,请调用 sql 函数,请参阅http://www.sqlite.org/lang_datefunc.html)。

这是我所做的,它并不漂亮,但就像想要的那样工作:

QueryBuilder<OffreEntity, Integer> qb = this.daoOffre.queryBuilder();
//Need to format the date i want to compare so it can actually be compare with what i have on db
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyMMdd");
String correctFormat = dateFormatter.format(dateLimite);
//In db the date is represented as a VARCHAR with format dd/MM/yy so i need to reformat so it matches yyMMdd
String rawQuery = String.format("substr(%1$s,7)||substr(%1$s,4,2)||substr(%1$s,1,2) > '%2$s'", OffreEntity.COLUMN_NAME_DATE, correctFormat);
qb.where().raw(rawQuery);
offresDept = qb.query();

希望能帮助到你!

ps:感谢杰克道格拉斯的日期格式查询

于 2012-06-06T09:25:10.197 回答