ODBC SQLExecute SQLPrepare

#include <sqlunix.h>
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>

void CheckReturnCode(SQLRETURN rc, SQLHENV henv,SQLHDBC hdbc, SQLHSTMT hstmt,char* msg, char *filename, int lineno);

void main( void )
{
     SQLRETURN rc = SQL_SUCCESS; /* General return code for the API */
     SQLHENV hEnv = SQL_NULL_HENV; /* Environment handle */
     SQLHDBC hDbc = SQL_NULL_HDBC; /* Connection handle */
     SQLHSTMT hStmt = SQL_NULL_HSTMT; /* Statement handle */

     //SQLCHAR *szConnString = (SQLCHAR *)"DSN=TT41Data";
     SQLCHAR *szConnString = (SQLCHAR *)"dsn=ttdatasvr;tcp_port=14502;uid=spa;pwd=a$h$e$01";
     SQLCHAR szConnOut[255];    /* Buffer for completed connection string */
     SQLSMALLINT cbConnOut;     /* number of bytes returned in szConnOut */

     // allocate environment
     rc = SQLAllocEnv(&hEnv);
     if (rc != SQL_SUCCESS) {
          fprintf(stderr,"Unable to allocate an environment handle/n");
          exit(1);
     }

     // allocate connection
     rc = SQLAllocConnect(hEnv, &hDbc);
     CheckReturnCode(rc, hEnv, SQL_NULL_HDBC, SQL_NULL_HSTMT, "Unable allocate connection handle/n", __FILE__, __LINE__);

     rc = SQLDriverConnect(hDbc, /
                           NULL, /
                           (SQLCHAR *)szConnString,/
                           SQL_NTS, /
                           szConnOut, /
                           255, /
                           &cbConnOut, /
                           SQL_DRIVER_NOPROMPT);
     CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Error in connecting to the driver/n",__FILE__, __LINE__);

        // allocate statement
     rc = SQLAllocStmt(hDbc, &hStmt);
     CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to allocate a statement handle/n", __FILE__,__LINE__);

     /* Your application code here */

     //SQLINTEGER v3;
     SQLCHAR    v1[256];
     SQLLEN     v1size;

     SQLCHAR    v2[256];
     SQLLEN     v2size;

     SQLCHAR    v3[256];
     SQLLEN     v3size;

     SQLLEN numOfRows;

     //int whereValue=1;
     SQLINTEGER whereValue=1;

     SQLCHAR sqlString[] = "select v1,v2 from tab where v3=?";
     rc = SQLPrepare( hStmt, (SQLCHAR*)sqlString, SQL_NTS );
     CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to SQLPrepare./n", __FILE__, __LINE__);

        // bind parameter
     memset((void *)v3, 0, (255) );
     strcpy((char *)v3,"123");
     rc = SQLBindParameter(     
          hStmt,                 
          1,                    /* ParameterNumber, start 1     */
          SQL_PARAM_INPUT,      /* InputOutputType              */
          SQL_C_CHAR,           /* ValueType: C data type of the parameter              */
          SQL_CHAR,             /* ParameterType: SQL data type of the parameter        */
          sizeof(v3),           /* ColumnSize: Precision of the corresponding parameter marker */
          0,                    /* DecimalDigits:scale of the corresponding parameter */
          (v3),                 /* ParameterValuePtr            */
          sizeof(v3),           /* BufferLength, valid for char */
          &v3size);             /* StrLen_or_IndPtr             */
     CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to SQLBindParameter 1/n", __FILE__, __LINE__);


        // bind column
     rc= SQLBindCol(hStmt,(SQLSMALLINT)1, SQL_C_CHAR, (v1), sizeof(v1), &v1size);
     CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to free the statement handle/n", __FILE__, __LINE__);

     rc= SQLBindCol(hStmt,(SQLSMALLINT)2, SQL_C_CHAR, (v2), sizeof(v2), &v2size);
     CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to free the statement handle/n", __FILE__, __LINE__);

        // Execute
     rc = SQLExecute(hStmt);                                                                   
     CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to SQLExecute/n", __FILE__, __LINE__);
                                                                                          
        // Fetch
     rc = SQLFetch(hStmt);
     CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to SQLFetch./n", __FILE__, __LINE__);                                                                                       
   
     rc = SQLFreeStmt(hStmt,SQL_CLOSE);
     CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to execute update SQL sentence./n", __FILE__, __LINE__);                                                                                       

     /*******************************************************************************/
     if (hStmt != SQL_NULL_HSTMT) {
     rc = SQLFreeStmt(hStmt, SQL_DROP);
     CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to free the statement handle/n", __FILE__, __LINE__);}

     if (hDbc != SQL_NULL_HDBC) {
          rc = SQLDisconnect(hDbc);
          CheckReturnCode(rc, hEnv, hDbc,SQL_NULL_HSTMT,"Unable to close the connection/n",__FILE__, __LINE__);
          rc = SQLFreeConnect(hDbc);
          CheckReturnCode(rc, hEnv, hDbc,SQL_NULL_HSTMT,"Unable to free the connection handle/n",__FILE__, __LINE__);
     }

     if (hEnv != SQL_NULL_HENV) {
          rc = SQLFreeEnv(hEnv);
          CheckReturnCode(rc, hEnv, SQL_NULL_HDBC,SQL_NULL_HSTMT,"Unable  free environment handle/n",__FILE__, __LINE__);
     }

}
void CheckReturnCode(SQLRETURN rc, SQLHENV henv,SQLHDBC hdbc, SQLHSTMT hstmt,char* msg, char *filename,int lineno)
{
     #define MSG_LNG 512
     SQLCHAR szSqlState[MSG_LNG]; /* SQL state string */
     SQLINTEGER pfNativeError; /* Native error code */
     SQLCHAR szErrorMsg[MSG_LNG]; /* Error msg text buffer pointer */
     SQLSMALLINT pcbErrorMsg; /* Error msg text Available bytes */
     SQLRETURN ret = SQL_SUCCESS;
     if (rc != SQL_SUCCESS && rc != SQL_NO_DATA_FOUND && rc != SQL_SUCCESS_WITH_INFO ) {
          if (rc != SQL_SUCCESS_WITH_INFO) { /* It's not just a warning */
               fprintf(stderr, "*** ERROR in %s, line %d: %s/n",filename, lineno, msg);
          }
          /*
          * Now see why the error/warning occurred
          */
          while (ret == SQL_SUCCESS ||ret == SQL_SUCCESS_WITH_INFO) {
               ret = SQLError(henv, hdbc, hstmt,szSqlState, &pfNativeError,szErrorMsg, MSG_LNG,&pcbErrorMsg);
               switch (ret) {
               case SQL_SUCCESS:
                    fprintf(stderr, "*** %s/n*** ODBC Error/Warning = %s, TimesTen Error/Warning = %d/n",szErrorMsg, szSqlState,pfNativeError);
                    break;
               case SQL_SUCCESS_WITH_INFO:
                    fprintf(stderr, "*** Call to SQLError failed with return code of SQL_SUCCESS_WITH_INFO./n *** Need to increase size of message buffer./n");
                    break;
               case SQL_INVALID_HANDLE:
                    fprintf(stderr, "*** Call to SQLError failed with return code of SQL_INVALID_HANDLE./n");
                    break;
               case SQL_ERROR:
                    fprintf(stderr, "*** Call to SQLError failed with return code of SQL_ERROR./n");
                    break;
               case SQL_NO_DATA_FOUND:
                printf("SQL_NO_DATA_FOUND/n");
                    break;
               default:
                    fprintf(stderr, "*** Call to SQLError failed with return code of UNKNOW./n");
                    break;
               } /* switch */
          } /* while */
          exit(1);
     }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值