如何在 C++ 中使用 setBlob() 设置二进制数据 连接器

发布于 2024-07-26 19:58:38 字数 90 浏览 9 评论 0原文

我知道之前已经有人问过这个问题,并且我尝试实现该解决方案,但是当我调用 ps->executeUpdate() 时,我只是收到异常错误。 有人有明确的例子吗?

I know this has been asked before, and I tried to implement the solution, but I just get exception errors when I call ps->executeUpdate(). Has anyone got an explicit example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澜川若宁 2024-08-02 19:58:38

这篇文章有点旧,但我遇到了同样的问题。 我采用了上面的方法,但它不太适合我的情况,我的情况是尝试获取一个向量并将其用于流。 我所做的就是获取 UUID 并将其转换为 16 字节二进制版本以在表中使用。 使用上面的方法,我发现只有一半的缓冲区被填充。

我最终使用了stringstream

std::vector<unsigned char>  convertedId;
std::stringstream           stream;

// convertedId has been populated with the 16 byte binary version
stream = std::stringstream(std::string(convertedId.begin(), convertedId.end()));
// Parameter 1 is BINARY(16)
pStatement->setBlob(1, &stream);

还有一些其他事情需要记住。 在调用 execute 变体之一之前,不会访问该流。 因此,您需要保留流,直到运行execute

希望这会对某人有所帮助并节省他们的时间。

This post is a bit old, but I ran across the same question. I employed the method above and it didn't quite work right for my case, which was trying to take a vector and use that for the stream. What I was doing was taking a UUID and converting it into a 16 byte binary version to use in the table. Using the method above, I found that only half my buffer was being populated.

I ended up using a stringstream.

std::vector<unsigned char>  convertedId;
std::stringstream           stream;

// convertedId has been populated with the 16 byte binary version
stream = std::stringstream(std::string(convertedId.begin(), convertedId.end()));
// Parameter 1 is BINARY(16)
pStatement->setBlob(1, &stream);

A few other things to keep in mind. The stream is not accessed until one of the execute variants is called. So you'll need to keep the stream around until you have run execute.

Hopefully this will help someone and save them time.

初与友歌 2024-08-02 19:58:38

抱歉,马修 - 我假设了这个问题的先前答案(由 elrohin 提供)。 也许我应该对此做出回应。 无论如何,这里是他建议的代码:

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


// prepare sql update statement etc. and set the data pointer
string* pData = ; // ...not part of the original answer

DataBuf buffer((char*)pData->data(), pData->length());
istream stream(&buffer);
ps->setBlob(1, &stream);
ps->executeUpdate(); // This causes an exception in free.c

我正在使用 VS9 和最新的(测试版)连接器/cpp 调试库。 我还尝试过使用 char* 而不是字符串。

Sorry Matthew - I assumed the previous answer to this question (by elrohin). Maybe I should have replied to that. Anyway heres the code he suggested:

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


// prepare sql update statement etc. and set the data pointer
string* pData = ; // ...not part of the original answer

DataBuf buffer((char*)pData->data(), pData->length());
istream stream(&buffer);
ps->setBlob(1, &stream);
ps->executeUpdate(); // This causes an exception in free.c

I'm using VS9 with the latest (beta) connector/cpp debug libs. I've also tried using char* instead of string.

眼眸印温柔 2024-08-02 19:58:38

这段代码对我来说效果很好:


  Driver *driver;
  Connection *conn;

  driver = get_driver_instance();
  conn = driver->connect("tcp://127.0.0.1:3306", "root", "root");

  std::auto_ptr use_stmt(conn->createStatement());
  use_stmt->execute("USE world");

  std::auto_ptr stmt(conn->prepareStatement("INSERT INTO terrain_texture_tiles_0 (data) VALUES(?)"));
  std::string value("A\0B", sizeof("A\0B") - 1);
    std::istringstream tmp_blob(value);
    stmt->setBlob(1, &tmp_blob);
    stmt->execute(); 

希望它有帮助...... Jaroslav Pribyl

This code works fine for me:


  Driver *driver;
  Connection *conn;

  driver = get_driver_instance();
  conn = driver->connect("tcp://127.0.0.1:3306", "root", "root");

  std::auto_ptr use_stmt(conn->createStatement());
  use_stmt->execute("USE world");

  std::auto_ptr stmt(conn->prepareStatement("INSERT INTO terrain_texture_tiles_0 (data) VALUES(?)"));
  std::string value("A\0B", sizeof("A\0B") - 1);
    std::istringstream tmp_blob(value);
    stmt->setBlob(1, &tmp_blob);
    stmt->execute(); 

hope it helps ... Jaroslav Pribyl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文