Confused in select sqlite statement

I am trying to get data from my database, but I am not getting a solution. I am trying so hard

- (invalid) information

{ //[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); } 

it crashes. It shows me an error: " * + [NSString stringWithUTF8String:]: NULL cString '* The call stack on the first throw:

+4
source share
4 answers

use

 if(sqlite3_step(compiledStatement) == SQLITE_ROW ) { char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0); NSString *name= [[NSString alloc]initWithUTF8String:firstColumn]; } 

Hope this helps you try this.

+3
source

I think firstColumn is NULL, this causes the program to crash. You are trying to check the data in the database is null.

+3
source

This is really obvious because you are trying to get data that is NULL. Try this code to check for NULL in your code:

 if (sqlite3_column_text(compiledStatement, 0)!= nil) 

i.e.

 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); 

try this ... Hope this helps ...

+3
source

I think there are two problems with this piece of code.

  • As already mentioned, you check the value to be zero, and not different from zero.
  • To order sql functions, you must have:
    • sqlite3_open
    • sqlite3_prepare_v2
    • sqlite3_step
    • sqlite3_column_text
    You can also read about this performance line at: http://www.iosdevelopment.be/sqlite-tutorial/
0
source

All Articles