我正在尝试执行查询“SELECT 1 from dual”。 但是在一个SQLFetch没有出现在ColumnData中之后,就会分配内存并且函数返回成功。如果查询返回varchar类型,则效果很好。刚刚开始使用ODBC,我真的很困惑因为......
以下代码
{
bool isDone = true;
Connect();
if(isConnected)
{
m_retcode = SQLExecDirect(m_hStmt, _queryText, SQL_NTS);
if (m_retcode == SQL_SUCCESS || m_retcode == SQL_SUCCESS_WITH_INFO)
{
m_retcode = SQLNumResultCols(m_hStmt, &numCols);
if (m_retcode == SQL_SUCCESS || m_retcode == SQL_SUCCESS_WITH_INFO)
{
for (i=0;i<numCols;i++)
{
ColumnName[i] = new SQLWCHAR[MAX_COL_NAME_LEN];
m_retcode = SQLDescribeCol(m_hStmt, i+1, ColumnName[i], MAX_COL_NAME_LEN, &ColumnNameLen[i], &ColumnDataType[i], &ColumnDataSize[i], &ColumnDataDigits[i], &ColumnDataNullable[i]);
if (m_retcode < 0 || m_retcode > 1)
{
ShowError(DESCRIBECOL);
}
printf("Column Name : %S\n SQL Data Type : %i\n Data Size : %i\n\n", ColumnName[i], (int)ColumnDataType[i], (int)ColumnDataSize[i]);
ColumnData[i] = new SQLCHAR[ColumnDataSize[i]+2];
switch(ColumnDataType[i])
{
case SQL_VARCHAR:
ColumnDataType[i]=SQL_C_CHAR;
m_retcode = SQLBindCol(m_hStmt, i+1, ColumnDataType[i], ColumnData[i], ColumnDataSize[i]*2, &ColumnDataLen[i]);
if (m_retcode < 0 || m_retcode > 1)
{
ShowError(DESCRIBECOL);
}
break;
case SQL_DECIMAL:
ColumnDataType[i]=SQL_C_CHAR;
m_retcode = SQLBindCol(m_hStmt, i+1, ColumnDataType[i], ColumnData[i], sizeof(double)*sizeof(double), &ColumnDataLen[i]);
if (m_retcode < 0 || m_retcode > 1)
{
ShowError(DESCRIBECOL);
}
break;
case SQL_FLOAT:
ColumnDataType[i]=SQL_C_DOUBLE;
m_retcode = SQLBindCol(m_hStmt, i+1, ColumnDataType[i], ColumnData[i], ColumnDataSize[i], &ColumnDataLen[i]);
if (m_retcode < 0 || m_retcode > 1)
{
ShowError(DESCRIBECOL);
}
break;
}
}
printf ("Data Records\n------------\n");
for (i=0; ; i++)
{
m_retcode = SQLFetch(m_hStmt);
if (m_retcode == SQL_NO_DATA)
{
break;
}
printf ("\nRecord %i \n", i+1);
for (j=0; j<numCols; j++)
{
printf("Column %S : ", ColumnName[j]);
if (ColumnDataType[j]==SQL_C_CHAR || ColumnDataType[j]==SQL_C_DOUBLE)
{
SQLGetData(m_hStmt, j, ColumnDataType[j], ColumnData[j], MAX_LEN, 0);
printf("%s\n", ColumnData[j]);
}
}
}
}
else
{
ShowError(NUMRESULTCOLS);
isDone = false;
}
}
else
{
ShowError(EXECDIRECT);
isDone = false;
}
}
else
{
printf("FAILED TO CONNECT");
}
Disconnect();
return isDone;
}
答案 0 :(得分:0)
我没有详细查看你的代码,但我可以看到一些未声明的变量。来自dual的select 1可能会返回一个整数的列类型,而你的switch语句不会覆盖它。我建议你在你的交换机上添加一个默认值来捕获你没有涵盖的列类型(无论如何这都是很好的编程习惯)。