我正在使用Storyboards构建iOS应用程序。我在sqlite数据库中遇到问题,我在这里获取字段值

NSLog(@"selfuserid: %@",selfuserid);

但我的代码再次转到以下代码。
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;
}

我收到此异常:
 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

最佳答案

如果value为null,stringWithFormat不会崩溃

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

10-08 05:50