我正在尝试从我的数据库中获取数据,但我没有得到解决 我试试这个
- (无效)信息
{
//[self createEditableCopyOfDatabaseIfNeeded];
NSString *filePath = [self getWritableDBPath];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
NSString *qu = @"Select Name FROM empl where SyncStatus ='1'";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [qu UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);
if (firstColumn==nil)
{
NSString *name= [[NSString stringWithUTF8String:firstColumn]autorelease];
NSLog(@"%c",name);
}
if(sqlite3_step(compiledStatement) != SQLITE_ROW )
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"User is not valid" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
它会崩溃而且
它向我显示错误原因:' * + [NSString stringWithUTF8String:]:NULL cString'
* 首先调用堆栈:
答案 0 :(得分:3)
我认为firstColumn是NULL,它会导致程序崩溃。您尝试检查数据库中的数据是否为空。
答案 1 :(得分:3)
这实际上是显而易见的,因为您正在尝试获取NULL数据 尝试使用此代码检查代码中的NULL:
if (sqlite3_column_text(compiledStatement, 0)!= nil)
即
if (sqlite3_column_text(compiledStatement, 0)!= nil)
char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);
else
char *firstColumn = @"";
NSString *name= [[NSString stringWithUTF8String:firstColumn]autorelease];
NSLog(@"%c",name);
试试这个...... 希望它有所帮助......
答案 2 :(得分:3)
像这样使用
if(sqlite3_step(compiledStatement) == SQLITE_ROW )
{
char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);
NSString *name= [[NSString alloc]initWithUTF8String:firstColumn];
}
我希望它会帮助你尝试这个
答案 3 :(得分:0)
我认为这段代码存在两个问题。
- sqlite3_open
- sqlite3_prepare_v2
- sqlite3_step
- sqlite3_column_text
醇>