根据官方文档,加入自己的实际操作。 //============================================================================ // Name : TimesTenTest.cpp // Author : chenqiang // Version : // Copyright : // Description : Hello World in C++, Ansi-style //============================================================================ #ifdef WIN32 #include <windows.h> #else #include <sqlunix.h> #endif #include <sql.h> #include <sqlext.h> #include <stdio.h> #include <string.h> #include <stdlib.h> /*返回值判断函数,用于检测具体的出错*/ void CheckReturnCode(SQLRETURN rc, SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt, char* msg, char *filename, int lineno); int main(int argc, char** argv) { /* General return code for the API */ SQLRETURN rc = SQL_SUCCESS; /* 1、Environment handle */ SQLHENV hEnv = SQL_NULL_HENV; /* 2、Connection handle */ SQLHDBC hDbc = SQL_NULL_HDBC; /* 3、Statement handle */ SQLHSTMT hStmt = SQL_NULL_HSTMT; /* Buffer for completed connection string */ SQLCHAR ConnOut[255]; /* number of bytes returned in ConnOut */ SQLSMALLINT connOutLen; /* Connection attributes *TT的一个DSN实例,这里的DSN是我自己的实例 */ SQLCHAR *ConnString = (SQLCHAR *)"DSN=cq_mmdb"; /*a、配置环境句柄*/ rc = SQLAllocEnv(&hEnv); if (rc != SQL_SUCCESS) { fprintf(stderr, "Unable to allocate an " "environment handle/n"); exit(1); } else { printf("SQLAllocEnc success/n"); } /*b、配置数据库连接句柄*/ rc = SQLAllocConnect(hEnv, &hDbc); CheckReturnCode(rc, hEnv, SQL_NULL_HDBC, SQL_NULL_HSTMT, "Unable to allocate a " "connection handle/n", __FILE__, __LINE__); #if 1 /*c、连接数据库(DSN)*/ rc = SQLDriverConnect(hDbc, NULL, ConnString, SQL_NTS, ConnOut, 255, &connOutLen, SQL_DRIVER_NOPROMPT); #else rc = SQLConnect(hDbc, ConnString, SQL_NTS,(SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"", SQL_NTS); printf("/n/nSQLConnect():%d/n",rc); #endif CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Error in connecting to the" " driver/n", __FILE__, __LINE__); /*d、将数据库句柄与SQL声明句柄关联*/ rc = SQLAllocStmt(hDbc, &hStmt); CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to allocate a " "statement handle/n", __FILE__, __LINE__); /* ========================================Your application code here ======================================*/ /*在这里添加我们的实际操作,这里面变量的具体意义,可以参考C_dev.pdf这份文档*/ #define MAXCOLS 64 #define COL_LEN_MAX 256 int i; SQLSMALLINT numCols; SQLLEN pcrow; SQLCHAR colname[32]; SQLSMALLINT colnamelen, coltype, scale, nullable; SQLULEN collen[MAXCOLS]; SQLLEN outlen[MAXCOLS]; SQLCHAR *data[MAXCOLS]; /*Prepare the SELECT statement*/ //SQLCHAR sqlqry[256] = "SELECT * FROM INFO"; //1、准备SQL语句,包括create、select、insert... rc = SQLPrepare(hStmt, (SQLCHAR *)"SELECT * FROM ROOT.TEST", SQL_NTS); CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "SQLPrepare failed", __FILE__, __LINE__); /*Determine number of columns in result rows*/ //2、执行完此条语句的结果集有多少列 rc = SQLNumResultCols(hStmt, &numCols); CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "SQLNumResultCols failed", __FILE__, __LINE__); printf("numCols = %d/n", numCols); /*Describe and bind the columns*/ //3、将每列与具体的变量绑定,这样输出查询的结果集,可以直接用绑定的变量 for(i = 0; i < numCols; i++) { rc = SQLDescribeCol(hStmt, (SQLSMALLINT)(i + 1), colname, (SQLSMALLINT)sizeof(colname), &colnamelen, &coltype, &collen[i], &scale, &nullable); CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "SQLDescribeCol failed", __FILE__, __LINE__); data[i] = (UCHAR *)malloc(collen[i] + 1); memset(data[i], 0, collen[i] + 1); rc = SQLBindCol(hStmt, (SQLSMALLINT)(i + 1), SQL_C_CHAR, data[i], COL_LEN_MAX, &outlen[i]); CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "QLBindCol failed", __FILE__, __LINE__); } printf("/n"); /*Execute the SELECT statement*/ //4、执行SQL语句 rc = SQLExecute(hStmt); /*Fetch the rows*/ //5、获取结果集 if(numCols > 0) { while(SQL_SUCCESS == (rc = SQLFetch(hStmt)) || SQL_SUCCESS_WITH_INFO == rc) { int j; for(j = 0; j < numCols; j++) { printf("%s ", data[j]); } printf("/n"); } //判断遍历结果集的过程是否出现异常 if( SQL_NO_DATA_FOUND != rc) { fprintf(stderr, "Unable to fetch the next row/n"); } } /*=======================================实际操作结束,以下是释放相关环境变量===========================*/ //d、释放Sql声明句柄 if (hStmt != SQL_NULL_HSTMT) { rc = SQLFreeStmt(hStmt, SQL_CLOSE); CheckReturnCode(rc, hEnv, hDbc, hStmt, "Unable to free the " "statement handle/n", __FILE__, __LINE__); } //c、断开数据库 if (hDbc != SQL_NULL_HDBC) { rc = SQLDisconnect(hDbc); CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to close the " "connection/n", __FILE__, __LINE__); b、释放数据库句柄 rc = SQLFreeConnect(hDbc); CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT, "Unable to free the " "connection handle/n", __FILE__, __LINE__); } //a、释放环境句柄 if (hEnv != SQL_NULL_HENV) { rc = SQLFreeEnv(hEnv); CheckReturnCode(rc, hEnv, SQL_NULL_HDBC, SQL_NULL_HSTMT, "Unable to free the " "environment handle/n", __FILE__, __LINE__); } return 1; } void CheckReturnCode(SQLRETURN rc, SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt, char* msg, char *filename, int lineno) { #define MSG_LNG 512 /* SQL state string */ SQLCHAR szSqlState[MSG_LNG]; /* Native error code */ SQLINTEGER pfNativeError; /* Error msg text buffer pointer */ SQLCHAR szErrorMsg[MSG_LNG]; /* Error msg text Available bytes */ SQLSMALLINT pcbErrorMsg; SQLRETURN ret = SQL_SUCCESS; if (rc != SQL_SUCCESS && rc != SQL_NO_DATA_FOUND) { 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: break; } /* switch */ } /* while */ } }