我的Android项目中有一个sqlite数据库,我尝试选择日期之间的所有行。 但是此查询不返回任何行:
String where = OperationEntry.COLUMN_NAME_ENTRY_ACCOUNT_FK + " = ? AND " +
OperationEntry.COLUMN_NAME_ENTRY_DATE + " > date('2013-01-01') AND "+
OperationEntry.COLUMN_NAME_ENTRY_DATE + " < date('2013-12-31') ;";
String[] whereValue = {accountName};
Cursor c = db.query(
OperationEntry.TABLE_NAME, // The table to query
projection, // The columns to return
where, // The columns for the WHERE clause
whereValue, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
此外,如果我使用2013-01-01和2014-12-31,则所有行都匹配 我有和没有日期功能的测试,我有同样的问题 请注意,OperationEntry.COLUMN_NAME_ENTRY_DATE是日期类型。
你能帮帮我吗?你在我的代码中发现了什么错误吗?编辑: 我在这里添加了一些额外的代码 查询表创建:
public static final String SQL_CREATE_ENTRIES =
"CREATE TABLE IF NOT EXISTS " + OperationEntry.TABLE_NAME + " (" +
OperationEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
... +
OperationEntry.COLUMN_NAME_ENTRY_DATE + " DATE, " + ...
" )";
查询插入:
GregorianCalendar calendar = ...;
String date = calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
ContentValues values = new ContentValues();
values.put(OperationEntry.COLUMN_NAME_ENTRY_TITLE, operation.getTitle());
values.put(OperationEntry.COLUMN_NAME_ENTRY_QTE, operation.getQte());
values.put(OperationEntry.COLUMN_NAME_ENTRY_DATE, date);
...
long newRowId;
newRowId = db.insert(
OperationEntry.TABLE_NAME,
null,
values);
答案 0 :(得分:1)
我不是Android专家,但你显然在滥用date SQLite function。 date('2013-01-01')
毫无意义,评估为'2013-01-01'
。
尝试改为:
String where = OperationEntry.COLUMN_NAME_ENTRY_ACCOUNT_FK + " = ? AND " +
"date(" + OperationEntry.COLUMN_NAME_ENTRY_DATE + ") > '2013-01-01' AND " +
"date(" + OperationEntry.COLUMN_NAME_ENTRY_DATE + ") < '2013-12-31';";
您需要确保返回的列数据已正确格式化,而不是您硬编码的字符串!
答案 1 :(得分:0)
试试这个:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date firstDate = formatter.parse("2013-01-01");
Date endDate = formatter.parse("2013-12-31");
String where = OperationEntry.COLUMN_NAME_ENTRY_ACCOUNT_FK + " = ? AND " +
OperationEntry.COLUMN_NAME_ENTRY_DATE + " > ? AND "+
OperationEntry.COLUMN_NAME_ENTRY_DATE + " < ?";
String[] whereValue = {accountName, firstDate.getTime(), endDate.getTime()};
答案 2 :(得分:0)
sqlite没有buildin dateformat。查询的结果取决于数据如何存储在列ENTRY_DATE中。
我正在使用sqlite-date的int值,它们是由java代码转换的。如果您的业务层要搜索日期范围,则数据库层可以搜索int范围。
如果您在没有where过滤器的情况下执行查询,结果值如何?
例如我将long从long转换为字符串
final private static java.text.DateFormat isoDateTimeformatter = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ");
public static String getDateStr(long dateTime) {
if (dateTime == TimeSlice.NO_TIME_VALUE) {
return "";
} else {
return isoDateTimeformatter.format(new Date(dateTime));
}
}
将字符串转换回long我使用此
public static long parseDate(String mDateSelectedForAdd) {
try {
return isoDateTimeformatter.parse(
mDateSelectedForAdd).getTime();
} catch (ParseException e) {
Log.w(Global.LOG_CONTEXT,"cannot reconvert " + mDateSelectedForAdd + " to dateTime using " + isoDateTimeformatter,e);
return TimeSlice.NO_TIME_VALUE;
}
}