1

I've stuck with a problem, trying to insert text as blob value, MySQL C++ connector crashed with exception: "Access violation reading location". I've seen questions like this here, but none has been answered. Here is a code sample:

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

#include <string>
#include <iostream>
#include <sstream>

using namespace sql;

void main(){
    Connection  *dbConnection;
    Driver *driver;
    Connection *con;

    driver = get_driver_instance();
    con = driver->connect("localhost", "root", "");
    Statement *stmt = con->createStatement();
    try{
        stmt->execute("USE test");
        stmt->execute("DROP TABLE blob_test");
        stmt->execute("CREATE TABLE blob_test("
                        "id         INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,"
                        "original   BLOB NOT NULL);");
        char smallBlob[32];

        for (char i = 0; i < sizeof(smallBlob); ++i)
        {
        smallBlob[i] = i;
        }
        std::istringstream str(smallBlob);
        PreparedStatement *pstmt = con->prepareStatement("INSERT INTO blob_test(id, original) VALUES (1, ?)");

        pstmt->setBlob( 1, &str);
        pstmt->executeUpdate();
    }catch(sql::SQLException &e){
        std::cerr << "# ERR: " << e.what();
        std::cerr << " (MySQL error code: " << e.getErrorCode();
        std::cerr << ", SQLState: " << e.getSQLState() << " )" << std::endl;
    }
}

Thanks in advance!

7
  • The main function does not return void. Commented Apr 2, 2013 at 18:44
  • It doesn't matter, it's just a sample. And it's crashed on the line pstmt->executeUpdate(); I use VS2010 Commented Apr 2, 2013 at 18:49
  • Have a look at this example: stackoverflow.com/questions/1121142/…. I suspect that you do not really want to use istringstream as that operates on strings. E.g., your smallBlob[0] is a NULL. So, you may actually be inserting NULL data. Doesn't explain the crash, I know. Commented Apr 2, 2013 at 19:09
  • At which line does it crash? Did you put a breakpoint inside the catch statement? Commented Apr 2, 2013 at 19:45
  • @Blazes: I've seen the question you gave, I think I meet with the same problem and there was no answer. Thank you for your point that [0] element of the array is null, I've changed the line to smallBlob[i] = 50 + i; and now smallBlob contains '23456789:;<=>?@ABCDEFGHIJKLMNOPQ' according to debug info before crash. To Thomas Mattews: Catch statement haven't reached, because exception is unhandled: Unhandled exception at 0x74d3eab7 in testMySQLblob.exe: 0xC0000005: Access violation reading location 0x00000000. Commented Apr 2, 2013 at 19:58

1 Answer 1

0

Problem solved compiling MySQL cpp connector from sources.

If you go that way read carefully this and this (specially comments in the end). I also had to changed a couple of lines in config.h to implicit cast of int to char*. Also be attentive don't mix Debug and Release, for example if you compile your application in Debug, you should link Debug version of MySQL connector and vice-a-verse.

After I linked lib that I had compiled everything launched ) You may read on dev.mysql.com forums that in most cases it's the only way force the connector to work. While compiling I met with the thing that prove this, include files in source code is different from includes in windows binary distributive.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.