【YashanDB知识库】YashanDB-OCI-快速上手

诗林  论坛元老 | 2024-9-27 18:29:11 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1341|帖子 1341|积分 4023

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本文内容来自YashanDB官网,详细内容请见https://www.yashandb.com/newsinfo/7488285.html?templateId=1718516
配景

OCI 是Oracle调用接口(Oracle Call Interface 简称OCI) 提供了一组对Oracle数据库进行存取的接口子例程(函数),通过在第三方设计语言(如C语言)中进行调用可达到存取ORACLE数据库的目标。
崖山数据库基于Oracle的OCI接口,开发了强兼容的接口,应用可以做比较少的改动,平滑地迁移到崖山数据库。
主要优点
1、Oracle数据库服务器特性中的即时可用性
2、企业级的性能和可伸缩性
3、矫健的安全模子
4、在所有运行Oracle的平台上的可移植性。
本文将介绍cmake 方式编译崖山数据库,帮助读者快速上手崖山数据库的OCI接口,并在使用OCI解决实际工作遇到的问题。
环境准备

1、配置yashandb的C驱动 和 OCI 文件
2、准备Oracle的OCI软件包,需要下载两个:
其一是instantclient-basic-linux.x64-21.14.0.0.0dbru.zip,
其二是instantclient-sdk-linux.x64-21.14.0.0.0dbru.zip
3、呆板提前安装GDB和CMAKE工具
怎样编译yashandb的OCI代码?

接下来,以官网OCI示例代码为例,快速上手编译Yashandb的OCI代码
准备编译前命令
编辑 CMakeList.txt,它是CMAKE工程编译的声明命令文件
点击检察代码
  1. [yashan103@localhost oci_dir]$ cat CMakeLists.txt
  2. cmake_minimum_required(VERSION 2.8.12)
  3. # CMAKE的工程名
  4. project(YAS_OCI_TEST)
  5. set(CMAKE_CXX_STANDARD 11)
  6. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  7. set(BUILD_USE_64BITS on)
  8. set(CMAKE_CONFIGURATION_TYPES "Debug" CACHE STRING "" FORCE)
  9. set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "" FORCE)
  10. #设置OCI接口的源代码路径
  11. SET(LIBHELLO_SRC
  12. ./bb.c
  13. )
  14. #查找库文件
  15. find_library(YAS_OCI_LIBRARY NAMES yas_oci PATHS /home/yashan103/oci_lib/yashandb-oci-23.2.1.100-4-gecc0e02-linux-x86_64)
  16. #确认库文件是否找到
  17. if(NOT YAS_OCI_LIBRARY)
  18.    message(FATAL_ERROR "Could not find libyas_oci.so")
  19. endif()
  20. # 添加OCI.h的头文件
  21. include_directories(/home/yashan103/instantclient_21_12/sdk/include)
  22. ##执行编译操作
  23. add_executable(YAS_OCI_TEST ${LIBHELLO_SRC})
  24. ##添加OCI的yashandb依赖文件
  25. target_link_libraries(YAS_OCI_TEST ${YAS_OCI_LIBRARY})
  26. [yashan103@localhost oci_dir]$
  27. [yashan103@localhost oci_dir]$
  28. [yashan103@localhost oci_dir]$ cat CMakeLists.txt
  29. cmake_minimum_required(VERSION 2.8.12)
  30. # CMAKE的工程名
  31. project(YAS_OCI_TEST)
  32. set(CMAKE_CXX_STANDARD 11)
  33. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  34. set(BUILD_USE_64BITS on)
  35. set(CMAKE_CONFIGURATION_TYPES "Debug" CACHE STRING "" FORCE)
  36. set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "" FORCE)
  37. #设置OCI接口的源代码路径
  38. SET(LIBHELLO_SRC
  39. ./bb.c
  40. )
  41. #查找库文件
  42. find_library(YAS_OCI_LIBRARY NAMES yas_oci PATHS /home/yashan103/oci_lib/yashandb-oci-23.2.1.100-4-gecc0e02-linux-x86_64)
  43. #确认库文件是否找到
  44. if(NOT YAS_OCI_LIBRARY)
  45.    message(FATAL_ERROR "Could not find libyas_oci.so")
  46. endif()
  47. # 添加OCI.h的头文件
  48. include_directories(/home/yashan103/instantclient_21_12/sdk/include)
  49. ##执行编译操作
  50. add_executable(YAS_OCI_TEST ${LIBHELLO_SRC})
  51. ##添加OCI的yashandb依赖文件
  52. target_link_libraries(YAS_OCI_TEST ${YAS_OCI_LIBRARY})
