简体   繁体   中英

Using sqlite3 in visual c++

I am using visual studio 2015 and i want to use sqlite3 with it, i have managed to integrate sqlite3 into it, but i can only use it through native c style, i cannot use sqlite using classes as we do in c++. There is a table test in my database which contain values (id int , id1 int , name string) For example this program runs fine

void forprinting(string a)
{
    cout  << a<<"\n";
}
 int callback(void *NotUsed, int argc, char **argv, char **azColName) {
    int i;
    string testing;
    string test2;
       for (i = 0; i<argc; i++) 
       {
          testing = testing + azColName[i];//irrelevant
          test2 += argv[i];
       }
    forprinting(test2);
    printf("\n");
    return 0;
}



int main()
{
    char *zErrMsg = 0;
    sqlite3 *db;
    int rc;
    rc = sqlite3_open("DATA.db", &db);
    char *data;
    const char *sql;
    sql = "SELECT * FROM test;";
    sqlite3_exec(db, sql, callback, 0, &zErrMsg);
     sqlite3_close(db);
    system("pause");
    return 0;
}

The output of this program is 0 0 test which is fine but when i try to implement the same logic using classes i get the error here

#include <iostream>
#include <stdio.h>
using namespace std;
void forprinting(string a) {
    cout << a << "\n";
} 
class testingSqlite {
    public :
    int callback(void *NotUsed, int argc, char **argv, char **azColName) {
        int i;
        string testing;
        string test2;
        for (i = 0; i<argc; i++) {

            testing = testing + azColName[i];
            test2 += argv[i];
        }
        forprinting(test2);
        printf("\n");
        return 0;
    }
    void initiate() {
        char *zErrMsg = 0;
        sqlite3 *db;
        int rc;
        rc = sqlite3_open("DATA.db", &db);
        char *data;
        const char *sql;
        sql = "SELECT * FROM test;";
        sqlite3_exec(db, sql, callback, 0, &zErrMsg);
        sqlite3_close(db);

    }
};
int main()
{
    testingSqlite test1;
    test1.initiate();
    system("pause");
    return 0;
}

I know its a bad practice to define functions in class but i was in hurry. And it is giving

Error C3867 'testingSqlite::callback': non-standard syntax; use '&' to create a pointer to member



Error (active)      argument of type "int (testingSqlite::*)(void *NotUsed, int argc, char **argv, char **azColName)" is incompatible with parameter of type "int (*)(void *, int, char **, char **)"

I tried to change the function arguments to fix it but nothing yet works.

This:

sqlite3_exec(db, sql, callback, 0, &zErrMsg);

Should be:

sqlite3_exec(db, sql, &testingSqlite::callback, 0, &zErrMsg);

The latter is the correct "member of" syntax. If you ever could use the former, it was nonstandard (some compilers did allow it). That said, it won't work either because the function signature does not indicate it can be a member function.

Instead, you can use the "void *" of the parameter list.

That is, you must create something like

int callback(void *o, int argc, char **argv, char **cname)
{
 testingSqlite op* = reinterpret_cast<testingSqlite *>(o);


 return op->callback( argc, argv, cname );
}

This means your callback in tesingSqlite doesn't take the void *

You also provide c style callback as the function pointer, not your member function. You also must provide the 'this' pointer as the 4th parameter (it becomes the void * when it calls you back), like:

sqlite3_exec( db, sql, callback, this, &zErrMsg );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM