Ios 编写sqlilte查询以首次将dateString转换为日期并与当前日期进行匹配

Ios 编写sqlilte查询以首次将dateString转换为日期并与当前日期进行匹配,ios,objective-c,sqlite,Ios,Objective C,Sqlite,在我的数据库表中有一列,日期存储为字符串。格式为2017年1月29日。现在我想检索当前日期大于存储日期的表的所有行数据。我应该首先将此列dateString转换为实际的NSDate,然后与当前日期进行比较,从而检索所有行数据。但是我没有得到关于如何实际编写查询的线索。任何帮助都将不胜感激。 我正在使用sqlite。您必须执行以下日期格式化程序转换,然后使用查询检索数据: //First You have to formate your date in your string date forma

在我的数据库表中有一列,日期存储为字符串。格式为2017年1月29日。现在我想检索当前日期大于存储日期的表的所有行数据。我应该首先将此列dateString转换为实际的NSDate,然后与当前日期进行比较,从而检索所有行数据。但是我没有得到关于如何实际编写查询的线索。任何帮助都将不胜感激。
我正在使用sqlite。

您必须执行以下日期格式化程序转换,然后使用查询检索数据:

//First You have to formate your date in your string date formate.
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-DD HH:MM:SS.SSS"];
NSString *currentDate=[dateformatter stringFromDate:[NSDate date]];

//Your Query 
NSString *fetchQuery = [NSString stringWithFormat:@"SELECT * FROM TableName WHERE date < '%@'",currentDate];
//首先,您必须在字符串日期格式中设置日期格式。
NSDateFormatter*dateformatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“YYYY-MM-DD HH:MM:SS.SSS”];
NSString*currentDate=[dateformatter stringFromDate:[NSDate date]];
//你的问题
NSString*fetchQuery=[NSString stringWithFormat:@“从表名中选择*,其中日期<'%@',当前日期];
编辑:

查看此屏幕截图:

链接:

如果您想要比较字符串日期,您需要遵循
SQLite
的日期格式,然后
SQLite
函数使用此查询

所以,只需做一件事,即插入数据,更改日期格式,并将数据插入数据库,所有事情都会按预期工作


希望这将帮助您获得正确的数据。

最好的方法是将日期字符串保存为SQlite时间字符串格式ex
yyy-MM-DD
,以便您可以直接比较日期时间

这是您案例中的sqlite查询(我在终端中进行了测试)

当我选择全部

select * from TestData;
0|Jan 27, 2017
1|Jan 28, 2017
2|Feb 10, 2017
3|Feb 12, 2017
4|Feb 13, 2017
5|Aug 20, 2017
6|Aug 21, 2017
7|Aug 22, 2017
这是当我获取所有日期为“20170210”的行时需要的查询

select rowID,replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(date_string, 'Jan', '01'), 'Feb', '02'), 'Mar', '03'), 'Apr', '04'), 'May', '05'), 'Jun', '06'), 'Jul', '07'), 'Aug', '08'), 'Sep', '09'), 'Oct', '10'), 'Nov', '11'), 'Dec', '12') as date_time_string 
from TestData 
where substr(date_time_string, 8)||substr(date_time_string, 1, 2)||substr(date_time_string, 4, 3) >= '20170210';

2|02 10, 2017
3|02 12, 2017
4|02 13, 2017
6|08 21, 2017
7|08 22, 2017

这似乎不是一个好方法,希望这对您有所帮助。

从sqlite检索数据后,您需要将stringDate转换为NSDate?最好将日期存储为DateTime格式,并且您可以直接比较检索数据后的转换点。首先,我必须匹配日期,然后只检索符合上述标准的数据。@u的建议是正确的,但现在我几乎完成了项目的一半,我还要做其他几项changes@mars这是一个小的工作,但现在它会让你的生活更容易,如果你妥善存储你的日期。永远不要将日期存储为字符串,除非可能是“yyyyMMdd”(至少可以排序和比较)。这会返回错误的条目,如“2016年1月14日”、“2017年1月23日”等。搜索“2017年12月27日”时。可能是因为此处比较的是字符串而不是实际日期。在此格式中,不能将日期作为字符串进行比较。月份名称将按字母顺序进行比较。而且它不处理不同的语言。你的更新也不会起作用,因为数据库中的日期以不可压缩的格式存储为字符串。
select rowID,replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(date_string, 'Jan', '01'), 'Feb', '02'), 'Mar', '03'), 'Apr', '04'), 'May', '05'), 'Jun', '06'), 'Jul', '07'), 'Aug', '08'), 'Sep', '09'), 'Oct', '10'), 'Nov', '11'), 'Dec', '12') as date_time_string 
from TestData 
where substr(date_time_string, 8)||substr(date_time_string, 1, 2)||substr(date_time_string, 4, 3) >= '20170210';

2|02 10, 2017
3|02 12, 2017
4|02 13, 2017
6|08 21, 2017
7|08 22, 2017