-1

The code I am using for executing the query is as below:

NSString *score_query = @"Insert into tbl_assessment (Question,Answer,Option1,Option2,Option3,Explanation,Used,ImageName,Reference) values ('All of the following questions would be asked in a lifestyle questionnaire except:','Do you feel any pain in your chest when you perform physical activity?','Does your occupation cause you anxiety (mental stress)?','Does your occupation require extended periods of sitting?','Do you partake in any recreational activities (golf, tennis, skiing, etc.)?','NULL','N','NULL','NULL')";
NSLog(@"%@",score_query);
[database executeQuery:score_query];

I have generated this query dynamically. But I have added the query directly to the string. But when I tried to execute this insert query, app crashes on below function:

- (NSArray )executeQuery:(NSString )sql, ... {
    va_list args;
    va_start(args, sql);

    NSMutableArray *argsArray = [[NSMutableArray alloc] init];
    NSUInteger i;
    for (i = 0; i < [sql length]; ++i)
    {
        if ([sql characterAtIndex:i] == '?')
            [argsArray addObject:va_arg(args, id)];
    }

    va_end(args);

    NSArray *result = [self executeQuery:sql arguments:argsArray];

    [argsArray release];
    return result;
}

The dropbox for the classes I am using for database operation is given below: https://www.dropbox.com/s/c4haxqnbh0re41c/Archive%202.zip?dl=0

I have already referred the below link but can't understand. Sqlite shows EXC_BAD_ACCESS in ios SDK

Pankaj Mundra
  • 1,401
  • 9
  • 25
  • You need to give us the line where you call that method. the value of `argsArray` and `sql`. – Larme Apr 11 '18 at 13:38
  • @Larme App crashes on [argsArray addObject:va_arg(args, id)]; this line. – Pankaj Mundra Apr 11 '18 at 14:06
  • 2
    So the code is not really related to SQLite. It's more about `variadic functions"` You should use this https://stackoverflow.com/questions/4804674/how-to-create-variable-argument-methods-in-objective-c for the loop instead of `[sql length]` first, because you might found more "?" than" objects in the arg list. So if that's the case, add a `NULL` yourself? – Larme Apr 11 '18 at 14:10

1 Answers1

0

In your example, you are only passing one argument to execQuery:, but your implementation is trying to pull additional arguments from the varargs every time it sees a '?' in the string, of which there are numerous '?'s.

That's causing the crash.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • So what can I do to insert values with ? (question mark) ? And can you please give me the link for latest updated classes of sqlite.h and sqlite.m ? – Pankaj Mundra Apr 12 '18 at 04:43
  • @PankajMundra You *can* use ?s, but you aren't passing any arguments save for the SQL string to your method. Frankly, hand writing SQL is a waste of time. Use CoreData or FMDB – bbum Apr 12 '18 at 18:13
  • Thanks for your reply, but I have solved my issue by replacing '?' sign with any other symbol. – Pankaj Mundra Apr 14 '18 at 04:09
  • @PankajMundra You've solved the symptom, but do you understand the crash? Also, **hand writing SQL and calling sqlite directly is a complete waste of time**. It is also extremely error prone. I have *never* seen a body of iOS code using SQLite directly get it right the first time. Use CoreData. Or FMDB. – bbum Apr 15 '18 at 16:22