Iphone sqlite select语句中的错误
我正在尝试从我的数据库中获取数据,但我没有得到解决 我试着这样做 -(无效)信息Iphone sqlite select语句中的错误,iphone,objective-c,Iphone,Objective C,我正在尝试从我的数据库中获取数据,但我没有得到解决 我试着这样做 -(无效)信息 { //[self createEditableCopyOfDatabaseIfNeeded]; NSString *filePath = [self getWritableDBPath]; sqlite3 *database; if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
{
//[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'
*第一次抛出时调用堆栈:我认为firstColumn为NULL,它会导致程序崩溃。您试图检查数据库中的数据是否为空。这实际上很明显,因为您试图获取的数据为空 请尝试使用此代码检查代码中的空值:
if (sqlite3_column_text(compiledStatement, 0)!= nil)
i、 e
试试这个。。。
希望它能帮助……像这样使用
if(sqlite3_step(compiledStatement) == SQLITE_ROW )
{
char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);
NSString *name= [[NSString alloc]initWithUTF8String:firstColumn];
}
我希望它能帮助您尝试这个我认为这段代码有两个问题
- 正如已经指出的,您正在检查是否等于零,而不是 与零不同
- sql函数的顺序应为:
- sqlite3_打开
- sqlite3\u prepare\u v2
- sqlite3_步骤
- sqlite3\u列\u文本
我的数据库不为空我在sqlite浏览器中启动SelectJourneyName fromUserJourney where SyncStatus='1'它给我数据谢谢它帮助我&amresh也告诉我你的答案也接近我的问题但谢谢你的帮助
if(sqlite3_step(compiledStatement) == SQLITE_ROW )
{
char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);
NSString *name= [[NSString alloc]initWithUTF8String:firstColumn];
}