这是场景:
CREATE TABLE amaaa
(
laaaid integer NOT NULL DEFAULT 0,
blobdata bytea
)
INSERT INTO amaaa(laaaid)VALUES (1);
SQLRETURN connect()
{
SQLRETURN ret; /* ODBC API return status */
/* Allocate an environment handle */
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &myestenv);
/* We want ODBC 3 support */
if (!ret) ret = SQLSetEnvAttr(myestenv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
/* Allocate a connection handle */
if (!ret) ret = SQLAllocHandle(SQL_HANDLE_DBC, myestenv, &dbc);
SQLSetConnectOption(dbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
const char* connectStr = "ByteaAsLongVarBinary=1;LFConversion=0;DSN=AMPG961;UID=postgres;PWD={postgres}";
if (!ret) ret = SQLDriverConnect(dbc, NULL, (SQLCHAR*)connectStr, strlen(connectStr), NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
/* Allocate a statement handle */
if (!ret) ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
if (ret) getErr(ret);
return ret;
}
void disconnect()
{
/* Disconnect and free all the handles */
SQLRETURN ret = SQLFreeHandle(SQL_HANDLE_STMT, stmt);
ret = SQLDisconnect(dbc);
ret = SQLFreeHandle(SQL_HANDLE_DBC, dbc);
ret = SQLFreeHandle(SQL_HANDLE_ENV, myestenv);
}
const USHORT LEN = 3;
SQLRETURN update()
{
SQLRETURN ret = connect();
if (ret) return ret;
const unsigned char vLob[LEN] = "v=";
long lBlobLen = LEN - 1;
int lMainRecordId = 1;
SQLLEN cbValue = lBlobLen;
ret = SQLPrepare(stmt, (SQLCHAR*)"UPDATE \"amaaa\" set blobdata = ? where laaaid = ?;", SQL_NTS);
if (!ret) ret = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT,
SQL_BINARY, SQL_LONGVARBINARY, 0,
0, (void*)vLob, lBlobLen, &cbValue);
SQLLEN cbLen = 4;
ret = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT,
SQL_C_LONG, SQL_INTEGER, 0,
0, (void*)&lMainRecordId, sizeof(long), &cbLen);
if (!ret) ret = SQLExecute(stmt);
if ((ret != SQL_SUCCESS) && (ret != SQL_NEED_DATA) && (ret != SQL_SUCCESS_WITH_INFO)) {
printf("SQLExecDirect Failed\n\n");
getErr(ret);
disconnect();
}
return ret;
}
当我从数据库中查询 blobdata 列时,该值始终为 'v\075',实际上我输入的值是 'v=';但同样的代码在windows平台上没有问题。有什么想法吗?
提前致谢。