sqlite3_prepare_v2问题

时间:2010-07-20 10:07:41

标签: iphone objective-c xcode sqlite

我收到以下错误:

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* + [NSString stringWithUTF8String:]:NULL cString'

在下面的代码行中:

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

我不确定这里发生了什么,我在SQL语句中引用的列包含数据。完整代码如下。

// 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);

2 个答案:

答案 0 :(得分:4)

啊啊:p - 我相信 - 如果你实施以下声明,你就不会遇到麻烦。

这绝不会像你提到的那样抛出异常 - *因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'* + [NSString stringWithUTF8String:]:NULL cString'

但请记住 - 当空值时 - 您的字符串会有(null)

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

就是这样。 UpVote如果发现有帮助。 :)

答案 1 :(得分:3)

处理语句

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

这将解决您的问题。

谢谢,
吉姆。

相关问题