代码片段如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef NO_WIN32
#include <io.h>
#include <windows.h>
#include <conio.h>
#endif /*NO_WIN32*/
#include "infxcli.h"
#define BUFFER_LEN 100
#define ERRMSG_LEN 200
SQLINTEGER checkError (SQLRETURN rc,
SQLSMALLINT handleType,
SQLHANDLE handle,
SQLCHAR* errmsg)
{
SQLRETURN retcode = SQL_SUCCESS;
SQLSMALLINT errNum = 1;
SQLCHAR sqlState[6];
SQLINTEGER nativeError;
SQLCHAR errMsg[ERRMSG_LEN];
SQLSMALLINT textLengthPtr;
if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
{
while (retcode != SQL_NO_DATA)
{
retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
if (retcode == SQL_INVALID_HANDLE)
{
fprintf (stderr, "checkError function was called with an invalid handle!!\n");
return 1;
}
if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
errNum++;
}
fprintf (stderr, "%s\n", errmsg);
return 1;
}
else
return 0;
}
int main (long argc,
char* argv[])
{
SQLCHAR* defDsn = "DRIVER=/home/gbasedbt/install_dir/330_2/lib/cli/iclit09b.so;DLOC=zh_CN.57372;CLOC=zh_CN.57372;DB=test;SRVR=er01;UID=root;PWD=123456";
SQLHDBC hdbc;
SQLHENV henv;
SQLHSTMT droptabstmt;
SQLHSTMT createtabstmt;
SQLHSTMT hInsertStmt;
SQLHSTMT selectStmt;
SQLRETURN rc,in = 0;
//SQLCHAR connStrIn[200];
SQLCHAR connStrOut[200];
SQLSMALLINT connStrOutLen;
SQLCHAR instr[100];
SQLCHAR c1[BUFFER_LEN];
SQLCHAR c2[BUFFER_LEN];
SQLLEN cbc1,cbc2;
SQLCHAR* droptabsql = (SQLCHAR*)"drop table if exists t2";
SQLCHAR* createtabsql =(SQLCHAR*)"create table t2(c1 byte,c2 text)";
SQLCHAR* insertsql = (SQLCHAR*)"insert into t2 values(?,?)";
SQLCHAR* selectsql = (SQLCHAR*)"select c1,c2 from t2";
/* Allocate the Environment handle */
rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
if (rc != SQL_SUCCESS)
{
fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
return (1);
}
/* Set the ODBC version to 3.0 */
rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
return (1);
/* Allocate the connection handle */
rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
return (1);
/* Establish the database connection */
rc = SQLDriverConnect (hdbc, NULL, (char*)defDsn , SQL_NTS, connStrOut, 200, &connStrOutLen, SQL_DRIVER_NOPROMPT);
if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
return (1);
/*
rc = SQLSetConnectAttr(hdbc,SQL_INFX_ATTR_LO_AUTOMATIC,(SQLPOINTER)SQL_TRUE,0);
if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLSetConnectAttr failed\nExiting!!"))
return (1);
*/
/* Allocate the statement handle for the CREATE TABLE statement */
rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &droptabstmt);
if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Create Table Statement Handle Allocation failed\nExiting!!"))
return (1);
/* Allocate the statement handle for the CREATE TABLE statement */
rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &createtabstmt);
if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Create Table Statement Handle Allocation failed\nExiting!!"))
return (1);
/* Allocate the statement handle for the INSERT statement */
rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hInsertStmt);
if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Insert Statement Handle Allocation failed\nExiting!!"))
return (1);
rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &selectStmt);
if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Select Statement Handle Allocation failed\nExiting!!"))
return (1);
fprintf (stdout, "STEP 1 done...connected to database\n");
rc = SQLExecDirect (droptabstmt,(SQLCHAR*)"drop table if exists t2", SQL_NTS);
if (checkError (rc, SQL_HANDLE_STMT, droptabstmt, (SQLCHAR *) "Error in Step 2.1 -- SQLExecDirect failed\n" ))
goto Exit;
rc = SQLExecDirect (createtabstmt, createtabsql, SQL_NTS);
if (checkError (rc, SQL_HANDLE_STMT, createtabstmt, (SQLCHAR *) "Error in Step 2.2 -- SQLExecDirect failed\n" ))
goto Exit;
rc = SQLPrepare(hInsertStmt,(SQLCHAR*)"insert into t2 values(?,?)",SQL_NTS);
if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 2.3 -- SQLExecDirect failed\n" ))
goto Exit;
char * textData = "this";
SQLINTEGER textLen = SQL_NTS;
rc = SQLBindParameter(hInsertStmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY,5, 0,(SQLPOINTER)textData, 0,NULL);
rc = SQLBindParameter(hInsertStmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, 5, 0,(SQLPOINTER)textData, 0, NULL);
rc = SQLExecute(hInsertStmt);
printf("%d\n",rc);
if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 2.4 -- SQLExecDirect failed\n" ))
goto Exit;
rc = SQLExecDirect (selectStmt, selectsql, SQL_NTS);
printf("%d\n",rc);
if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2.5 -- SQLExecDirect failed\n"))
goto Exit;
/* Bind the result set columns */
rc = SQLBindCol (selectStmt, 1, SQL_C_BINARY, c1, BUFFER_LEN, &cbc1);
if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2.6 -- SQLBindCol failed (column 1)\n"))
goto Exit;
rc = SQLBindCol (selectStmt, 2, SQL_C_CHAR, c2, BUFFER_LEN, &cbc2);
if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2.7 -- SQLBindCol failed (column 2)\n"))
goto Exit;
while (1)
{
/* Fetch the data */
rc = SQLFetch (selectStmt);
if (rc == SQL_NO_DATA_FOUND)
break;
else if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2.8 -- SQLFetch failed\n"))
goto Exit;
/* Display the results */
fprintf (stdout, "c1: %s, c2: %s\n", c1,c2);
}
/* Close the result set cursor */
rc = SQLCloseCursor(selectStmt);
if (checkError (rc, SQL_HANDLE_STMT, selectStmt, (SQLCHAR *) "Error in Step 2.9 -- SQLCloseCursor failed\n"))
goto Exit;
Exit:
/* Close all the statement handles */
SQLFreeStmt (droptabstmt, SQL_CLOSE);
SQLFreeStmt (createtabstmt, SQL_CLOSE);
SQLFreeStmt (hInsertStmt, SQL_CLOSE);
SQLFreeStmt (selectStmt, SQL_CLOSE);
/* Free all the statement handles */
SQLFreeHandle (SQL_HANDLE_STMT, droptabstmt);
SQLFreeHandle (SQL_HANDLE_STMT, createtabstmt);
SQLFreeHandle (SQL_HANDLE_STMT, hInsertStmt);
SQLFreeHandle (SQL_HANDLE_STMT, selectStmt);
SQLDisconnect (hdbc);
/* Free the environment handle and the database connection handle */
SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
SQLFreeHandle (SQL_HANDLE_ENV, henv);
fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
in = getchar ();
return (rc);
}