Problem with the dateformatter's in Iphone sdk.
- by monish
Hi guys,
Here I had a problem with the date formatters actually I had an option to search events based on the date.for this Im comparing the date with the date store in the database and getting the event based on the date for this I wrote the code as follows:
-(NSMutableArray*)getSearchAllLists:(EventsList*)aEvent
{
[searchList removeAllObjects];
EventsList *searchEvent = nil;
const char* sql;
NSString *conditionStr = @" where ";
if([aEvent.eventName length] > 0)
{
NSString *str = @"'%";
str = [str stringByAppendingString:aEvent.eventName];
str = [str stringByAppendingString:@"%'"];
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" eventName like %s",[str UTF8String]]];
}
if([aEvent.eventWineName length]>0)
{
NSString *str = @"'%";
str = [str stringByAppendingString:aEvent.eventWineName];
str = [str stringByAppendingString:@"%'"];
if([aEvent.eventName length]>0)
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and (wineName like %s)",[str UTF8String]]];
}
else
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@"wineName like %s",[str UTF8String]]];
}
}
if([aEvent.eventVariety length]>0)
{
NSString *str = @"'%";
str = [str stringByAppendingString:aEvent.eventVariety];
str = [str stringByAppendingString:@"%'"];
if([aEvent.eventName length]>0 || [aEvent.eventWineName length]>0)
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and (variety like %s)",[str UTF8String]]];
}
else
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@"variety like %s" ,[str UTF8String]]];
}
}
if([aEvent.eventWinery length]>0)
{
NSString *str = @"'%";
str = [str stringByAppendingString:aEvent.eventWinery];
str = [str stringByAppendingString:@"%'"];
if([aEvent.eventName length]>0 || [aEvent.eventWineName length]>0 || [aEvent.eventVariety length]>0)
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and (winery like %s)",[str UTF8String]]];
}
else
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@"winery like %s" ,[str UTF8String]]];
}
}
if(aEvent.eventRatings >0)
{
if([aEvent.eventName length]>0 || [aEvent.eventWineName length]>0 || [aEvent.eventWinery length]>0 || [aEvent.eventVariety length]>0)
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and ratings = %d",aEvent.eventRatings]];
}
else
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" ratings = %d",aEvent.eventRatings]];
}
}
if(aEvent.eventDate >0)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MMM-yy 23:59:59"];
NSString *dateStr = [dateFormatter stringFromDate:aEvent.eventDate];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter release];
NSTimeInterval interval = [date timeIntervalSinceReferenceDate];
printf("\n Interval in advance search:%f",interval);
if([aEvent.eventName length]>0 || [aEvent.eventWineName length]>0 || [aEvent.eventWinery length]>0 || aEvent.eventRatings>0 || [aEvent.eventVariety length]>0)
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" and eventDate = %f",interval]];
}
else
{
conditionStr = [conditionStr stringByAppendingString:[NSString stringWithFormat:@" eventDate = %f",interval]];
}
}
NSString* queryString= @" select * from event";
queryString = [queryString stringByAppendingString:conditionStr];
sqlite3_stmt* statement;
sql = (char*)[queryString UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_bind_text(statement, 1, [aEvent.eventName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [aEvent.eventWineName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [aEvent.eventVariety UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [aEvent.eventWinery UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(statement, 5,aEvent.eventRatings);
sqlite3_bind_double(statement,6, [[aEvent eventDate] timeIntervalSinceReferenceDate]);
while (sqlite3_step(statement) == SQLITE_ROW)
{
primaryKey = sqlite3_column_int(statement, 0);
searchEvent = [[EventsList alloc] initWithPrimaryKey:primaryKey database:database];
[searchList addObject:searchEvent];
[searchEvent release];
}
sqlite3_finalize(statement);
return searchList;
}
Here I compared the interval value in the database and the date we are searching and Im getting the different values and results were not found.
Guy's help me to get rid of this.
Anyone's help will be much appreciated.
Thank you
Monish Calapatapu.