MySQL连接器/ C ++在CodeBlocks中不起作用

时间:2017-04-07 18:44:34

标签: c++ mysql codeblocks

我已从此网站https://dev.mysql.com/downloads/connector/cpp/

安装了最新的MySQL连接器/ c ++ 1.1.8

这是我的main.cpp

#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>
#include <stdlib.h>
/* MySQL Connector/C++ specific headers */
#include <driver.h>
#include <connection.h>
#include <statement.h>
#include <prepared_statement.h>
#include <resultset.h>
#include <metadata.h>
#include <resultset_metadata.h>
#include <exception.h>
#include <warning.h>
#define DBHOST "removed"
#define USER "removed"
#define PASSWORD "removed"
#define DATABASE "removed"
#define NUMOFFSET 1
#define COLNAME 1
using namespace std;
using namespace sql;
static void retrieve_data_and_print (ResultSet *rs, int type, int colidx, 
string colname)

 /* retrieve the row count in the result set */
 cout << "\nRetrieved " << rs -> rowsCount() << " row(s)." << endl;

 cout << "\nTestColumnName" << endl;
 cout << "--------" << endl;

 /* fetch the data : retrieve all the rows in the result set */
 while (rs->next()) {
    if (type == NUMOFFSET) {
                   cout << rs -> getString(colidx) << endl;
    } else if (type == COLNAME) {
                   cout << rs -> getString(colname) << endl;
     } // if-else
 } // while

cout << endl;
 }

int main(int argc, const char *argv[]) {
Driver *driver;
Connection *con;
Statement *stmt;
ResultSet *res;
PreparedStatement *prep_stmt;
Savepoint *savept;

int updatecount = 0;

/* initiate url, user, password and database variables */
string url(argc >= 2 ? argv[1] : DBHOST);
const string user(argc >= 3 ? argv[2] : USER);
const string password(argc >= 4 ? argv[3] : PASSWORD);
const string database(argc >= 5 ? argv[4] : DATABASE);

try {
    driver = get_driver_instance();
/* create a database connection using the Driver */
    con = driver -> connect(url, user, password);

    /* turn off autocommit */
    con -> setAutoCommit(0);

    cout << "Database connection\'s autocommit mode = " << con -> 
   getAutoCommit() << endl;

    // select database schema
    con -> setSchema(database);

    // create a statement object
    stmt = con -> createStatement();

    cout << "Executing Query: \"SELECT * FROM organizations\" ... " << endl;

    /* run query */
    res = stmt -> executeQuery ("SELECT * FROM organizations");

    cout << "Retrieving the result set ..." << endl;
    retrieve_data_and_print (res, NUMOFFSET, 1, string("TestColumnName"));

} catch (SQLException &e) {
    cout << "ERROR: SQLException in " << __FILE__;
    cout << " (" << __func__<< ") on line " << __LINE__ << endl;
    cout << "ERROR: " << e.what();
    cout << " (MySQL error code: " << e.getErrorCode();
    cout << ", SQLState: " << e.getSQLState() << ")" << endl;

    if (e.getErrorCode() == 1047) {
        /*
        Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)
        Message: Unknown command
        */
        cout << "\nYour server does not seem to support Prepared Statements 
    at all. ";
        cout << "Perhaps MYSQL < 4.1?" << endl;
    }

    return EXIT_FAILURE;
 } catch (std::runtime_error &e) {

    cout << "ERROR: runtime_error in " << __FILE__;
    cout << " (" << __func__ << ") on line " << __LINE__ << endl;
    cout << "ERROR: " << e.what() << endl;

        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

当我尝试构建示例代码时,会出现此错误消息:

error displayed in Build message

问题似乎出现在MySQL连接器/ c ++的头文件中。 代码块显示的错误在这些头文件

config.h

也在这。

stdint.h

1 个答案:

答案 0 :(得分:0)

此错误通常与64位计算机上的mingw32有关。您需要使用mingw64 http://mingw-w64.org/doku.php/download并适当地设置工具链可执行文件。