开发者

problem while loading data stored in data base into table view

hii every one

i am using following code to loading data stored in data base into table view but its crasing when i re-launch the application

InsertRecord is the instance of class 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(selectstm开发者_StackOverflow中文版t) == 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);
}

application is crashing at the following line

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

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


You need to check for null data in your database;

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


Test if the sqlite3_column_text(selectstmt, 1) return an valid string.


(char *)sqlite3_column_text(selectstmt, 1) should be given nil (null) value.

So, Add validation when you get data from database.

Like,

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

}
else
{
    InsertRecord.strFirstName = @"";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