1

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
LBes
  • 3,366
  • 1
  • 32
  • 66
Niklas
  • 1,753
  • 4
  • 16
  • 35
  • Have you tried running the query from the command-line or through MySQL workbench? – PaulF Sep 27 '16 at 13:31
  • PaulF --> Yes from workbench and it works – Niklas Sep 27 '16 at 13:40
  • When using workbench do you connect the same way using tcp://localhost:3306 or do you use tcp://127.0.0.1:3306. Although normally the same, MySQL does distinguish between the two & you may need to check you have the same permission set. Have you stepped through the code & checked the value of res after the call to executeQuery? – PaulF Sep 27 '16 at 13:48
  • PaulF--> Yes i use localhost for both. Yes the res has the value "" – Niklas Sep 27 '16 at 14:12
  • Googling that error came up with this : http://stackoverflow.com/questions/18946459/error-reading-characters-of-string – PaulF Sep 27 '16 at 14:25
  • it did not work, but thanks anyway. – Niklas Sep 27 '16 at 14:39
  • What was the code you tried & how did it fail this time? – PaulF Sep 27 '16 at 14:41
  • I tried const_cast(res->getString(2)) but it can not compile, error: 7 IntelliSense: a const_cast can only adjust type qualifiers; it cannot change the underlying type.. Im new to c++ so maybe im thinking wrong? – Niklas Sep 27 '16 at 14:50
  • I can pick out int values so the connection works. – Niklas Sep 27 '16 at 14:54
  • Note that the answer suggested making a copy of the string in a local variable before using it : std::string text(res->getString(1)); cout << const_cast(text.c_str()) << endl; You may not need the const_cast just to print the result. cout << (text.c_str()) << endl; – PaulF Sep 27 '16 at 15:14
  • I get the same error "Unhandled exception at..." for std::string text(res->getString(2)); – Niklas Sep 27 '16 at 15:31
  • Sorry I couldn't help. My copy of Sakila database does not contain a cars table - so I can't check the structure of that - are columns 1 & 2 both strings? – PaulF Sep 27 '16 at 16:24
  • I have tried different tables like actor(standard) etc.. in my example column 1 and 2 is strings. Thanks for the help anyway. – Niklas Sep 27 '16 at 16:45

1 Answers1

4

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;
PaulF
  • 6,673
  • 2
  • 18
  • 29