【问题标题】:problem while loading data stored in data base into table view将存储在数据库中的数据加载到表视图中时出现问题
【发布时间】:2011-05-03 20:32:54
【问题描述】:

大家好

我正在使用以下代码将存储在数据库中的数据加载到表视图中,但是当我重新启动应用程序时它会崩溃

InsertRecord 是类 insertUpdateDelete 的实例

+ (void) getInitialDataToDisplay:(NSString *)dbPath 
{


    iICS_testAppDelegate *appDelegate = (iICS_testAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        const char *sql = "select * from tbl_Users";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                insertUpdateDelete *InsertRecord = [[insertUpdateDelete alloc] initWithPrimaryKey:primaryKey];

                InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
                InsertRecord.strMiddleName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
                [appDelegate.arrObjects addObject:InsertRecord];
                [InsertRecord release];
            }
        }
    }
    else
    {
        sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }
    NSLog(@"arrObjects----%@",appDelegate.arrObjects);
}

应用程序在以下行崩溃

 InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];

crash log is-- Terminating app due to unaught exception 'NSInvalidArgumentException', reason: '* +[NSString stringWithUTF8String:]: NULL cString'

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    您需要检查数据库中的空数据;

    // load char in temporary variable
    char *tempChar = sqlite3_column_text(selectstmt, 1);
    
    if(tempChar == null)  // test for null
        InsertRecord.strFirstName = nil;
    else
        InsertRecord.strFirstName = [NSString stringWithUTF8String:tempChar];
    
    // go to the next field in the database
    tempChar = sqlite3_column_text(selectstmt, 2);
    
    // do the same type of test
    

    【讨论】:

      【解决方案2】:

      测试sqlite3_column_text(selectstmt, 1) 是否返回有效字符串。

      【讨论】:

        【解决方案3】:
        (char *)sqlite3_column_text(selectstmt, 1) should be given nil (null) value.
        

        所以,当您从数据库中获取数据时添加验证。

        喜欢,

        if ((char *)sqlite3_column_text(selectstmt, 1) != nil)
        {
            InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
        
        }
        else
        {
            InsertRecord.strFirstName = @"";
        }
        

        【讨论】:

          猜你喜欢
          • 2016-02-01
          • 1970-01-01
          • 2012-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-11
          • 1970-01-01
          相关资源
          最近更新 更多