Heyall. I'm trying to use the sqlite3_exec function and am having trouble with the callback feature.

I've placed my code below. I realise the callback function provides and argument void *data and the

sqlite3_exec function provides a const char *data variable.

That is where I'm getting the error. I've searched the web for a similar example but I think this is occuring because I haven't cast the address of 'const char data' to the 'void data variable'. I'm just not sure where to put it.

I've provided all my code below. The error message (from the build log) is also below. This error occurs when compiling.

ERROR MESSAGE FROM BUILD LOG.

C:\Documents and Settings\xxxx\My Documents\codeblock files\SQLITE_EXEC\main.cpp:38: error: invalid conversion from 'const void*' to 'void*'
C:\Documents and Settings\xxxx\My Documents\codeblock files\SQLITE_EXEC\main.cpp:38: error: initializing argument 4 of 'int sqlite3_exec(sqlite3*, const char*, int (*)(void*, int, char**, char**), void*, char**)'
C:\Documents and Settings\xxxx\My Documents\codeblock files\SQLITE_EXEC\main.cpp: At global scope:
C:\Documents and Settings\xxxx\My Documents\codeblock files\SQLITE_EXEC\main.cpp:63: error: expected constructor,

CALLBACK FUNCTION

int callback(void *data, int ncols, char** values, char** headers);

int main()

{

sqlite3 *db;
int rc, kt;
char *err;
const char *sql;


rc = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE, NULL);   //this works fine

if(SQLITE_OK == sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE, NULL));
{
    cout << "Database Opened Fine" << endl;
}


sql = "select * from foods";

const char* data ="Callback Function called";

kt = sqlite3_exec(db,sql, callback,data,&err);


    if( kt!=SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", err);
        /* This will free err if assigned */
        if (err)
           free(err);
      }

system("pause>nul");

}

// callback function below

int callback(void *data, int ncols, char** values, char** headers)
{
    data = 0;
cout << "start of callback" << endl;

return 0;

}

Just note, where i've opened table tests.db it should be foods.db and I still get the same error.

Also, if I put a zero '0' in place of 'data' in kt = sqlite3_exec(db,sql, callback,data,&err); the function works. My only concern is being able to use this function in the future (with the callback data variable) for a message, etc.

Any clues as to how this could be done.

Thanks

Here is a working code snippet. Just remove the const keyword when declaring data should solve the problem.

char data[] = "Callback Function called";

It works. Actually, both the below work.

char data[] = "Callback Function called";

char *data = "Callback Function called";

Thanks.

which one you use might depend on what the callback function is going to do with it. If the callback function is going to change anything in the string then use the first one because the second is (probably) a pointer into read-only memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.