我用于执行查询的代码如下:
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];
我已动态生成此查询。但我已将查询直接添加到字符串中。 但是当我尝试执行此插入查询时,应用程序崩溃在下面的函数:
- (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;
}
我用于数据库操作的类的dropbox如下: https://www.dropbox.com/s/c4haxqnbh0re41c/Archive%202.zip?dl=0
我已经提到了以下链接但无法理解。 Sqlite shows EXC_BAD_ACCESS in ios SDK
答案 0 :(得分:0)
在您的示例中,您只将一个参数传递给execQuery:
,但是您的实现每次看到'?'时都会尝试从varargs中提取其他参数。在字符串中,有很多'?'。
这导致了崩溃。