C++ MySQL C++;执行带BLOB的插入时连接器崩溃

C++ MySQL C++;执行带BLOB的插入时连接器崩溃,c++,blob,mysql-connector,C++,Blob,Mysql Connector,我遇到了一个问题,试图插入文本作为BLUB值,MySQL C++连接器崩溃了:“访问冲突读取位置”。我在这里见过这样的问题,但没有一个得到回答。下面是一个代码示例: #include "mysql_connection.h" #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/stateme

我遇到了一个问题,试图插入文本作为BLUB值,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;
    }
}
#包括“mysql_connection.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间sql;
void main(){
连接*dbConnection;
司机*司机;
连接*con;
driver=get_driver_instance();
con=驱动程序->连接(“本地主机”、“根目录”、“根目录”);
语句*stmt=con->createStatement();
试一试{
stmt->执行(“使用测试”);
stmt->execute(“DROP TABLE blob_test”);
stmt->execute(“创建表blob_测试(”
id整数主键非空自动递增
“原始BLOB(非空);”;
char-smallBlob[32];
对于(字符i=0;iprepareStatement(“插入到blob_测试(id,原始)值(1,)”;
pstmt->setBlob(1,&str);
pstmt->executeUpdate();
}捕获(sql::SQLException&e){

std::cerr从源代码处编译MySQL cpp连接器的问题已解决

如果您这样做,请仔细阅读(特别是最后的注释)。我还必须将config.h中的几行内容更改为int到char*的隐式转换。还要注意不要将调试和发布混用,例如,如果您在调试中编译应用程序,您应该链接MySQL connector的调试版本,反之亦然

在我链接lib之后,我已经编译了所有内容(启动)
你可能会在dev.mysql.com论坛上读到,在大多数情况下,这是强制连接器工作的唯一方法。在编译时,我遇到了一件事情,证明了这一点,源代码中的include文件与windows binary distributive中的include文件不同。

main
函数不会返回
void
。没关系,它只是一个sam例如,它在pstmt->executeUpdate()行崩溃;我使用VS2010看看这个示例:。我怀疑您不想使用istringstream,因为它在字符串上运行。例如,您的smallBlob[0]是空的。因此,您可能实际插入了空数据。我知道这并不能解释崩溃的原因。它在哪一行崩溃?您是否在
catch
语句中放置了断点?@Blazes:我已经看到了您提出的问题,我想我遇到了相同的问题,并且没有答案。谢谢您的观点[0]数组的元素为null,我已将行更改为smallBlob[I]=50+i;根据崩溃前的调试信息,现在smallBlob包含“23456789:;?@ABCDEFGHIJKLMNOPQ”。to Thomas Mattews:Catch语句尚未到达,因为异常未处理:testMySQLblob.exe中0x74d3eab7处的未处理异常:0xC0000005:访问冲突读取位置0x00000000。