Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I can't get an ODBC call to work in Visual C++ 1

Status
Not open for further replies.

MDIGX

Programmer
Feb 18, 2004
7
US
What could I be doing wrong? I pulled this code from a kind soul who posted here last week. The switch case at the end tells me that I'm hitting SQL_ERROR when I debug. But I can not find the reason. When the code runs, it just continues on silently as if there was no problem.

The sql runs just fine in access. Any ideas?

strSQL = "INSERT INTO usage_log ( dtTimestamp, txtAction, txtURL, txtVersion, txtNTID ) SELECT #2/12/2004 2:06:10 PM#, ""ReportViewing"", ""Spending by Account - All Suppliers"", ""1"", ""RiesbeckRS""";

HENV hEnv;
HDBC hDbc;
RETCODE rc;
int iOut = 0;
char strOut[256];
char* strDSN = "driver={Microsoft Access Driver (*.mdb)};dbq=[c:\\work\\ActuateLog\\usagelog.mdb];";

rc = SQLAllocEnv(&hEnv);
rc = SQLAllocConnect(hEnv, &hDbc);

rc = SQLDriverConnect(hDbc, NULL, (unsigned char *)strDSN, SQL_NTS, (unsigned char *)strOut, 255, (SQLSMALLINT*)iOut, SQL_DRIVER_NOPROMPT);
{
int test = false;
int iVal;
int ret1;
// int ret2;
char chVal[128];
HSTMT hStmt;
rc = SQLAllocStmt(hDbc,&hStmt);
rc = SQLPrepare(hStmt,(unsigned char*)strSQL, SQL_NTS); //1
rc = SQLBindCol(hStmt, 1, SQL_C_ULONG, &iVal, 4, (SQLINTEGER*)&ret1);
rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chVal, 128, (SQLINTEGER*)&ret1);
rc = SQLExecute (hStmt); // 2
//rc = SQLExecDirect(hStmt, (unsigned char*)strSQL, SQL_NTS);

switch (rc)
{
case SQL_SUCCESS:
printf("SQL_SUCCESS");
break;
case SQL_SUCCESS_WITH_INFO:
printf("SQL_SUCCESS_WITH_INFO");
break;
case SQL_NEED_DATA:
printf("SQL_NEED_DATA");
break;
case SQL_STILL_EXECUTING:
printf("SQL_STILL_EXECUTING");
break;
case SQL_ERROR:
printf("SQL_ERROR"); break;
case SQL_NO_DATA:
printf("SQL_NO_DATA");
break;
case SQL_INVALID_HANDLE:
printf("SQL_INVALID_HANDLE");
break;
}

rc = SQLFreeStmt(hStmt, SQL_DROP);
}
rc = SQLDisconnect(hDbc);
rc = SQLFreeEnv(hEnv);
 
use function SQLError to get more details on error

Ion Filipski
1c.bmp
 
What would be an appropriate call to SQLError?



(I am not a good C++ programmer at all)
 
I was able to get SQLError to work. Here's my error:

"[Microsoft][ODBC Driver Manager]Function sequence error"

Does anyone have any ideas?
 
Nevermind... I got it working. The double quotes in the SQL were messing things up... But I learned a valuable lesson in SQLError!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top