Niklas Niklas - 2 months ago 7
C++ Question

Cant read result from mysql

I use visual c++, i have linked to libaries c++ connector (MySQL Connector C++ 1.1.7) and boost (boost_1_61_0).

I use the 32 bit connector for mysql, 64 bit connector does not work at all.

I have windows 10 (64 bit)

it compiles.

But it crashes when the debugger comes to line

cout << res->getString(1) << endl;
the example is taken from https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html

Code:

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


Error message:

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

Answer

After looking at the original solution I referred to in my comments, I managed to recreate your situation & found I got exactly the same errors. This is what solved it for me :

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