复制代码
准备Oracle的依赖文件点击检察代码
  1. [yashan103@localhost instantclient_21_12]$ ll
  2. total 272500
  3. -rwxr-xr-x. 1 yashan103 yashan103 42192 Dec 21 2023 adrci
  4. -rw-r--r--. 1 yashan103 yashan103 5780 Dec 21 2023 BASIC_LICENSE
  5. -rw-r--r--. 1 yashan103 yashan103 1634 Dec 21 2023 BASIC_README
  6. drwxrwxr-x. 2 yashan103 yashan103 55 Apr 1 10:46 bin
  7. -rwxr-xr-x. 1 yashan103 yashan103 59544 Dec 21 2023 genezi
  8. drwxrwxr-x. 2 yashan103 yashan103 6 Aug 5 21:38 include
  9. drwxrwxr-x. 2 yashan103 yashan103 6 Aug 5 21:38 lib
  10. lrwxrwxrwx. 1 yashan103 yashan103 21 Jul 23 11:22 libclntshcore.so -> libclntshcore.so.21.1
  11. lrwxrwxrwx. 1 yashan103 yashan103 21 Jul 23 11:22 libclntshcore.so.12.1 -> libclntshcore.so.21.1
  12. lrwxrwxrwx. 1 yashan103 yashan103 21 Jul 23 11:22 libclntshcore.so.18.1 -> libclntshcore.so.21.1
  13. lrwxrwxrwx. 1 yashan103 yashan103 21 Jul 23 11:22 libclntshcore.so.19.1 -> libclntshcore.so.21.1
  14. lrwxrwxrwx. 1 yashan103 yashan103 21 Jul 23 11:22 libclntshcore.so.20.1 -> libclntshcore.so.21.1
  15. -rwxr-xr-x. 1 yashan103 yashan103 8108592 Dec 21 2023 libclntshcore.so.21.1
  16. lrwxrwxrwx. 1 yashan103 yashan103 17 Jul 23 11:22 libclntsh.so -> libclntsh.so.21.1
  17. lrwxrwxrwx. 1 yashan103 yashan103 17 Jul 23 11:22 libclntsh.so.10.1 -> libclntsh.so.21.1
  18. lrwxrwxrwx. 1 yashan103 yashan103 17 Jul 23 11:22 libclntsh.so.11.1 -> libclntsh.so.21.1
  19. lrwxrwxrwx. 1 yashan103 yashan103 17 Jul 23 11:22 libclntsh.so.12.1 -> libclntsh.so.21.1
  20. lrwxrwxrwx. 1 yashan103 yashan103 17 Jul 23 11:22 libclntsh.so.18.1 -> libclntsh.so.21.1
  21. lrwxrwxrwx. 1 yashan103 yashan103 17 Jul 23 11:22 libclntsh.so.19.1 -> libclntsh.so.21.1
  22. lrwxrwxrwx. 1 yashan103 yashan103 17 Jul 23 11:22 libclntsh.so.20.1 -> libclntsh.so.21.1
  23. -rwxr-xr-x. 1 yashan103 yashan103 84191392 Dec 21 2023 libclntsh.so.21.1
  24. -rw-r--r--. 1 yashan103 yashan103 3412144 Apr 1 10:46 libcrypto.so.1.1
  25. lrwxrwxrwx. 1 yashan103 yashan103 14 Apr 1 10:46 libcsvexp.so -> libcsvexp.so.0
  26. lrwxrwxrwx. 1 yashan103 yashan103 20 Apr 1 10:46 libcsvexp.so.0 -> libcsvexp.so.1.1.100
  27. -rwxr-xr-x. 1 yashan103 yashan103 241088 Apr 1 10:46 libcsvexp.so.1.1.100
  28. lrwxrwxrwx. 1 yashan103 yashan103 15 Apr 1 10:46 liblz4.so -> liblz4.so.1.9.3
  29. lrwxrwxrwx. 1 yashan103 yashan103 15 Apr 1 10:46 liblz4.so.1 -> liblz4.so.1.9.3
  30. -rw-r--r--. 1 yashan103 yashan103 236216 Apr 1 10:46 liblz4.so.1.9.3
  31. -rwxr-xr-x. 1 yashan103 yashan103 5844176 Dec 21 2023 libnnz21.so
  32. lrwxrwxrwx. 1 yashan103 yashan103 21 Jul 23 11:22 libocci_gcc53.so -> libocci_gcc53.so.21.1
  33. -rwxr-xr-x. 1 yashan103 yashan103 956832 Dec 21 2023 libocci_gcc53.so.21.1
  34. lrwxrwxrwx. 1 yashan103 yashan103 15 Jul 23 11:22 libocci.so -> libocci.so.21.1
  35. lrwxrwxrwx. 1 yashan103 yashan103 15 Jul 23 11:22 libocci.so.10.1 -> libocci.so.21.1
  36. lrwxrwxrwx. 1 yashan103 yashan103 15 Jul 23 11:22 libocci.so.11.1 -> libocci.so.21.1
  37. lrwxrwxrwx. 1 yashan103 yashan103 15 Jul 23 11:22 libocci.so.12.1 -> libocci.so.21.1
  38. lrwxrwxrwx. 1 yashan103 yashan103 15 Jul 23 11:22 libocci.so.18.1 -> libocci.so.21.1
  39. lrwxrwxrwx. 1 yashan103 yashan103 15 Jul 23 11:22 libocci.so.19.1 -> libocci.so.21.1
  40. lrwxrwxrwx. 1 yashan103 yashan103 15 Jul 23 11:22 libocci.so.20.1 -> libocci.so.21.1
  41. -rwxr-xr-x. 1 yashan103 yashan103 2374808 Dec 21 2023 libocci.so.21.1
  42. -rwxr-xr-x. 1 yashan103 yashan103 142717504 Dec 21 2023 libociei.so
  43. -r-xr-xr-x. 1 yashan103 yashan103 153600 Dec 21 2023 libocijdbc21.so
  44. -rwxr-xr-x. 1 yashan103 yashan103 116208 Dec 21 2023 liboramysql.so
  45. -rw-r--r--. 1 yashan103 yashan103 771118 Apr 1 10:46 libpcre2-8.a
  46. lrwxrwxrwx. 1 yashan103 yashan103 20 Apr 1 10:46 libpcre2-8.so -> libpcre2-8.so.0.10.4
  47. lrwxrwxrwx. 1 yashan103 yashan103 20 Apr 1 10:46 libpcre2-8.so.0 -> libpcre2-8.so.0.10.4
  48. -rw-r--r--. 1 yashan103 yashan103 383080 Apr 1 10:46 libpcre2-8.so.0.10.4
  49. lrwxrwxrwx. 1 yashan103 yashan103 13 Apr 1 10:46 libssl.so -> libssl.so.1.1
  50. -rw-r--r--. 1 yashan103 yashan103 693200 Apr 1 10:46 libssl.so.1.1
  51. lrwxrwxrwx. 1 yashan103 yashan103 14 Apr 1 10:46 libyascli.so -> libyascli.so.0
  52. lrwxrwxrwx. 1 yashan103 yashan103 20 Apr 1 10:46 libyascli.so.0 -> libyascli.so.1.1.100
  53. -rwxr-xr-x. 1 yashan103 yashan103 2699208 Apr 1 10:46 libyascli.so.1.1.100
  54. lrwxrwxrwx. 1 yashan103 yashan103 17 Apr 1 10:46 libyas_infra.so -> libyas_infra.so.0
  55. lrwxrwxrwx. 1 yashan103 yashan103 23 Apr 1 10:46 libyas_infra.so.0 -> libyas_infra.so.1.1.100
  56. -rwxr-xr-x. 1 yashan103 yashan103 11305496 Apr 1 10:46 libyas_infra.so.1.1.100
  57. -rwxrwxr-x. 1 yashan103 yashan103 597808 Aug 5 22:04 libyas_oci.so
  58. -rwxr-xr-x. 1 yashan103 yashan103 408008 Apr 1 10:46 libyex_client.so
  59. lrwxrwxrwx. 1 yashan103 yashan103 16 Apr 1 10:46 libzstd.so -> libzstd.so.1.5.2
  60. lrwxrwxrwx. 1 yashan103 yashan103 16 Apr 1 10:46 libzstd.so.1 -> libzstd.so.1.5.2
  61. -rw-r--r--. 1 yashan103 yashan103 1159832 Apr 1 10:46 libzstd.so.1.5.2
  62. drwxr-xr-x. 3 yashan103 yashan103 19 Dec 21 2023 network
  63. -rw-r--r--. 1 yashan103 yashan103 5245454 Dec 21 2023 ojdbc11.jar
  64. -rw-r--r--. 1 yashan103 yashan103 5153068 Dec 21 2023 ojdbc8.jar
  65. drwxr-xr-x. 5 yashan103 yashan103 79 Dec 21 2023 sdk
  66. -rw-r--r--. 1 yashan103 yashan103 5780 Dec 21 2023 SDK_LICENSE
  67. -rw-rw-r--. 1 yashan103 yashan103 1628 Dec 21 2023 SDK_README
  68. -rw-r--r--. 1 yashan103 yashan103 1805644 Dec 21 2023 ucp.jar
  69. -rwxr-xr-x. 1 yashan103 yashan103 236176 Dec 21 2023 uidrvci
  70. -rw-r--r--. 1 yashan103 yashan103 31869 Dec 21 2023 xstreams.jar
  71. -rw-r--r--. 1 yashan103 yashan103 20076 Apr 1 10:46 yacli.h
