使用 ODBC 将二进制 std::string 插入 BLOB 列
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它确实是一个二进制字符串,并且其中有实际的二进制数据(例如嵌入的空值),那么您需要小心不要使用任何可以将其视为 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, astd::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.