我用于执行查询的代码如下:
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;
}
下面给出了我用于数据库操作的类的保管箱:https://www.dropbox.com/s/c4haxqnbh0re41c/Archive%202.zip?dl=0
我已经参考了下面的链接,但无法理解。Sqlite 在 iOS SDK 中显示EXC_BAD_ACCESS
在您的示例中,您只将一个参数传递给execQuery:
,但您的实现每次在字符串中看到"?"时都会尝试从 varargs 中提取其他参数,其中有许多"?"。s.
这导致了崩溃。