复制代码
准备Yashandb OCI 依赖文件
点击检察代码
  1. [yashan103@localhost yashandb-oci-23.2.1.100-4-gecc0e02-linux-x86_64]$ pwd
  2. /home/yashan103/oci_lib/yashandb-oci-23.2.1.100-4-gecc0e02-linux-x86_64
  3. [yashan103@localhost yashandb-oci-23.2.1.100-4-gecc0e02-linux-x86_64]$ ll
  4. total 600
  5. -rwxrwxr-x. 1 yashan103 yashan103 597808 Jul 8 17:14 libyas_oci.so
  6. -rwxrwxr-x. 1 yashan103 yashan103 13576 Jul 8 17:14 yasociconntest
复制代码
执行编译命令
点击检察代码
  1. [yashan103@localhost oci_dir]$ ll
  2. total 32
  3. -rw-rw-r--. 1 yashan103 yashan103 4534 Aug 5 23:03 bb.c
  4. drwxrwxr-x. 5 yashan103 yashan103 4096 Aug 5 22:25 CMakeFiles
  5. -rw-rw-r--. 1 yashan103 yashan103 861 Aug 5 23:05 CMakeLists.txt
  6. -rwxrwxr-x. 1 yashan103 yashan103 15896 Aug 5 22:25 YAS_OCI_TEST
  7. [yashan103@localhost oci_dir]$ cmake -B build .
  8. -- Configuring done
  9. -- Generating done
  10. -- Build files have been written to: /home/yashan103/oci_dir
  11. [yashan103@localhost oci_dir]$ make
  12. Scanning dependencies of target YAS_OCI_TEST
  13. [ 50%] Building C object CMakeFiles/YAS_OCI_TEST.dir/bb.c.o
  14. [100%] Linking C executable YAS_OCI_TEST
  15. [100%] Built target YAS_OCI_TEST
