sqlite3 : re-storing results

Discussion in 'iOS Programming' started by DennisBlah, Dec 20, 2014.

  1. DennisBlah, Dec 20, 2014
    Last edited: Dec 20, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hi all,

    I'm trying to grab the day of date from my agenda table where the month is current month.

    Code:
    [NSString stringWithFormat: @"SELECT strftime('%d', agenda_datum) as dayOfMonth FROM agenda_items WHERE strftime('%m', agenda_datum) = '%i'", month]
    
    However this NSString assumes that.. %d %m and %i must be set.
    However the %d should say, return day of month from date
    and %m should say, return month from date

    Here I try to get all days, from my agenda where month is equal to >month< (int of current month, or the month I'm looking at in my agenda)

    ---
    Resolved:
    I had to do the query setup in 2 steps.
    Code:
            NSString *tempQuery = @"SELECT >DISTINCT< strftime('%d', agenda_datum) as dayOfMonth FROM agenda_items WHERE strftime('%m', agenda_datum) = ";
            NSString *finalQuery = [NSString stringWithFormat: @"%@'%i'", tempQuery, month];
    
    ---


    Now I try to store these 'days' into an NSArray. Like this:
    Code:
    NSArray *dates = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:5], nil];
    
    But this needs to be done programaticly.

    This is giving me back an different number every time:
    Code:
            NSString *tempQuery = @"SELECT >DISTINCT< strftime('%d', agenda_datum) as dayOfMonth FROM agenda_items WHERE strftime('%m', agenda_datum) = ";
            NSString *finalQuery = [NSString stringWithFormat: @"%@'%i'", tQuery, month];
            NSArray *markDays = [[NSArray alloc] initWithArray:[dbManager loadDataFromDB:finalQuery]];
            NSInteger indexOfDayOfMonth = [dbManager.arrColumnNames indexOfObject:@"dayOfMonth"];
            NSMutableArray *markedDays = [[NSMutableArray alloc] initWithObjects: nil];
            for(int a=0; a<markDays.count; a++) {
                int mDay = (int)[[markDays objectAtIndex: a] objectAtIndex: indexOfDayOfMonth];
                [markedDays addObject: [[markDays objectAtIndex: a] objectAtIndex: indexOfDayOfMonth]];
            }
    


    (p.s. DISTINCT is like >DISTINCT< because I get blocked due SQL Injection :-/... woops ;D )
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    Your NSString stringWithFormat is looking for 3 values for the parameters designated by the percent signs. You are supplying only 'month'. Perhaps you want to escape the percent signs. Try adding a percent sign immediately before them.

    I'm guessing what you want your actual SQL to be something like this
    Code:
    SELECT strftime('%d', agenda_datum) as dayOfMonth FROM agenda_items WHERE strftime('%m', agenda_datum) = ’12’
    
    Here is a sample broken down, without the escaping since it isn't needed in this case.
    Code:
        NSInteger month = 12;
        NSString * coreSQL = @"SELECT strftime('%d', agenda_datum) as dayOfMonth FROM agenda_items WHERE strftime('%m', agenda_datum) = ";
        NSString * paramSQL = [NSString stringWithFormat: @"%@'%ld'",coreSQL,(long)month];
        NSLog(@"SQL:  %@", paramSQL);
    
    I suggest you break down that line into several so that you can better understand the problem.
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    ^_^ that's indeed what I did, now I'm having a next issue with storing these days in an new array.
     

Share This Page