C++使用CLI连接DB2

 
<!--推荐博文-->
 

 

DB2调用层接口(DB2 Call Level Interface简称CLI)是DB2数据库访问的基础接口。

下面例子依次执行了
  • 连接DB2数据库
  • 创建操作句柄
  • 查询操作
  • 新增操作
  • 断开连接
  • 释放资源

示例代码:
cliTest.cpp

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sqlcli.h>
  4. #include <sqlcli1.h>
  5. #include <sqlenv.h>
  6. #include <sqlda.h>
  7. #include <sqlca.h>
  8. #include <string.h>
  9. using namespace std;
  10. struct result
  11. {
  12. char ename[50];
  13. char cname[100];
  14. int ename_len;
  15. int cname_len;
  16. result()
  17. {
  18. memset(ename, '\0', sizeof(ename));
  19. memset(cname, '\0', sizeof(cname));
  20. }
  21. };
  22. int main()
  23. {
  24. SQLRETURN cliRC = SQL_SUCCESS;
  25. SQLHANDLE henv = SQL_NULL_HENV;
  26. SQLHANDLE hdbc = SQL_NULL_HDBC;
  27. SQLHANDLE hstmt = SQL_NULL_HSTMT;
  28. //获取环境句柄
  29. cliRC = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  30. //获取连接句柄
  31. cliRC = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
  32. cliRC = SQLSetConnectAttr(hdbc,
  33. SQL_ATTR_AUTOCOMMIT,
  34. (SQLPOINTER)SQL_AUTOCOMMIT_OFF,
  35. SQL_IS_INTEGER);
  36. cliRC = SQLConnect(hdbc,
  37. (SQLCHAR *)"mydb",
  38. SQL_NTS,
  39. (SQLCHAR *)"user",
  40. SQL_NTS,
  41. (SQLCHAR *)"passwd",
  42. SQL_NTS);
  43. if (cliRC != SQL_SUCCESS)
  44. {
  45. cout<<"connect fail"<<endl;
  46. }
  47. //设置连接
  48. cliRC = SQLSetConnection(hdbc);
  49. //获取操作句柄
  50. SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
  51. cliRC = SQLSetStmtAttr(hstmt,
  52. SQL_ATTR_DEFERRED_PREPARE,
  53. (SQLPOINTER)SQL_DEFERRED_PREPARE_ON,
  54. SQL_IS_INTEGER);
  55. cliRC = SQLSetStmtAttr(hstmt, SQL_ATTR_BLOCK_FOR_NROWS, (SQLPOINTER)100, SQL_IS_INTEGER);
  56. if (cliRC == SQL_SUCCESS)
  57. {
  58. cout<<"setStmtAttr"<<endl;
  59. }
  60. //查询语句
  61. const char * strSQL = "select col1, col2 from table1 ";
  62. cliRC = SQLExecDirect(hstmt, (SQLCHAR*)strSQL, SQL_NTS);
  63. if (cliRC == SQL_SUCCESS)
  64. {
  65. cout<<"SQL execute successfully"<<endl;
  66. }
  67. SQLSMALLINT ulColumns = 0;
  68. //获取列数
  69. cliRC = SQLNumResultCols(hstmt, &ulColumns);
  70. if (cliRC == SQL_SUCCESS)
  71. {
  72. cout<<"SQLNumResultCols num="<< ulColumns<<endl;
  73. }
  74. // SQLCHAR szName[50] = "";
  75. // SQLSMALLINT cbNameLen = 0;
  76. // SQLSMALLINT iType = 0;
  77. // SQLUINTEGER ulColumnSize = 0;
  78. // SQLSMALLINT iScale = 0;
  79. // cliRC = SQLDescribeCol(hstmt, (SQLSMALLINT)(0 + 1), szName, 32, &cbNameLen, &iType, &ulColumnSize, &iScale, NULL);
  80. // if (cliRC == SQL_SUCCESS)
  81. // {
  82. // printf("SQLDescribeCol ulColumnSize=[%d]\n", ulColumnSize);
  83. // }
  84. //获取查询结果
  85. SQLPOINTER rgbValue;
  86. SQLINTEGER aaa = 0;
  87. SQLINTEGER *pcbValue = &aaa;
  88. char temp[100] = "";
  89. rgbValue = temp;
  90. struct result rst;
  91. cliRC = SQLBindCol(hstmt, 1, SQL_C_CHAR, rst.ename, sizeof(rst.ename), &rst.ename_len);
  92. cliRC = SQLBindCol(hstmt, 2, SQL_C_CHAR, rst.cname, sizeof(rst.cname), &rst.cname_len);
  93. while(SQLFetch(hstmt) != SQL_NO_DATA_FOUND)
  94. {
  95. cout<<"ename="<<rst.ename<<", cname="<< rst.cname<<endl;
  96. }
  97. //************************************INSERT 操作 ***************************************
  98. const char *insert = " insert into table1 (col1, col2) values('testcli', 'cli')";
  99. cliRC = SQLExecDirect(hstmt, (SQLCHAR*)insert, SQL_NTS);
  100. if (cliRC != SQL_SUCCESS)
  101. {
  102. printf("exec fail \n");
  103. }
  104. //成功操作行数
  105. SQLLEN ulRowEffected = 0;
  106. cliRC = SQLRowCount(hstmt, &ulRowEffected);
  107. cout<<"ulRowEffected = "<<ulRowEffected<<endl;
  108. //结束会话
  109. cliRC = SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  110. //释放资源
  111. cliRC = SQLFreeStmt(hstmt, SQL_UNBIND);
  112. cliRC = SQLFreeStmt(hstmt, SQL_CLOSE);
  113. return 0;
  114. }

源自:http://blog.chinaunix.net/uid-7454512-id-3269938.html

你可能感兴趣的:(C++)