Reputation: 479
I have problem while selecting data from database. I created the "foldertable" in database with following query
CREATE TABLE foldertable(fid integer primary key autoincrement,foldername text);
and here is my code to get foldername according to fid(folderid)
-(NSString *)getFolderFromDatabase:(int)folderId
{
NSString * retriveFolderName;
UIApplication * application=[UIApplication sharedApplication];
ScrapMemoAppDelegate * appDelegate=application.delegate;
NSString * destinationPath=[appDelegate getDestinationPath];
sqlite3 * database;
int retriveWhere=folderId;
if(sqlite3_open([destinationPath UTF8String], &database)==SQLITE_OK)
{
const char * query="select foldername from foldertable where fid = ?;";
sqlite3_stmt * statement;
if(sqlite3_prepare_v2(database, query, -1, &statement, NULL)==SQLITE_OK)
{
if (sqlite3_step(statement)==SQLITE_DONE)
{
sqlite3_bind_int(statement, 1, retriveWhere);
retriveFolderName=[NSString stringWithCString:sqlite3_column_text(statement,0) encoding:NSASCIIStringEncoding];
}
else
{
NSLog(@"Error %s",sqlite3_errmsg(database));
}
sqlite3_reset(statement);
}
sqlite3_close(database);
}
return retriveFolderName;
}
But I get null foldername while I fired same query on terminal it gives proper name.Please provide me solution.
Upvotes: 1
Views: 576
Reputation: 318774
From looking at the code, the primary issue is that you try to execute (step) the query and then you try to bind the value for fid
. You need to do the bind first.
Upvotes: 1