mercredi 22 avril 2015

cannot fetch value from sqlite in IOS

Table values:

ID=1 CUSTOMERID=1 NAME=John EMAIL=email USERNAME=usernaeme

I am using this code to fetch customerId from Usertable with this code

@try {
    //CustomerIdField=@"admin";
   // customerUsername=@"admin";
    NSLog(@"the value of customerusername is %@",customerUsername);
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docsDir = [dirPaths objectAtIndex:0];
NSLog(@"inside function");
databasePath  = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"EBook.db" ]];
    const char *dbpath;
    @try {
        dbpath = [databasePath UTF8String];
        NSLog(@"the  const char is %s",dbpath);
    }
    @catch (NSException *exception) {
        NSLog(@"the exception3 is %@",exception);
    }


    if (sqlite3_open(dbpath, &Ebookreaderdb) == SQLITE_OK)

    {
    NSString *selectSQL = [NSString stringWithFormat: @"SELECT CUSTOMERID FROM Usertable WHERE EMAIL=\"%@\"",customerUsername];


        sqlite3_stmt  *selstatement;
    const char *select_stmt = [selectSQL UTF8String];
    //NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    if (sqlite3_prepare_v2(Ebookreaderdb,
                           select_stmt, -1, &selstatement, NULL ) == SQLITE_OK)
    {

        NSLog(@"inside sqlite OK"); //this prints in log
        if (sqlite3_step(selstatement) == SQLITE_ROW)
        {

            NSLog(@"inside sqlite ROW"); // this is also printing in log
            NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selstatement,1)];
            NSLog(@"val is %@",userInfoStr);
          char *tmp = sqlite3_column_text(selstatement,1);
            if (tmp == NULL)
                CustomerIdField = nil;
            else
                CustomerIdField = [[NSString alloc] initWithUTF8String:tmp];



             CustomerIdField = [[NSString alloc] initWithUTF8String:
                             (const char *) sqlite3_column_text(selstatement,1)];



            NSLog(@"inside customer is %@",CustomerIdField);
           // [resultArray addObject:name];

        }

        else{
            NSLog(@"Not found");
          //  return nil;
        }
        sqlite3_reset(selstatement);
    }

But i am getting this exception Newpjtonfriday[2165:84017] the exception is 2 *** +[NSString stringWithUTF8String:]: NULL cString

I googled with the above result and everywhere it is saying that the value is null that is why the exception occurs. but in my case the value is there.

Because the code NSLog(@"inside sqlite ROW");

is coming in log meaning that a row exists in table. But cannot fetch it.

Please help

Aucun commentaire:

Enregistrer un commentaire