我遇到了一个问题,尝试将文本作为 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;
}
}
提前致谢!
最佳答案
从源代码编译 MySQL cpp 连接器的问题已解决。
如果你走那条路,请仔细阅读 this和 this (最后特别评论)。我还必须更改 config.h 中的几行,以将 int 隐式转换为 char*。还要注意不要混用 Debug 和 Release,例如,如果您在 Debug 中编译应用程序,则应链接 MySQL 连接器的 Debug 版本,反之亦然。
在我链接 lib 之后,我编译了所有启动的东西) 您可能会在 dev.mysql.com 论坛上看到,在大多数情况下,这是强制连接器工作的唯一方法。在编译时我遇到了证明这一点的事情,源代码中的包含文件不同于 Windows 二进制分发包中的包含文件。
关于c++ - MySQL C++ 连接器在使用 BLOB 执行 INSERT 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15771880/