這裏所說的CLI指的是DB2 Call Level Interfacesql
代碼:數據庫
#include <stdio.h> #include <string.h> #include <sqlcli1.h> int main() { // Declare The Local Memory Variables SQLHANDLE EnvHandle = 0; SQLHANDLE ConHandle = 0; SQLHANDLE StmtHandle = 0; SQLRETURN RetCode = SQL_SUCCESS; SQLCHAR SQLStmt[255]; SQLCHAR JobType[10]; SQLCHAR EmpNo[10]; SQLCHAR LastName[25]; /*-----------------------------------------------------*/ /* INITIALIZATION */ /*-----------------------------------------------------*/ // Allocate An Environment Handle SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &EnvHandle); // Set The ODBC 應用程序 Version To 3.x if (EnvHandle != 0) SQLSetEnvAttr(EnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER); // Allocate A Connection Handle if (EnvHandle != 0) SQLAllocHandle(SQL_HANDLE_DBC, EnvHandle, &ConHandle); // Connect To The Appropriate Data Source if (ConHandle != 0) RetCode = SQLConnect(ConHandle, (SQLCHAR *) "SAMPLE", /*db2樣例數據庫,db2sampl程序可建立它*/ SQL_NTS, (SQLCHAR *) "db2inst1", /*你的用戶名*/ SQL_NTS, (SQLCHAR *) "yourpasswd", /*你的密碼*/ SQL_NTS); /*-----------------------------------------------------*/ /* TRANSACTION PROCESSING */ /*-----------------------------------------------------*/ // Allocate An SQL Statement Handle if (ConHandle != 0 && RetCode == SQL_SUCCESS) SQLAllocHandle(SQL_HANDLE_STMT, ConHandle, &StmtHandle); // Define A SELECT SQL Statement That Uses A Parameter // Marker strcpy((char *) SQLStmt, "SELECT empno, lastname FROM "); strcat((char *) SQLStmt, "employee WHERE job = ?"); // Prepare The SQL Statement RetCode = SQLPrepare(StmtHandle, SQLStmt, SQL_NTS); // Bind The Parameter Marker Used In The SQL Statement To // An Application Variable RetCode = SQLBindParameter(StmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(JobType), 0, JobType, sizeof(JobType), NULL); // Populate The "Bound" Application Variable strcpy((char *) JobType, "DESIGNER"); // Execute The SQL Statement RetCode = SQLExecute(StmtHandle); // If The SQL Statement Executed Successfully, Retrieve // The Results if (RetCode == SQL_SUCCESS) { // Bind The Columns In The Result Data Set Returned // To Application Variables SQLBindCol(StmtHandle, 1, SQL_C_CHAR, (SQLPOINTER) EmpNo, sizeof(EmpNo), NULL); SQLBindCol(StmtHandle, 2, SQL_C_CHAR, (SQLPOINTER) LastName, sizeof(LastName), NULL); // While There Are Records In The Result Data Set // Produced, Retrieve And Display Them while (RetCode != SQL_NO_DATA) { RetCode = SQLFetch(StmtHandle); if (RetCode != SQL_NO_DATA) printf("%-8s %sn", EmpNo, LastName); } } // Commit The Transaction RetCode = SQLEndTran(SQL_HANDLE_DBC, ConHandle, SQL_COMMIT); // Free The SQL Statement Handle if (StmtHandle != 0) SQLFreeHandle(SQL_HANDLE_STMT, StmtHandle); /*-----------------------------------------------------*/ /* TERMINATION */ /*-----------------------------------------------------*/ // Terminate The Data Source Connection if (ConHandle != 0) RetCode = SQLDisconnect(ConHandle); // Free The Connection Handle if (ConHandle != 0) SQLFreeHandle(SQL_HANDLE_DBC, ConHandle); // Free The Environment Handle if (EnvHandle != 0) SQLFreeHandle(SQL_HANDLE_ENV, EnvHandle); // Return Control To The Operating System return(0); }
編譯運行:code
$ gcc -o db2test db2test.c -I/home/db2inst1/sqllib/include -L/home/db2inst1/sqllib/lib -ldb2 $ ./db2test 000150 ADAMSONn000160 PIANKAn000170 YOSHIMURAn000180 SCOUTTENn000190 WALKERn000200 BROWNn000210 JONESn000220 LUTZn200170 YAMAMOTOn200220 JOHNn