2010-08-31 101 views
0
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
     const char *sqlStatement = "select count(*) from mytable"; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 

      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
       NSString *aRecord = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]; 

      } 
     } 
} 

正在工作correctsql正在抛出异常

但是,当我改变我的查询const char *sqlStatement = "select * from mytable"; OR

const char *sqlStatement = "select columnname from mytable"; 

那么它抛出一个

uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'

回答

1
while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
     char *myChar = (char *)sqlite3_column_text(compiledStatement, 0); 
      if (myChar !=NULL) { 
      NSString *aRecord = [NSString stringWithUTF8String: myChar]; 
      } 
      else 
       myChar = nil; 
} 
0

我发现了一个forum thread具有类似的异常喜欢你,我希望这将有助于

根据论坛,你的列中的一些行有NULL数据,你必须检查反对它使用类似的东西(在论坛上)

char *localityChars = sqlite3_column_text(init_statement, 12); 

if (localityChars ==null) 
    self.Locality = nil; 
else 
    self.Locality = [NSString stringWithUTF8String: localityChars];