error in callback function refrance

I starting learn C/C++ sqlite API. There is a simple example for executing queries in database:
1
2
3
4
5
6
7
8
9
10
11
12
13
callbackfunction:

static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
   for(int i = 0; i < argc; i++)
   {
     std::cout << szColName[i] << " = " << argv[i] << std::endl;
   }

   std::cout << "\n";

   return 0;
}


executing queries:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sqlite3 *db;
   char *szErrMsg = 0;
   // open database
   int rc = sqlite3_open("askyb.db", &db);
   if(rc)
   {
     std::cout << "Can't open database\n";
   } else {
     std::cout << "Open database successfully\n";
   } 
   // prepare our sql statements
   const char *pSQL[6];
   pSQL[0] = "CREATE TABLE Employee(Firstname varchar(30), Lastname varchar(30), Age smallint)";
   pSQL[1] = "INSERT INTO Employee(Firstname, Lastname, Age) VALUES ('Woody', 'Alan', 45)";
   pSQL[2] = "INSERT INTO Employee(Firstname, Lastname, Age) VALUES ('Micheal', 'Bay', 38)";
   pSQL[3] = "SELECT * FROM Employee";
   // execute sql
   for(int i = 0; i < 4; i++)
   {
     rc = sqlite3_exec(db, pSQL[i], callback, 0, &szErrMsg);
     if(rc != SQLITE_OK)
     {
       std::cout << "SQL Error: " << szErrMsg << std::endl;
       sqlite3_free(szErrMsg);
       break;
     }   
   }


Thats works nice. I'm Trying to write my own class for database connection. This
is my class header[abbr]:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Connection
{
  public:
	//for Inserting data to db
	void to_database(Glib::ustring);
	//for Importing data from db
	vector< pair<Glib::ustring, Glib::ustring> > from_database(Glib::ustring);
	vector< pair<Glib::ustring, Glib::ustring> > get_last_trans_result(Glib::ustring) const;
	//manage the result of query
	int callback(void *NotUsed, int argc, char **argv, char **szColName);
	private:
		sqlite3 *db;
		char *szErrMsg;
		int rc;
		//below keeps result
		vector< pair<Glib::ustring, Glib::ustring> > query_result;
};


Definition of callback:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int Connection::callback(void *NotUsed, int argc, char **argv, char **szColName)
{
	query_result.clear();
	if(argc)	
	{
	//add name and value
		Glib::ustring col, val;
		for(int i = 0; i < argc; i++)
		{
			col = szColName[i]; val = argv[i];
		    pair<Glib::ustring, Glib::ustring> col_val_pr(col, val); 
			query_result.push_back(col_val_pr);  //add both
		}
	}
	return 0;
}

And how I use the callback (to_database):
1
2
3
4
void Connection::to_database(Glib::ustring query_string)
{
	rc = sqlite3_exec(db, query_string.c_str(), callback, 0, &szErrMsg);
}


But my class has following error:
1
2
In member function ‘void sqlite_access::Connection::to_database(Glib::ustring)’:
database.cpp:26: error: argument of type ‘int (sqlite_access::Connection::)(void*, int, char**, char**)’ does not match ‘int (*)(void*, int, char**, char**)’


How should I pass the callback function to sqlite_exec?
What's the problem?
thnx in advance
1
2
3
4
5
6
7
void foo::bar(int arg){ //the method bar in class for

}
//is equivalent to 
void bar( foo *this, int arg ){ //as you see, you need 2 arguments

}
So your callback does not respect the prototype.

A work-around
1
2
3
4
5
6
7
8
9
int callback(void *obj, int argc, char **argv, char **szColName){ //global function (friend if needed)
  Connection *connection = static_cast< Connection* > (obj);
//...
}

void Connection::to_database(Glib::ustring query_string)
{
	rc = sqlite3_exec(db, query_string.c_str(), callback, this, &szErrMsg);
}
Last edited on
Topic archived. No new replies allowed.