Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

iphone - Retrieve month part from date saved in sqlite database table for displaying month wise data

hope every one is doing well :)

I have struck with retrieving the month from the date that is saved in sqlite database.First of all,I would like to thank some of the experts who guided me for proper insertion of a date in to sqlite database table i.e of the following format:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

I have displayed all the data including date in a controller page say "View Reminder" page.

Now I have a view controller for the user to view month wise,say if the user selects month "January" from picker view it will navigate to view controller where I display the reminders corresponding to the selected month.

I have earlier got struck at how to access the string value selected from picker view,so that we can use the query:

"SELECT * FROM reminders WHERE Grp = '%@'",appDelegate.groupString

Where groupString is declared in app delegate file,assigned a value in view controller where picker view is present and accessed that string using the above query in display view

which was the solution I found out here .Now I am trying to do the same thing for retrieving dates,i.e. simply write the query as:

"SELECT * FROM reminders WHERE Date = '%@'",appDelegate.monthString

Its not working because the date is saved in @"yyyy-MM-dd HH:mm:ss".So I have made my research on this through various links,I experimented the following ways:

"SELECT * FROM reminders WHERE MONTH like 'January %'" etc..

but as I have mentioned the string selected must be referenced to the value,hence used:

MONTH like '%@',appDelegate.monthString

"SELECT * FROM reminders WHERE strftime('%m', Date) = '%@'",appDelegate.monthString

Gone through sqlite date data types and This Link

None of them worked

As we all know we have a straight forward query for retrieving all the reminders from a specific month:

"SELECT * FROM reminders WHERE Date BETWEEN '2012-01-01' AND '2012-01-31'"

It works perfect,but I need to reference the January, February,March (selected from picker view) etc.. as reference string to date,I am not aware of how to get month part from date.Is there any query for retrieving the month part from a date say 2012-01-01 as January or some other in that particular context....

Please help me,Thanks all in advance :)

EDIT:

I have changed the display format in to just month followed by date i.e.:

ReminderClass *remin = [[ReminderClass alloc]init];
remin.Date = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)]autorelease];

                NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
                [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSDate *date = [dateFormat dateFromString:remin.Date];
                [dateFormat setDateFormat:@"MMMM dd"];
                NSString *dateVal = [dateFormat stringFromDate:date];
                remin.Date = dateVal;

But before we do that we need to write the sql query for retrieving data,i.e.:

sqlQuery = [NSString stringWithFormat:@"select * from reminders WHERE ????",appDelegate.monthString];

The question mark is what I need,thanks once again :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can use dateFormat for retrieving month from date:

NSDate *today = [NSDate date];

NSDateFormatter *monthFormat = [[NSDateFormatter alloc] init];
[monthFormat setDateFormat:@"MM"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...