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
stringWithFormatto build a SQL statement. Use?placeholders andsqlite3_bind_textfunction (which, confusingly, uses one-based index, unlike the zero-based index ofsqlite3_column_text).sqlite3_reset(used to reset bound columns to prepared statement, so the statement can be performed again) and you clearly meant to callsqlite3_finalize(which frees memory). Replaceresetwithfinalize. Also, did you mean to open the database here and not close it? Personally, I'd separate opening of a database from the performing of SQL statements, but do whatever you want, but just make sure that every open has its corresponding close call. Needless to say, these annoying little details are what makes libraries like FMDB so compelling...