Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a callback function to sqlite3_exec on iOS 5.1?

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!

like image 597
John Doh Avatar asked Apr 22 '26 14:04

John Doh


1 Answers

The callback needs to be a C function. Try

int myCallback(void *a_parm, int argc, char **argv, char **column)
{
    return 0;
}
like image 157
Tammo Freese Avatar answered Apr 24 '26 15:04

Tammo Freese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!