How do I pass a callback function to sqlite3_exec on iOS 5.1?
Posted
by
John Doh
on Stack Overflow
See other posts from Stack Overflow
or by John Doh
Published on 2012-06-25T21:13:51Z
Indexed on
2012/06/25
21:15 UTC
Read the original article
Hit count: 445
I am new to both xcode/iOS/Objective-C and sqlite. I am trying to teach myself the basics - and I would like to use the sqlite3 wrapper "sqlite3_exec" for a select query. For some reason, I can't find a simple example anywhere of someone doing this.
Basically, the method has a parameter (the third one) for a callback function:
int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
That's fine. I'm no stranger to callbacks. However, I just can't seem to get the syntax down right. I took over one of the view controllers in my iPad (iOS 5.1) xcode (4.3) project, and made the changes shown below:
#import "SecondViewController.h"
#import "sqlite3.h"
#import "AppState.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (int)myCallback:(void *)a_parm argc:(int)argc argv:(char **)argv column:(char **)column
{
return 0;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//grab questionnaire names
char *sql = "select * from QST2Main order by [Name]";
char *err = nil;
sqlite3 *db = [[AppState sharedManager] getgCn];
sqlite3_exec(db, sql, myCallback, nil, &err);
}
Essentially, I want to run a query when this view first loads, to store some data for later use. But, XCode doesn't like the "myCallback" usage at the bottom there. It says: Undeclared Use of Identifier 'myCallback.'
That method is declared in the header file, and I've even tried making it static. Nothing seems to make this error go away. I know I must be doing something fundamentally wrong here, but for the life of me I can't figure out what - I can't even find other code samples in this area that could help me figure out what I'm missing.
Many thanks!
© Stack Overflow or respective owner