objective-c - 调用 SQLite 方法时的 EXC_BAD_ACCESS

标签 objective-c sqlite exc-bad-access

sqlite3_stmt *stmt;

    NSString *selectQuery =[NSString stringWithFormat:@"select * from myaccount"];

    NSMutableArray *fl =[[NSMutableArray alloc]init];
    NSString *sname;
    NSString *myMobNo;
    NSMutableString *str = [[NSMutableString alloc]init];

    if (sqlite3_prepare_v2(db, [selectQuery UTF8String], -1, &stmt, nil) == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {

            sname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
            myMobNo = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 4)];
            [str appendString:[NSString stringWithFormat:@"%@!%@",sname,myMobNo]];

            [fl addObject:str];

        }
    }

    sqlite3_finalize(stmt);

    return fl;

我在我的项目中使用带有 SQL Lite 的 Objective C。当我调用上述方法时,我在 If 条件下收到 EXC_BAD_ACCESS 错误。我的代码有什么问题。任何帮助将不胜感激。提前致谢。
  0x0003c099 -[DBModel getCName] + 112
1   Kinkey  0x0001d623 -[ChatViewController viewDidLoad] + 302
2   0x34dae579 <redacted> + 364
3   0x34e031f7 <redacted> + 26
4   0x34e0313d <redacted> + 28
5   0x34e03021 <redacted> + 32
6   0x34e02f4d <redacted> + 272
7   0x34e02699 <redacted> + 64
8   0x34e02581 <redacted> + 324
9   0x34df0b5b <redacted> + 858
10  0x34df07fb <redacted> + 38
11  0x0004cd33 -[ChatView tableView:didSelectRowAtIndexPath:] + 970
12  0x34e5131d <redacted> + 876
13  0x34ed3da9 <redacted> + 156
14  0x3388f657 <redacted> + 450
15  0x32f55857 <redacted> + 14
16  0x32f55503 <redacted> + 274
17  0x32f54177 <redacted> + 1230
18  0x32ec723d CFRunLoopRunSpecific + 356
19  0x32ec70c9 CFRunLoopRunInMode + 104
20  0x36aa533b GSEventRunModal + 74
21  0x34de32b9 UIApplicationMain + 1120
22  0x00002a31 main + 108
23  0x000024cc start + 40



NSMutableArray *Details = [[DBModel database]getCName];
    NSArray *array = [[Details objectAtIndex:0] componentsSeparatedByString:@"!"];
    acName = [array objectAtIndex:0];
    acMobileNo = [array objectAtIndex:1];

if ((char *)sqlite3_column_text(stmt4, 1)!= nil) {
        sname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt4, 1)];
        }

最佳答案

如果 db指针未指向有效的打开数据库。如果 (a) 您忽略了调用 sqlite3_open,则可能会发生这种情况。和 db指针不是 NULL ;或者 (b) 您在某个时候关闭了数据库并忽略了 NULL db然后在尝试再次使用它之前忽略了重新打开它。底线,如果您将无效的非 NULL 值传递给 sqlite_prepare_v2 ,您将生成 EXC_BAD_ACCESS异常(exception)。

顺便说一句,此代码中还有另一个异常风险:如果这两个文本字段之一是 NULL (例如,数据库有 NULL 值或列索引错误(例如,作为从零开始的索引,sqlite3_column_text(stmt, 1) 是第二列,sqlite3_column_text(stmt, 4) 是第五列),您的程序也会生成异常(尽管有不同的异常(exception))。底线,您不能将 NULL 值传递给 stringWithUTF8String 而不会产生异常。我可能鼓励您在调用 stringWithUTF8String 之前检查这两个值。(如果您已经定义了数据库这些列是 NOT NULL ,这可以稍微减轻这种风险。)

关于objective-c - 调用 SQLite 方法时的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16609034/

相关文章:

objective-c - 我们可以在 Windows 上编译 objective c 吗?

android - 关于删除级联android 2.2

python sqlite "create table if not exists"问题

android - 安全取消后台线程中的 ORMLite 写入

xCode 4.3 在暂停或断点处崩溃

iphone - UITextView 文字渗色效果

ios - 保存或返回到以前的 iOS 场景

ios - 使用泛型时的 EXC_BAD_ACCESS

iphone - NSMutableDictionary上的EXC_BAD_ACCESS

iphone - handleOpenUrl 中的 RESTFUL url 设计