问题,同时存储在数据库到表视图加载数据
问题描述:
HII每一个问题,同时存储在数据库到表视图加载数据
我使用下面的代码保存在数据库到表视图加载数据,但其crasing当我重新启动应用程序
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)];
崩溃实例3210
崩溃日志is--终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: '* + [NSString的stringWithUTF8String:]:NULL CSTRING'
答
您需要检查数据库中的空数据;
// 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
答
测试如果sqlite3_column_text(selectstmt, 1)
返回一个有效的字符串。
答
(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 = @"";
}