MySQL C ++ Connector崩溃执行INSERT与BLOB

时间:2013-04-02 18:43:23

标签: c++ blob mysql-connector

我遇到了一个问题,试图将文本作为blob值插入,MySQL C ++连接器崩溃,异常:“访问冲突读取位置”。我在这里看过这样的问题,但没有人回答。这是一个代码示例:

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

提前致谢!

1 个答案:

答案 0 :(得分:0)

问题解决了从源代码编译MySQL cpp连接器。

如果你这样做,请仔细阅读thisthis(特别是最后的评论)。我还必须在config.h中将几行更改为将隐式转换为int到char *。另外要注意不要混合使用Debug和Release,例如,如果在Debug中编译应用程序,则应链接Debug版本的MySQL连接器和反之亦然。

在我链接了lib后,我编译了所有已启动的内容) 您可以在dev.mysql.com论坛上阅读,在大多数情况下,这是强制连接器工作的唯一方法。在编译时我遇到了证明这一点的东西,源代码中的包含文件与windows二进制分发中的包含不同。