在sqlite for ios中选择准备语句时崩溃

时间:2016-07-19 07:26:20

标签: ios objective-c sqlite prepared-statement

我正在尝试从我的本地数据库中获取特定记录。为此我写了下面的代码。

- (NSArray *)selectFromActivityTableWithActivityId:(NSString *)activityId
{
    @synchronized(self)
    {
        sqlite3_stmt *statement;
        sqlite3 *database = nil;
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &database) != SQLITE_OK)
        {

        }
        else
        {

            if (sqlite3_prepare_v2(database, "select * from activity_master where activityId=?", -1, &statement, nil) != SQLITE_OK)
            {
                NSLog(@"error in preparing select:%s",sqlite3_errmsg(database));
            }
            activityId = [NSString stringWithFormat:@"%lld",[activityId longLongValue]];
            if (sqlite3_bind_text(statement, 1, activityId.UTF8String, -1, SQLITE_TRANSIENT) != SQLITE_OK)
            {
                NSLog(@"error in binding..%d",sqlite3_errcode(database));
            }
            while (sqlite3_step(statement) == SQLITE_ROW)
            {
                int record_id = sqlite3_column_int(statement, 0);
                int someValue1 = sqlite3_column_int(statement, 1);
                NSString *someValue2 = [NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 2)];
                int activityId = sqlite3_column_int(statement, 3);
                double someValue3 = sqlite3_column_double(statement, 4);


                NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:record_id], @"id", [NSNumber numberWithInt:someValue1], @"someKey1", someValue2, @"someKey2", [NSNumber numberWithInt:activityId], @"activityId", [NSNumber numberWithDouble:someValue3], @"someKey3", nil];

                [arr addObject:dict];
            }
            if (sqlite3_finalize(statement) != SQLITE_OK)
            {
                NSLog(@"error in finalizing statement:%s",sqlite3_errmsg(database));
            }
            if (sqlite3_close(database) != SQLITE_OK)
            {}
            else
            {}
        }
        statement = nil;

        return arr;
    }
}

在此代码中,我在if (sqlite3_prepare_v2(database, "select * from activity_master where activityId=?", -1, &statement, nil) != SQLITE_OK)

上遇到了崩溃

并非总是在此声明中获得一些时间异常。 我不知道是什么造成了异常。
以下是关于crashlytics的崩溃报告的屏幕截图。
Crashreport 请帮忙解决这个问题。
提前谢谢。

0 个答案:

没有答案