在sqlite ios NULL cString';中获取错误;?

在sqlite ios NULL cString';中获取错误;?,ios,objective-c,sqlite,exception,Ios,Objective C,Sqlite,Exception,我正在使用故事板构建一个iOS应用程序。我在sqlite数据库中遇到了一个问题,我在这里得到了字段值 NSLog(@"selfuserid: %@",selfuserid); NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)]; 但我的代码再次转到下面的代码 NSString * selfuserid =[NSString stringWithUTF

我正在使用故事板构建一个iOS应用程序。我在sqlite数据库中遇到了一个问题,我在这里得到了字段值

NSLog(@"selfuserid: %@",selfuserid);
NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
但我的代码再次转到下面的代码

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
这是我的密码:

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
-(NSArray *) getRecords:(NSString*) filePath where:(NSString *)whereStmt
{
NSMutableArray * students =[[NSMutableArray alloc] init];
sqlite3* db = NULL;
sqlite3_stmt* stmt =NULL;
int rc=0;
rc = sqlite3_open_v2([filePath UTF8String], &db, SQLITE_OPEN_READONLY , NULL);
if (SQLITE_OK != rc)
{
    sqlite3_close(db);
    NSLog(@"Failed to open db connection");
}
else
{
    NSString  * query = @"SELECT * from students";
    if(whereStmt)
    {
        query = [query stringByAppendingFormat:@" WHERE %@",whereStmt];
    }
    rc =sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL);
    if(rc == SQLITE_OK)
    {
        while (sqlite3_step(stmt) == SQLITE_ROW) //get each row in loop
        {
NString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];//here the code comes again
NSString * profilepic =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
NSString * username =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
NSString * selectedsports =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
     NSDictionary *student =[NSDictionary dictionaryWithObjectsAndKeys:selfuserid,@"selfuserid",profilepic,@"profilepic",username,@"username",selectedsports,@"selectedsports", nil];
[students addObject:student];
NSLog(@"selfuserid: %@",selfuserid);i'm getting selfuserid here but code goes again to the above stm
NSLog(@"pic: %@",profilepic);
}
NSLog(@"Done");
sqlite3_finalize(stmt);
}
else
{
NSLog(@"Failed to prepare statement with rc:%d",rc);
}
sqlite3_close(db);
}
return students;
}
我得到一个例外:

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'

    *** First throw call stack:
    (
    0   CoreFoundation                      0x000000010450ef35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001041a7bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010450ee6d +[NSException raise:format:] + 205
    3   Foundation                          0x0000000103d0c464 +[NSString stringWithUTF8String:] + 78
    4   Playo                               0x0000000101a038f9 -[Play getRecords:where:] + 617
    5   Playo                               0x0000000101a06697 -[Play tableView:didSelectRowAtIndexPath:] + 231
    6   UIKit                               0x0000000104c26393 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1293
    7   UIKit                               0x0000000104c264d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    8   UIKit                               0x0000000104b61331 _applyBlockToCFArrayCopiedToStack + 314
    9   UIKit                               0x0000000104b611ab _afterCACommitHandler + 516
    10  CoreFoundation                      0x0000000104443dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    11  CoreFoundation                      0x0000000104443d20 __CFRunLoopDoObservers + 368
    12  CoreFoundation                      0x0000000104439b53 __CFRunLoopRun + 1123
    13  CoreFoundation                      0x0000000104439486 CFRunLoopRunSpecific + 470
    14  GraphicsServices                    0x00000001063299f0 GSEventRunModal + 161
    15  UIKit                               0x0000000104b3e420 UIApplicationMain + 1282
    16  Playo                               0x0000000101a34813 main + 115
    17  libdyld.dylib                       0x0000000105f47145 start + 1
   )
   libc++abi.dylib: terminating with uncaught exception of type NSException
检查以下行:

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
NString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];//here the code comes again
NSString * profilepic =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
NSString * username =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
NSString * selectedsports =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
这里发生的情况是,上面的一个字段是return
NULL
string。我猜这些字段的数据库设计约束具有允许向这些字段插入
NULL
数据的属性

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
因此,您必须检查这些字段中是否有任何一个返回
NULL
string,然后适当地存储一个空字符串

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
为此,请执行以下操作:

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
创建一个函数来验证
NULL
字符串:

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
- (NSString *) validateNilString:(NSString *)strValue {

    NSString *returnString = @"";

    @try {
        if (!strValue)
            return returnString;

        if ([strValue isKindOfClass:[NSNull class]])
            return returnString;

        if ([strValue isEqualToString:@"<nil>"])
            return returnString;

        if ([strValue isEqualToString:@"<null>"])
            return returnString;

        if ([strValue isEqualToString:@"NULL"])
            return returnString;

        if ([strValue isEqualToString:@"nil"])
            return returnString;

        if ([strValue isEqualToString:@"(null)"])
            return returnString;

        return strValue;
    }
    @catch (NSException *exception) {
        NSLog(@"Exception :%@",exception);
        return returnString;
    }
}

如果值为null,stringWithFormat不会崩溃

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
 NSString* str=[NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(statement, 0)];

我知道这是两年前的事了,但这在功能上等同于stringWithUTF8String吗?i、 e:UTF8当量?我也得到了空崩溃,如果它是安全的,并且做了同样的事情,我想使用它。它可以防止崩溃,但我仍然得到空值。