更新 2
我打开了跟踪并运行了我的示例查询。这是痕迹。我确实看到了声明Strlen Or Ind = 0x7fff9c84ee88 -> 255
。indicator
变量定义为Is SQLLEN indicator;
this not initialized proper?
[ODBC][22407][1379343424.503572][__handles.c][450]
Exit:[SQL_SUCCESS]
Environment = 0x14f8160
[ODBC][22407][1379343424.503627][SQLSetEnvAttr.c][182]
Entry:
Environment = 0x14f8160
Attribute = SQL_ATTR_ODBC_VERSION
Value = 0x3
StrLen = 0
[ODBC][22407][1379343424.503654][SQLSetEnvAttr.c][349]
Exit:[SQL_SUCCESS]
[ODBC][22407][1379343424.503678][SQLAllocHandle.c][364]
Entry:
Handle Type = 2
Input Handle = 0x14f8160
[ODBC][22407][1379343424.503707][SQLAllocHandle.c][482]
Exit:[SQL_SUCCESS]
Output Handle = 0x14f8a90
[ODBC][22407][1379343424.503745][SQLDriverConnect.c][688]
Entry:
Connection = 0x14f8a90
Window Hdl = (nil)
Str In = [DSN=MyDB;UID=MyUID;][length = 15 (SQL_NTS)]
Str Out = 0x7fff9c84cc80
Str Out Max = 2048
Str Out Ptr = (nil)
Completion = 1
UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'
[ODBC][22407][1379343424.523244][SQLDriverConnect.c][1497]
Exit:[SQL_SUCCESS]
Connection Out [[DSN=MyDB;UID=MyUID;][length = 15 (SQL_NTS)]]
[ODBC][22407][1379343424.523297][SQLAllocHandle.c][529]
Entry:
Handle Type = 3
Input Handle = 0x14f8a90
[ODBC][22407][1379343424.523343][SQLAllocHandle.c][1064]
Exit:[SQL_SUCCESS]
Output Handle = 0x1526b40
[ODBC][22407][1379343424.523377][SQLExecDirect.c][236]
Entry:
Statement = 0x1526b40
SQL = [SELECT '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890...][length = 309 (SQL_NTS)]
[ODBC][22407][1379343424.523948][SQLExecDirect.c][499]
Exit:[SQL_SUCCESS]
[ODBC][22407][1379343424.523982][SQLNumResultCols.c][152]
Entry:
Statement = 0x1526b40
Column Count = 0x7fff9c84eeae
[ODBC][22407][1379343424.524005][SQLNumResultCols.c][244]
Exit:[SQL_SUCCESS]
Count = 0x7fff9c84eeae -> 1
[ODBC][22407][1379343424.524030][SQLFetch.c][158]
Entry:
Statement = 0x1526b40
[ODBC][22407][1379343424.524056][SQLFetch.c][340]
Exit:[SQL_SUCCESS]
[ODBC][22407][1379343424.524084][SQLGetData.c][233]
Entry:
Statement = 0x1526b40
Column Number = 1
Target Type = 1 SQL_CHAR
Buffer Length = 5000
Target Value = 0x7fff9c84da90
StrLen Or Ind = 0x7fff9c84ee88
[ODBC][22407][1379343424.524115][SQLGetData.c][497]
Exit:[SQL_SUCCESS]
Buffer = [12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678]
Strlen Or Ind = 0x7fff9c84ee88 -> 255
[ODBC][22407][1379343424.524142][SQLColAttribute.c][286]
Entry:
Statement = 0x1526b40
Column Number = 1
Field Identifier = SQL_DESC_NAME
Character Attr = 0x7fff9c84ee20
Buffer Length = 100
String Length = 0x7fff9c84ee86
Numeric Attribute = (nil)
[ODBC][22407][1379343424.524167][SQLColAttribute.c][657]
Exit:[SQL_SUCCESS]
[ODBC][22407][1379343424.524229][SQLFetch.c][158]
Entry:
Statement = 0x1526b40
[ODBC][22407][1379343424.524257][SQLFetch.c][340]
Exit:[SQL_NO_DATA]
[ODBC][22407][1379343424.524321][SQLDisconnect.c][204]
Entry:
Connection = 0x14f8a90
[ODBC][22407][1379343424.524375][SQLDisconnect.c][341]
Exit:[SQL_SUCCESS]
[ODBC][22407][1379343424.524415][SQLFreeHandle.c][279]
Entry:
Handle Type = 2
Input Handle = 0x14f8a90
[ODBC][22407][1379343424.524438][SQLFreeHandle.c][330]
Exit:[SQL_SUCCESS]
[ODBC][22407][1379343424.524463][SQLFreeHandle.c][212]
Entry:
Handle Type = 1
Input Handle = 0x14f8160
更新 我进一步调查了我的 C++ 程序。我现在看到查询结果在调用中被截断
data_ret = SQLGetData(stmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator);
buf
长度足够且data_ret
为零(成功)。我是在滥用SQLGetData
,还是有一些我不知道的行为?
我见过其他人有类似的问题,但我还没有完全解决它。如果我查询一个长字符串,它会被截断为 255 个字符。我正在使用 unixODBC 和 FreeTDS 从 Linux 机器上的 bash 脚本查询 SQL Server DB。
我通过放置来设置驱动程序
[FreeTDS]
Description = v0.91 with protocol v7.2
Driver = /usr/lib64/libtdsodbc.so.0
在模板中并运行
odbcinst -i -d -f tds.driver.template
然后我把
[MyDB]
Driver = FreeTDS
Description = Database Description
Trace = No
Server = <serverIP>
Port = 1433
Database = <myDB>
UID = <myUID>
在模板中运行
odbcinst -i -s -f tds.datasource.template
我试过这个答案,但我一定做错了什么。任何建议表示赞赏。