FMDB and datetime() function

Status
Not open for further replies.

kingshaka_1977

Wise, Aged Ars Veteran
102
I'm having a few problems with dates and FMDB. I have a whole bunch of records with datetime values in them which I want to group by date/hour etc. Using a basic sqllite query tool, I have determined the correct sql syntax to use:

Code:
select strftime('%Y-%m-%d',datetime('2001-01-01',zentrydate || 'seconds','localtime')) as dteEntry...

The above works and the returned data is correct. However, when i try and execute the same using the FMDB wrapper, my result does not return the correct results. Any fields returned when using the datetime() function reads as NULL when i print it out to the console. Also, the query is not executing correctly either since only 1 record is returned, i know this is incorrect since i can execute the same query using my sqllite tools which returns the correct results.

Can anyone help me work out what I need to do? i only downloaded the FMDB lib a few weeks ago so should have a relatively recent version.
 

kingshaka_1977

Wise, Aged Ars Veteran
102
Hi, not sure how my code will help, as the problem seems to lie with the SQL I am trying to execute. However, I have included it below as I'm totally at a loss as to what I can do next:

Code:
FMDatabase *database = [FMDatabase databaseWithPath:[[CADAL fullDBPath] description]];
[database open];
    
NSMutableArray *retValue = [[NSMutableArray alloc] init];
    
NSString *str_sql = @"select "
"strftime('%Y-%m-%d',datetime('2001-01-01',zentrydate || 'seconds','localtime')) as dteEntry, "
"strftime('%H',datetime('2001-01-01',zentrydate || 'seconds','localtime')) as hourEntry, "
"sum(zentryoffset) as qtyHits "
"from zcountentry "
"group by strftime('%Y-%m-%d',datetime('2001-01-01',zentrydate || 'seconds','localtime')), strftime('%H',datetime('2001-01-01',zentrydate || 'seconds','localtime')) "
    "order by strftime('%Y-%m-%d',datetime('2001-01-01',zentrydate || 'seconds','localtime')), strftime('%H',datetime('2001-01-01',zentrydate || 'seconds','localtime'))";
    
FMResultSet *results = [self executeQueryForDatabase:database withSQL:str_sql];
    
while ([results next]) 
{
        id x = [NSNumber numberWithInt:[results intForColumn:@"qtyHits"]];
        [retValue addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,key, nil]];
        
}

[database close];

I need to be able to format the dates correctly so my grouping will work correctly in the SQL string. As I have mentioned in the OP, I have tested this query using an SQLLite desktop query tool and it works correctly.

I really dont know of any other way to retrieve the correct date time values from what is stored in the database, so would love someone to educate me :)

Thanks
 

kingshaka_1977

Wise, Aged Ars Veteran
102
ShuggyCoUk":19r8032q said:
are you opening the right db?

If you just do select count(*) from the table and print the result what do you get?

Hi Shaggy, definately opening the right dbase. If I do a select count(*), I get the correct results. It seems as though the datetime() component is throwing things out, whenever i introduce that to my query, things start to get wierd.

I am populating the data using Core Data. I was thinking that maybe I should just change my entity type to an int or string to see if inserting the date in a different format can simplify my sql syntax. I cant think of any other way to fix this as it seems that FMDB just wont handle the sql in its current form.

What do you think?
 
Status
Not open for further replies.