使用 ODBC 将二进制 std::string 插入 BLOB 列

发布于 2024-07-16 18:29:15 字数 230 浏览 6 评论 0原文

有人可以给我一个如何使用 ODBC 和 C++ 将二进制 std::string 插入 BLOB 列的示例吗?

编辑:

如何将二进制 std::string 插入 BLOB 的重复项(MySQL )

can somebody give me an example how to insert binary std::string using ODBC and C++ into BLOB column, please?

EDIT:

Duplicate of How to INSERT binary std::string into BLOB (MySQL).

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

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

发布评论

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

评论(1

强辩 2024-07-23 18:29:15

如果它确实是一个二进制字符串,并且其中有实际的二进制数据(例如嵌入的空值),那么您需要小心不要使用任何可以将其视为 C 字符串的 std::string 成员(例如使用终止 null),因为您显然最终会丢失数据。

std::string 类并不真正适合此目的 - 它是在假设您正在使用的实际上是一个字符串的情况下编写的 - 因为您使用的是 blob 或二进制数组数据,std::vector 可能更合适。

要插入数据,您需要使用绑定参数将向量的内容绑定到 SQL 查询。 也就是说,不要使用 SQLExecDirect要执行包含 SQL 查询的字符串,您应该使用 SQLPrepare 创建语句句柄 - 将二进制值的值与“?”放在一起,然后使用 SQLBindParameter 将占位符绑定到二进制值。 然后使用 SQLExecute 调用该函数。

上面的 SQLBindParameter 文档中有一些代码示例。

If it is really a binary string, and you have actual binary data in it (e.g. embedded nulls) then you need to be careful not to use any of the std::string members that can treat it as a C string (e.g. with a terminating null) as you will obviously end up losing data.

The std::string class is not really appropriate for this purpose - it is written on the assumption that what you are working with is actually a string of characters - since you are using a blob or array of binary data, a std::vector is probably more appropriate.

To insert the data will require you to bind the content of the vector to the SQL query using bind parameters. That is to say, instead of using SQLExecDirect to execute a string containing the SQL query, you should use SQLPrepare to create the statement handle - you leave the spot where the value of the binary value would go with a '?', then use SQLBindParameter to bind the placeholder to the binary value. You then call the function using SQLExecute.

There are some code examples in the SQLBindParameter documentation above.

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