复制代码
执行二进制软件
点击检察代码
  1. [yashan103@localhost oci_dir]$ ./YAS_OCI_TEST
  2. test succeed!
复制代码
以一个OCI代码解释代码常用语法的含义

点击检察代码
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <oci.h>
  6. // 调用OCI代码的CALL函数
  7. #define OCI_TEST_CALL(ociFunc) \
  8.     do { \
  9.         sword r = ociFunc; \
  10.         if (r != OCI_SUCCESS) { \
  11.             checkerr(errhp, r); \
  12.             return r; \
  13.         } \
  14.     } while (0)
  15. static text* username = (text*)"sys";
  16. static text* password = (text*)"Cod-2022";
  17. static text* dbname = (text*)"CITEST";
  18. static OCIEnv* envhp = NULL;
  19. static OCIError* errhp = NULL;
  20. static OCISvcCtx* svchp = NULL;
  21. static OCISession* authp = NULL;
  22. static OCIServer* srvhp = NULL;
  23. // ## 检查代码执行是否返回异常的函数
  24. void checkerr(OCIError* errhp, sword status)
  25. {
  26.     text errbuf[512];
  27.     sb4 errcode = 0;
  28.       // 根据status 判断OCI执行结果状态
  29.     switch (status) {
  30.         case OCI_SUCCESS:
  31.             break;
  32.         case OCI_SUCCESS_WITH_INFO:
  33.             (void)printf("Error - OCI_SUCCESS_WITH_INFO\n");
  34.             break;
  35.         case OCI_NEED_DATA:
  36.             (void)printf("Error - OCI_NEED_DATA\n");
  37.             break;
  38.         case OCI_NO_DATA:
  39.             (void)printf("Error - OCI_NODATA\n");
  40.             break;
  41.         case OCI_ERROR:
  42.             (void)OCIErrorGet((dvoid*)errhp, (ub4)1, (text*)NULL, &errcode, errbuf, (ub4)sizeof(errbuf),
  43.                               OCI_HTYPE_ERROR);
  44.             (void)printf("Error - %.*s\n", 512, errbuf);
  45.             break;
  46.         case OCI_INVALID_HANDLE:
  47.             (void)printf("Error - OCI_INVALID_HANDLE\n");
  48.             break;
  49.         case OCI_STILL_EXECUTING:
  50.             (void)printf("Error - OCI_STILL_EXECUTE\n");
  51.             break;
  52.         case OCI_CONTINUE:
  53.             (void)printf("Error - OCI_CONTINUE\n");
  54.             break;
  55.         default:
  56.             break;
  57.     }
  58. }
  59. // 单行绑定导入数据
  60. sword testSingleBind()
  61. {
  62.     OCIStmt* stmthp = NULL;
  63.     // 申请一个句柄
  64.     (void)OCIHandleAlloc((dvoid*)envhp, (dvoid**)&stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid**)0);
  65.     OCIBind* bindp1 = NULL;
  66.     OCIDefine* definep1 = NULL;
  67.     ub4 intOut1[3] = {0, 1, 2};
  68.     OraText* sql = "drop table if exists tbl_bind";
  69.     // 类似JDBC的prepare statement
  70.     //在 OCI(Oracle Call Interface)中,ub4 是 Oracle C 数据类型之一,它代表一个无符号的 4 字节(32位)整数。OCI 是 Oracle 数据库提供的编程接口,允许开发者使用 C 或 C++ 编写可以与 Oracle 数据库交互的程序。
  71.     OCI_TEST_CALL(OCIStmtPrepare(stmthp, errhp, sql, (ub4)strlen((char*)sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
  72.     // 类似JDBC的执行
  73.     OCI_TEST_CALL(OCIStmtExecute(svchp, stmthp, errhp, (ub4)1, (ub4)0, NULL, NULL, OCI_DEFAULT));
  74.     sql = "create table tbl_bind(col1 int, col2 varchar(20))";
  75.     OCI_TEST_CALL(OCIStmtPrepare(stmthp, errhp, sql, (ub4)strlen((char*)sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
  76.     OCI_TEST_CALL(OCIStmtExecute(svchp, stmthp, errhp, (ub4)1, (ub4)0, NULL, NULL, OCI_DEFAULT));
  77.     sql = "insert into tbl_bind values (:1, '11')";
  78.     OCI_TEST_CALL(OCIStmtPrepare(stmthp, errhp, sql, (ub4)strlen((char*)sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
  79.     OCI_TEST_CALL(OCIBindByPos(stmthp, &bindp1, errhp, (ub4)1, (dvoid*)intOut1, (sb4)sizeof(ub4), SQLT_INT, 0, 0, 0, 0, NULL, OCI_DEFAULT));
  80.     OCI_TEST_CALL(OCIStmtExecute(svchp, stmthp, errhp, (ub4)3, (ub4)0, NULL, NULL, OCI_DEFAULT));
  81.    
  82.     ub4 rowcnt;
  83.     OCI_TEST_CALL(OCIAttrGet((CONST dvoid*)stmthp, (ub4)OCI_HTYPE_STMT, (void*)&rowcnt, (ub4*)0, (ub4)OCI_ATTR_ROW_COUNT, errhp));
  84.     if (rowcnt != 3) {
  85.         return OCI_ERROR;
  86.     }
  87.    
  88.     // 单行取数据
  89.     sql = "select col1 + 99 from tbl_bind";
  90.     OCI_TEST_CALL(OCIStmtPrepare(stmthp, errhp, sql, (ub4)strlen((char*)sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
  91.     OCI_TEST_CALL(OCIDefineByPos(stmthp, &definep1, errhp, (ub4)1, (dvoid*)intOut1, (sb4)sizeof(ub4), SQLT_INT, 0, 0, 0, OCI_DEFAULT));
  92.     OCI_TEST_CALL(OCIStmtExecute(svchp, stmthp, errhp, (ub4)3, (ub4)0, NULL, NULL, OCI_DEFAULT));
  93.     if (intOut1[0] != 99 || intOut1[1] != 100 || intOut1[2] != 101) {
  94.         return OCI_ERROR;
  95.     }
  96.       //释放句柄
  97.     (void)OCIHandleFree((dvoid*)stmthp, (ub4)OCI_HTYPE_STMT);
  98.     return OCI_SUCCESS;
  99. }
  100. // 批量绑定导入数据
  101. sword testBatchBind()
  102. {
  103.     OCIStmt* stmthp = NULL;
  104.     (void)OCIHandleAlloc((dvoid*)envhp, (dvoid**)&stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid**)0);
  105.     OCIBind* bindp = NULL;
  106.     OCIDefine* definep = NULL;
  107.     sb1 intIn1[4];
  108.     sb2 ind1[4];
  109.     ub2 rlen1[4];
  110.     OraText* sql = "drop table if exists tbl_bind";
  111.     OCI_TEST_CALL(OCIStmtPrepare(stmthp, errhp, sql, (ub4)strlen((char*)sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
  112.     OCI_TEST_CALL(OCIStmtExecute(svchp, stmthp, errhp, (ub4)1, (ub4)0, NULL, NULL, OCI_DEFAULT));
  113.     sql = "create table tbl_bind(col1 bigint)";
  114.     OCI_TEST_CALL(OCIStmtPrepare(stmthp, errhp, sql, (ub4)strlen((char*)sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
  115.     OCI_TEST_CALL(OCIStmtExecute(svchp, stmthp, errhp, (ub4)1, (ub4)0, NULL, NULL, OCI_DEFAULT));
  116.     intIn1[0] = 0;
  117.     intIn1[1] = 1;
  118.     intIn1[2] = 2;
  119.     ind1[0] = 0;
  120.     ind1[1] = 0;
  121.     ind1[2] = 0;
  122.     rlen1[0] = 1;
  123.     rlen1[1] = 1;
  124.     rlen1[2] = 1;
  125.     OraText* sql1 = "insert into tbl_bind values (:1)";
  126.     OCI_TEST_CALL(OCIStmtPrepare(stmthp, errhp, sql1, (ub4)strlen((char*)sql1), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
  127.     OCI_TEST_CALL(OCIBindByPos(stmthp, &bindp, errhp, (ub4)1, (dvoid*)intIn1, (sb4)sizeof(sb1), SQLT_INT, ind1, rlen1, 0, 0, NULL, OCI_DEFAULT));
  128.     OCI_TEST_CALL(OCIStmtExecute(svchp, stmthp, errhp, (ub4)3, (ub4)0, NULL, NULL, OCI_DEFAULT));
  129.    
  130.     // 批量取数据
  131.     OraText* sql2 = "select col1 from tbl_bind order by rowid";
  132.     OCI_TEST_CALL(OCIStmtPrepare(stmthp, errhp, sql2, (ub4)strlen((char*)sql2), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT));
  133.     OCI_TEST_CALL(OCIDefineByPos(stmthp, &definep, errhp, (ub4)1, (dvoid*)intIn1, (sb4)sizeof(sb1), SQLT_INT, ind1, rlen1, 0, OCI_DEFAULT));
  134.     OCI_TEST_CALL(OCIStmtExecute(svchp, stmthp, errhp, (ub4)3, (ub4)0, NULL, NULL, OCI_DEFAULT));
  135.     if (intIn1[0] != 0 || intIn1[1] != 1 || intIn1[2] != 2) {
  136.         return OCI_ERROR;
  137.     }
  138.     if (ind1[0] != 0 || ind1[1] != 0 || ind1[2] != 0) {
  139.         return OCI_ERROR;
  140.     }
  141.     if (rlen1[0] != 1 || rlen1[1] != 1 || rlen1[2] != 1) {
  142.         return OCI_ERROR;
  143.     }
  144.    
  145.     (void)OCIHandleFree((dvoid*)stmthp, (ub4)OCI_HTYPE_STMT);
  146.     return OCI_SUCCESS;
  147. }
  148. // 连接数据库
  149. sword testConnect()
  150. {
  151.     sword errcode = 0;
  152.     errcode = OCIEnvCreate((OCIEnv**)&envhp, (ub4)OCI_THREADED, (dvoid*)0, (dvoid * (*)(dvoid*, size_t))0,
  153.                      (dvoid * (*)(dvoid*, dvoid*, size_t))0, (void (*)(dvoid*, dvoid*))0, (size_t)0, (dvoid**)0);
  154.     if (errcode != 0) {
  155.         (void)printf("OCIEnvCreate failed with errcode = %d.\n", errcode);
  156.         return OCI_ERROR;
  157.     }
  158.     (void)OCIHandleAlloc((dvoid*)envhp, (dvoid**)&errhp, OCI_HTYPE_ERROR, (size_t)0, (dvoid**)0);
  159.     (void)OCIHandleAlloc((dvoid*)envhp, (dvoid**)&svchp, OCI_HTYPE_SVCCTX, (size_t)0, (dvoid**)0);
  160.     (void)OCIHandleAlloc((dvoid*)envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER, (size_t)0, (dvoid**)0);
  161.     OCI_TEST_CALL(OCIServerAttach(srvhp, errhp, (text*)dbname, (sb4)strlen((char*)dbname), 0));
  162.     (void)OCIAttrSet((dvoid*)svchp, OCI_HTYPE_SVCCTX, (dvoid*)srvhp, (ub4)0, OCI_ATTR_SERVER, (OCIError*)errhp);
  163.     (void)OCIHandleAlloc((dvoid*)envhp, (dvoid**)&authp, (ub4)OCI_HTYPE_SESSION, (size_t)0, (dvoid**)0);
  164.     (void)OCIAttrSet((dvoid*)authp, (ub4)OCI_HTYPE_SESSION, (dvoid*)username, (ub4)strlen((char*)username), (ub4)OCI_ATTR_USERNAME, errhp);
  165.     (void)OCIAttrSet((dvoid*)authp, (ub4)OCI_HTYPE_SESSION, (dvoid*)password, (ub4)strlen((char*)password), (ub4)OCI_ATTR_PASSWORD, errhp);
  166.     OCI_TEST_CALL(OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, (ub4)OCI_DEFAULT));
  167.     (void)OCIAttrSet((dvoid*)svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid*)authp, (ub4)0, (ub4)OCI_ATTR_SESSION, errhp);
  168.     return OCI_SUCCESS;
  169. }
  170. // 关闭数据库连接
  171. sword testDisconnect()
  172. {
  173.     OCI_TEST_CALL(OCISessionEnd(svchp, errhp, authp, (ub4)0));
  174.     OCI_TEST_CALL(OCIServerDetach(srvhp, errhp, (ub4)OCI_DEFAULT));
  175.     (void)OCIHandleFree((dvoid*)authp, (ub4)OCI_HTYPE_SESSION);
  176.     (void)OCIHandleFree((dvoid*)srvhp, (ub4)OCI_HTYPE_SERVER);
  177.     (void)OCIHandleFree((dvoid*)svchp, (ub4)OCI_HTYPE_SVCCTX);
  178.     (void)OCIHandleFree((dvoid*)errhp, (ub4)OCI_HTYPE_ERROR);
  179.     (void)OCIHandleFree((dvoid*)envhp, (ub4)OCI_HTYPE_ENV);
  180.     return OCI_SUCCESS;
  181. }
  182. sword runTest()
  183. {
  184.     if (testConnect() != OCI_SUCCESS) {
  185.         return 1;
  186.     }
  187.     if (testSingleBind() != OCI_SUCCESS) {
  188.         return 2;
  189.     }
  190.     if (testBatchBind() != OCI_SUCCESS) {
  191.         return 3;
  192.     }
  193.     if (testDisconnect() != OCI_SUCCESS) {
  194.         return 5;
  195.     }
  196.     return 0;
  197. }
  198. int main(argc, argv)
  199. int argc;
  200. char* argv[];
  201. {
  202.     sword runResult = runTest();
  203.     if (runResult == 0) {
  204.         printf("test succeed!\n");
  205.     } else {
  206.         printf("test failed! failed test num : %d\n", runResult);
  207.     }
  208.     return runResult;
  209. }
复制代码
参考资料https://www.yashandb.com/newsinfo/7488285.html?templateId=1718516

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

诗林

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表