2010-08-31 99 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];