在C ++中没有为MYSQL数据库正确初始化的类

时间:2017-10-29 06:11:23

标签: c++ mysql class

我正在尝试创建一个类来保存与数据库的连接,这样我就可以使用普通函数来对数据库进行查询。但是,例如,如果我尝试在main中执行此操作,则会出现错误:

\main.cpp|280|error: cannot convert 'int (attribute((stdcall)) )(SOCKET, const sockaddr, int) {aka int (attribute((stdcall)) )(unsigned int, const sockaddr, int)}' to 'MYSQL* {aka st_mysql}' for argument '1' to 'int mysql_query(MYSQL, const char*)'|

以下是我的代码:

class Connection {
    public:
        int init() {
        MYSQL *connect; // Create a pointer to the MySQL instance
        connect=mysql_init(NULL); // Initialise the instance
        /* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/
        if(!connect)    /* If instance didn't initialize say so and exit with fault.*/
        {
            cout << "MySQL Initialization Failed";
            return 1;
        }
        /* Now we will actually connect to the specific database.*/
        connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0);
        /* Following if statements are unneeded too, but it's worth it to show on your
        first app, so that if your database is empty or the query didn't return anything it
        will at least let you know that the connection to the mysql server was established. */
            cout << "[+] Exchange Order Book v1.0\n";
            cout << "[+] Attempting connection to database\n";
        if(connect){
            cout << "[+] Connection Success!\n";
            cout << "[+] Loading initializationn\n";
        }
        else{
            cout << "[!] Connection Failed!\n";
            return 0;
        }
        MYSQL_RES *res_set; /* Create a pointer to recieve the return value.*/
        MYSQL_ROW row;  /* Assign variable for rows. */
       // mysql_query(connect,"SELECT * FROM orderque");
        /* Send a query to the database. */
        unsigned int i = 1; /* Create a counter for the rows */

        }
    };

主():

  Connection.init();
  mysql_query(connect,"SELECT * FROM orderque"); 

奇怪的是,如果我在主要版本中有类数据,它可以100%正常工作,就在我尝试将其设为全局或将其放入类中时,它会给我带来错误

0 个答案:

没有答案
相关问题