4
votes

I am trying get the data from my database but i am not getting solve out I try like this

-(void)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 get crash And It show me error that reason: '* +[NSString stringWithUTF8String:]: NULL cString' * Call stack at first throw:

4

4 Answers

3
votes

use like this

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


            }

I hope it will help you try this

3
votes

I think firstColumn is NULL , it make program crash . You try to check data in database is null.

3
votes

This is actually obvious because you are trying to fetch data which is NULL Try for this code for checking 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 it helps....

0
votes

I think there are 2 issues with this piece of code.

  • As already pointed out you are checking for equal to nil and not for different from nil.
  • To order of the sql functions should be:
    1. sqlite3_open
    2. sqlite3_prepare_v2
    3. sqlite3_step
    4. sqlite3_column_text
http://www.iosdevelopment.be/sqlite-tutorial/