我有一个 MySQL 数据库,其中的表包含 BINARY(16)
列。
当我从我的 C++ 应用程序通过 ODBC 写入二进制数据时,数据被视为以 Null 终止的字符串,即它在数据中的第一个 Null 字节处被截断。
在我的 C++ 应用程序中,我使用 SQLBindParameter
绑定(bind)一个 unsigned char [16]
数组,如下所示:
SQLBindParameter(statementHandler, paramCount, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_BINARY, MAX_NOTIFICATION_OUT_IP_BIN_LEN, 0, _notificationOutDstIP, MAX_NOTIFICATION_OUT_IP_BIN_LEN, 0);
MAX_NOTIFICATION_OUT_IP_BIN_LEN
为 16。
即使二进制数据包含 Null 值,我如何确保将 16 个字节写入数据库?
最佳答案
来自MSDN documentation for SQLBindParameter
:
If
StrLen_or_IndPtr
is a null pointer, the driver assumes that all input parameter values are non-NULL
and that character and binary data is null-terminated.
相反,将该参数设置为指向数据长度的指针:
SQLBindParameter(statementHandler,
paramCount,
SQL_PARAM_INPUT,
SQL_C_BINARY, SQL_BINARY,
MAX_NOTIFICATION_OUT_IP_BIN_LEN,
0,
_notificationOutDstIP,
MAX_NOTIFICATION_OUT_IP_BIN_LEN,
&MAX_NOTIFICATION_OUT_IP_BIN_LEN
);
关于c++ - 通过 ODBC 写入 MySQL 表的二进制数据被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10548076/