我遇到了一个问题,试图将文本作为 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;
}
}
提前致谢!