2010-07-20 3 views
2

Je reçois l'erreur suivante:problème sqlite3_prepare_v2

* Mettre fin application en raison d'exception uncaught 'NSInvalidArgumentException', raison: '* + [NSString stringWithUTF8String:]: NULL CSTRING'

sur la ligne de code ci-dessous:

NSString *aName = [NSString stringWithUTF8String(char*)sqlite3_column_text(compiledStatement, 1)]; 

Je ne sais pas ce qui se passe ici, la colonne I référencé dans mon instruction SQL contient des données. Code complet ci-dessous.

// Open the database from the users filessytem 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    { 
     // Setup the SQL Statement and compile it for faster access 
     const char *sqlStatement = "select ZROUTE_NAME from ZROUTE"; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
      // Loop through the results and add them to the Route Name array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
      { 
       // Read the data from the result row 
       NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 

       // Add the animal object to the animals Array 
       //[list addObject:animal];    
       [list addObject:aName];    
       //[animal release]; 
      } 
     } 
     else 
     { 
      NSLog(@"Error: failed to select details from database with message '%s'.", sqlite3_errmsg(database)); 
     } 

     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement);   
    } 
    sqlite3_close(database); 

Répondre

3

la condition Poignée de pointeur NULL pour l'instruction

if((char*)sqlite3_column_text(compiledStatement, 1) != NULL) 
{ 
     NSString *aName = [NSString stringWithUTF8String(char*)sqlite3_column_text(compiledStatement, 1)]; 
} 

Cela permettra de résoudre votre problème.

Merci,
Jim.

+1

Cela a exactement le même problème. Vous ne devriez pas vérifier le 'NSString', mais la chaîne C brute. – Joost

+0

Ouais, mon erreur. Maintenant corrigé. Merci JoostK. – Jim

+0

Merci Jim et JoostK, j'ai changé mon code comme ci-dessus. S'il vous plaît noter, je devais changer sqlite3_column_text (compiledStatement, 1) en sqlite3_column_text (compiledStatement, 0), pas tout à fait sûr pourquoi, mais son fonctionnement maintenant. – Stephen

4

Ahhh: p - Je crois - Si vous implémentez l'instruction suivante, vous n'auriez pas de problèmes.

Ce ne sera jamais jeter exception que vous avez mentionné - * Mettre fin application en raison d'une exception non interceptée 'NSInvalidArgumentException', raison: '* + [NSString stringWithUTF8String:]: NULL CSTRING'

Mais rappelez-vous - quand il y a valeur vide - votre chaîne aurait (null) valeur

NSString *bName=[NSString stringWithFormat: @"%s",(char *)sqlite3_column_text(compiledStmt, 0)]; 

Voilà. UpVote si trouvé utile. :)

+0

J'aime ça, mais cela soulève la question de savoir si j'ai 'NULL' dans ma base de données, est-ce que je veux un 'Nil'' NSString * 'ou non .... Hmmm – lol