选择特定月份和年份sqlite xcode的数据

时间:2015-02-04 11:03:42

标签: ios iphone xcode sqlite date

从sqlite中检索数据时遇到一些问题,希望你们能帮助我,

所以,让我在这里描述我的问题,

CREATE TABLE ex_manager (ex_id integer PRIMARY KEY AUTOINCREMENT,ex_title text,ex_amount text,ex_description text,ex_category text,ex_date DATE ,ex_upload_date DATE, ex_image text)

这是用于创建表的查询,

表的日期数字ex_date具有DATE数据类型

现在,我插入格式为yyyy-MM-dd的日期,

NSDate *today=[NSDate date];
    NSDateFormatter *df=[[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd-MM-yyyy"];
    NSString *datestring = [df stringFromDate:today];

我执行的查询是:

query= [NSString stringWithFormat:@"insert into ex_manager1 (ex_title,ex_amount,ex_description,ex_date,ex_category,ex_upload_date,ex_image) values ('%@','%@','%@','%@','%@','%@','%@')",_ex_title.text,_ex_amount.text,@"abcd",_ex_date.text,_ex_category.text,datestring,path ];

而且,我可以成功地将数据添加到数据库中,

我希望检索特定月份和年份的数据。

我像

一样运行查询
SELECT * FROM ex_manager where strftime('%Y%m',ex_date) = '201502'

并且结果中没有数据。

任何人都可以指导我如何创建数据库并插入值以及如何通过选择并在查询中传递数据来检索特定月份和年份的数据。

先谢谢。这对我很有帮助,请帮帮我。

2 个答案:

答案 0 :(得分:1)

dd-MM-yyyy不是supported date formats for time strings之一,因此strftime()的返回值始终为NULL。

请改用yyyy-MM-dd (在这种情况下,您也可以避免调用strftime函数并改为使用WHERE ex_date GLOB '2015-02*',这将允许数据库使用索引进行搜索。)

答案 1 :(得分:0)

我认为可能存在插入数据的问题,尝试替换此行可能对您有帮助。

query= [NSString stringWithFormat:@"insert into ex_manager1 (ex_title,ex_amount,ex_description,ex_date,ex_category,ex_upload_date,ex_image) values (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",_ex_title.text,_ex_amount.text,@"abcd",_ex_date.text,_ex_category.text,datestring,path ];