1

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.

4
  • "Provide me solution." You could have asked a bit nicer. Commented Oct 30, 2012 at 5:27
  • 1
    [ProvideMeSolution appendString:@" please"]; Commented Oct 30, 2012 at 5:29
  • Have you run this through the debugger? Does sqlite2_open succeed? Does sqlite3_prepare_v2 succeed? BTW - you really should use sqlite3_open_v2. Commented Oct 30, 2012 at 5:31
  • Yup sqlite2_open and sqlite3_prepare_v2 succeed. Commented Oct 30, 2012 at 5:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.