无法读取mysql的结果

时间:2016-09-27 13:04:18

标签: c++ mysql

我使用visual c ++,我已经链接到libaries c ++ connector(MySQL Connector C ++ 1.1.7)和boost(boost_1_61_0)。

我使用32位连接器用于mysql,64位连接器根本不起作用。

我有Windows 10(64位)

它编译。<​​/ p>

但是当调试器到达cout << res->getString(1) << endl;行时,它会崩溃,示例来自https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html

代码:

#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;

int main(void)
{
    cout << endl;
    cout << "Running SELECT * from cars" << endl;


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

    /* Create a connection */
    driver = get_driver_instance();
    con = driver->connect("tcp://localhost:3306", "root", "*****");
    /* Connect to the MySQL test database */
    con->setSchema("sakila");

    stmt = con->createStatement();
    res = stmt->executeQuery("SELECT * from cars");
    while (res->next()) {

        cout << res->getString(1) << endl;


    }
    delete res;
    delete stmt;
    delete con;

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

    cout << endl;

    return EXIT_SUCCESS;
}

错误讯息:

Unhandled exception at 0x01327EA6 (msvcp120d.dll) in cppMySql.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC

1 个答案:

答案 0 :(得分:2)

在查看我在评论中提到的原始解决方案后,我设法重新创建了您的情况&amp;发现我得到了完全相同的错误。这就是为我解决的问题:

cout << res->getString(1).c_str() << endl;