c++ - Mysql 与 C++ 不工作

标签 c++ mysql database

我尝试将我的程序与数据库连接(我有导入mysql连接器,我没有boost),但是当我编译时有很多错误

这是我的代码

#include <stdlib.h>
#include <iostream>


#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

class Database{
public :
 Database(){};
~Database(){};
void query(const sql::SQLString& query, const sql::SQLString &bdd);

};

void Database::query(const sql::SQLString &query, const sql::SQLString &bdd){

try {
    sql::Driver *driver { };
      sql::Connection *con{ };
      sql::Statement *stmt{ };
      sql::ResultSet *res{ };

      /* Create a connection */
      driver = get_driver_instance();
      con = driver->connect("tcp://127.0.0.1:3306", "root", "");

      /* Connect to the MySQL database */
      con->setSchema(bdd);

      stmt = con->createStatement();
      res = stmt->executeQuery(query);

      while (res->next()) {
        cout << "\t... MySQL reponses: ";
        /* Access column data by alias or column name */
        cout << res->getString("content") << endl;
        cout << "\t... MySQL says it again: ";
        /* Access column fata by numeric offset, 1 is the first column */
        cout << res->getString(1) << endl;
      }
      delete res;
      delete stmt;
      delete con;

    } catch (sql::SQLException &e) {
      cout << "# ERR: SQLException a " << __FILE__;
      cout << "(" << __FUNCTION__ << ") a la ligne "
         << __LINE__ << endl;
      cout << "# ERR: " << e.what();
      cout << " (MySQL error code: " << e.getErrorCode();
      cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

    cout << endl;

}

错误:

 C:\Program Files\MySQL\c++\include/cppconn/warning.h:40:0: warning: 
 ignoring #pragma warning  [-Wunknown-pragmas]

  #pragma warning (disable : 4290)
   ^

 In file included from C:\Program Files\MySQL\c++\include/cppconn
 /variant.h:38:0,
             from C:\Program Files\MySQL\c++\include/cppconn
 /connection.h:10,
             from C:\Program Files\MySQL
 \c++\include/mysql_connection.h:30,
             from ..\src\Database.cpp:10:

 C:\Program Files\MySQL\c++\include/cppconn/exception.h:48:0: warning: 
 ignoring #pragma warning  [-Wunknown-pragmas]

  #pragma warning (disable : 4290)


 C:\Program Files\MySQL\c++\include/cppconn/exception.h:52:0: warning: 
 ignoring #pragma warning  [-Wunknown-pragmas]

  #pragma warning(push)


 C:\Program Files\MySQL\c++\include/cppconn/exception.h:53:0: warning: 
 ignoring #pragma warning  [-Wunknown-pragmas]

  #pragma warning(disable: 4275)


 C:\Program Files\MySQL\c++\include/cppconn/exception.h:58:0: warning: 
 ignoring #pragma warning  [-Wunknown-pragmas]

  #pragma warning(pop)

 ^

 In file included from ..\src\Database.cpp:10:0:

 C:\Program Files\MySQL\c++\include/mysql_connection.h:174:2: error: 
 'boost' does not name a type

  boost::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy;

  ^

 C:\Program Files\MySQL\c++\include/mysql_connection.h:178:2: error: 
 'boost' does not name a type

   boost::scoped_ptr< ::sql::mysql::MySQL_Statement > service;

   ^

 C:\Program Files\MySQL\c++\include/mysql_connection.h:180:2: error: 
 'boost' does not name a type

  boost::scoped_ptr< ::sql::mysql::MySQL_ConnectionData > intern; /* 
 pimpl */

  ^

 In file included from C:\Program Files\MySQL\c++\include/cppconn
 /resultset.h:30:0,
             from ..\src\Database.cpp:13:

 C:\Program Files\MySQL\c++\include/cppconn/config.h:95:19: error: 
 conflicting declaration 'typedef long int int32_t'

 typedef __int32   int32_t;
               ^`enter code here`

 c:\mingw\include\stdint.h:31:14: error: 'int32_t' has a previous 
 declaration as 'typedef int int32_t'

 typedef int  int32_t;
          ^

 In file included from C:\Program Files\MySQL\c++\include/cppconn
 /resultset.h:30:0,
             from ..\src\Database.cpp:13:

 C:\Program Files\MySQL\c++\include/cppconn/config.h:99:26: error: 
 conflicting declaration 'typedef long unsigned int uint32_t'

  typedef unsigned __int32 uint32_t;
                      ^


 c:\mingw\include\stdint.h:32:20: error: 'uint32_t' has a previous 
 declaration as 'typedef unsigned int uint32_t'

  typedef unsigned   uint32_t;
                ^

 In file included from C:\Program Files\MySQL\c++\include/cppconn
 /statement.h:30:0,
             from ..\src\Database.cpp:14:

 C:\Program Files\MySQL\c++\include/cppconn/config.h:95:19: error: 
 conflicting declaration 'typedef long int int32_t'

 typedef __int32   int32_t;
               ^


 c:\mingw\include\stdint.h:31:14: error: 'int32_t' has a previous 
 declaration as 'typedef int int32_t'

  typedef int  int32_t;
          ^

 In file included from C:\Program Files\MySQL\c++\include/cppconn
 /statement.h:30:0,
             from ..\src\Database.cpp:14:

 C:\Program Files\MySQL\c++\include/cppconn/config.h:99:26: error: 
 conflicting declaration 'typedef long unsigned int uint32_t'

  typedef unsigned __int32 uint32_t;
                      ^

 c:\mingw\include\stdint.h:32:20: error: 'uint32_t' has a previous 
 declaration as 'typedef unsigned int uint32_t'

 typedef unsigned   uint32_t;'

我认为这不是我的程序,而是图书馆,但也许我犯了一个错误 谢谢你,祝你有美好的一天

最佳答案

显然你的mysqlcpp连接器需要boost,所以从here下载boost并将 boost 文件夹的路径添加到项目的附加包含目录列表中。至于重新定义,请注释掉stdint.h中重新定义的部分。

关于c++ - Mysql 与 C++ 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36521253/

相关文章:

c++ - 使用 typename 作为模板参数

c++ - 从特定库实例化对象时出现段错误

C++ cin 在 while 循环中使用,但控制台屏幕未保持

mysql - 如何通过触发器减少另一个表中的值? (MySQL)

php - 帮助 AJAX 中的 3 个按钮

sql - 用于检测 PostgreSQL 趋势的聚合函数

c++ - 函数原型(prototype)设计 - C++

MYSQL 查询 : How to UNION two table with ORDER BY

database - Postgres 是否支持 jsonb 列类型中的 Ilike 语句?

php - 警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源