0

我想要一个具有 sqlite DB 的应用程序。在我的 sqlite 数据库中存在 5 条记录,任何记录都有 2 列。(名称,ID,键)所有ID之一是NULL,我想得到这个。(ID 是整数变量)

这是我的代码,但运行时应用程序崩溃了。

do
        {
            sqlite3 *database2;
            if(sqlite3_open([[self dataFilePath] UTF8String], &database2) == SQLITE_OK)
            {            
                NSString *sqlStatement_userInfo2 =[NSString stringWithFormat:@"Select * from table1 where Name = %@ and ID = %@",p,p2];
                sqlite3_stmt *compiledStatement2;
                if(sqlite3_prepare_v2(database2, [sqlStatement_userInfo2 UTF8String], -1, &compiledStatement2, NULL) == SQLITE_OK)
                {
                // Loop through the results and add them to the feeds array
                    while(sqlite3_step(compiledStatement2) == SQLITE_ROW)
                    {
                        NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];
                        // Init the Data Dictionary
                        childID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement2, 1)];
                        childName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement2, 0)];
                        b = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement2, 2) ];
                        p = childID;
                        p2 = b;
                        [_dataDictionary setObject:[NSString stringWithFormat:@"%@",childName] forKey:@"Name"];
                        [array addObject:_dataDictionary];
                    }
                }

            else
            {
                NSLog(@"No Data Found");
            }

                // Release the compiled statement from memory
                sqlite3_finalize(compiledStatement2);

            }
                sqlite3_close(database2);

        } while (b != NULL);

此代码不起作用,我收到此错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'
*** First throw call stack:
(0x2092012 0x119fe7e 0x2091deb 0xb97480 0x35b7 0x2e08 0x1c8817 0x1c8882 0x1c8b2a 0x1dfef5 0x1dffdb 0x1e0286 0x1e0381 0x1e0eab 0x1e0fc9 0x1e1055 0x2e63ab 0x13792d 0x11b36b0 0x268efc0 0x268333c 0x268eeaf 0x1d68cd 0x11f1a6 0x11dcbf 0x11dbd9 0x11ce34 0x11cc6e 0x11da29 0x120922 0x1cafec 0x117bc4 0x117dbf 0x117f55 0x120f67 0xe4fcc 0xe5fab 0xf7315 0xf824b 0xe9cf8 0x1feddf9 0x1fedad0 0x2007bf5 0x2007962 0x2038bb6 0x2037f44 0x2037e1b 0xe57da 0xe765c 0x2b6d 0x2a95)
libc++abi.dylib: terminate called throwing an exception
(lldb) 
4

2 回答 2

2

在尝试使用它创建之前,您需要添加一个关于是否sqlite3_column_text(compiledStatement2, 1)返回字节的检查。NULLNSString

于 2013-05-18T12:04:40.613 回答
-1

如果 ID 是整数,则使用此:

  if( [ NSString stringWithFormat:@"%@", sqlite3_column_int(compiledStatement2, 1)]==nil)
 {
     childID = @"0";
  }
    else
   {
     childID = [ NSString stringWithFormat:@"%@", sqlite3_column_int(compiledStatement2, 1)];
   }
于 2013-05-18T12:09:12.620 回答