请大家帮忙看看,使用mysql connector/c++ 往数据库中插入blob字段出1210错

ll_pp 2011-02-19 10:46:00
如题,有一表test_test,两个字段(id,b),其中b是blob字段
代码段如下:
class DataBuf : public streambuf
{
public:
DataBuf(char * d, size_t s)
{
setg(d, d, d + s);
}
};

void main()
{
......
try
{
sql::SQLString sql = "INSERT INTO test_test (id,b) VALUES(?,?)";

sql::Driver *driver;
sql::Connection *con;

driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306","root","root");
con->setSchema("wg"); //数据库名
sql::PreparedStatement *prep_stmt = con->prepareStatement(sql);

int a = 1;
memcpy(buf,&a,sizeof(a));
memcpy(buf + sizeof(a),&a,sizeof(a));
memcpy(buf + 2 * sizeof(a),&a,sizeof(a));
DataBuf buffer(buf,12);

prep_stmt->setInt(1,1);
prep_stmt->setBlob(2,&stream);
prep_stmt->execute();
}
catch(sql::SQLException &e) {......}
}

运行时出错 ,提示错误代码1210 incorrect arguments to ......
请大家看看这个错误是怎么回事?如果不插入blob字段,则没有问题。谢谢!
...全文
361 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ll_pp 2011-02-20
  • 打赏
  • 举报
回复
啊,对不起,我代码少贴了一行。不是编译错误,是执行错误,如果stream变量没定义,编译通不过的,再贴一次代码
class DataBuf : public streambuf
{
public:
DataBuf(char * d, size_t s)
{
setg(d, d, d + s);
}
};

void main()
{
......
try
{
sql::SQLString sql = "INSERT INTO test_test (id,b) VALUES(?,?)";

sql::Driver *driver;
sql::Connection *con;

driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306","root","root");
con->setSchema("wg"); //数据库名
sql::PreparedStatement *prep_stmt = con->prepareStatement(sql);

int a = 1;
memcpy(buf,&a,sizeof(a));
memcpy(buf + sizeof(a),&a,sizeof(a));
memcpy(buf + 2 * sizeof(a),&a,sizeof(a));
DataBuf buffer(buf,12);

istream stream(&buffer);
prep_stmt->setInt(1,1);
prep_stmt->setBlob(2,&stream);
prep_stmt->execute();
}
catch(sql::SQLException &e) {......}
}
iihero 2011-02-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ll_pp 的回复:]

我在我原先的环境下运行了一下iihero的代码,产生一样的错误。iihero提示我的环境相关的问题,但是我也不知道环境问题在哪里,于是重新下载了最新版的mysql,安装,再运行代码,正确执行了。问题解决了,谢谢iihero!
[/Quote]
解决了就好,这种情况,大多是由于connector版本和mysql版本不完全匹配造成的。以后碰到此类问题,可以把相关版本全部给出,这样大家实测的时候就有根据了。
ll_pp 2011-02-20
  • 打赏
  • 举报
回复
我在我原先的环境下运行了一下iihero的代码,产生一样的错误。iihero提示我的环境相关的问题,但是我也不知道环境问题在哪里,于是重新下载了最新版的mysql,安装,再运行代码,正确执行了。问题解决了,谢谢iihero!
iihero 2011-02-20
  • 打赏
  • 举报
回复
&stream变量 什么? 没定义? 这种代码,稍微debug一下就知道哪儿出错了。
iihero 2011-02-20
  • 打赏
  • 举报
回复
是不是你环境的问题???
我这边用VS2008, mysql5.1.46, connector1.1.0,实测了一下,没有任何问题。

下边是完整的代码,楼主最好有时候能详细的debug一下.
[code=C]#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include "mysql_connection.h"
#include "mysql_driver.h"

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

#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "root"
#define EXAMPLE_PASS "******"
#define EXAMPLE_DB "test"

using namespace std;


class DataBuf : public std::streambuf
{
public:
DataBuf(char* d, size_t s)
{
setg(d, d, d+s);
}
};

int main(int argc, const char **argv)
{

string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);

cout << "Connector/C++ tutorial framework..." << endl;
cout << endl;


try {

/* INSERT TUTORIAL CODE HERE! */
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", user, pass);
stmt = con->createStatement();
stmt->execute("USE " EXAMPLE_DB);
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");

stmt->execute("DROP TABLE IF EXISTS test_test");
stmt->execute("CREATE TABLE test_test(id INT primary key, b BLOB)");
delete stmt;


sql::PreparedStatement* pstmt;
pstmt = con->prepareStatement("INSERT INTO test_test VALUES(?, ?)");
int a = 1;

char buf[80];
memcpy(buf,&a,sizeof(a));
memcpy(buf + sizeof(a),&a,sizeof(a));
memcpy(buf + 2 * sizeof(a),&a,sizeof(a));
DataBuf buffer(buf,12);

std::istream s(&buffer);
pstmt->setInt(1, a);
pstmt->setBlob(2, &s);
pstmt->execute();

delete pstmt;



delete con;


} catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:

- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << "__FUNCTION__" << ") on line " << __LINE__ << endl;
/* Use what() (derived from std::runtime_error) to fetch the error message */
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;

return EXIT_FAILURE;
}

cout << "Done." << endl;
return EXIT_SUCCESS;
}
[/code]
ll_pp 2011-02-20
  • 打赏
  • 举报
回复
没有人遇到过这种问题吗?

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