C++06:使用OTL操作Oracle数据库

2024-06-15 03:08

本文主要是介绍C++06:使用OTL操作Oracle数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、编写代码

注:以下代码来自OTL示例

/* * otl_test.cpp * */ #include <iostream>
using namespace std;#include <cstdio>#define OTL_ORA10G_R2 // Compile OTL 4.0/OCI10gR2
#include <otlv4.h> // include the OTL 4.0 header file
otl_connect db; // connect objectvoid insert()
// insert rows into table
{otl_stream o(50, // buffer size"insert into test_tab values(:f1<int>,:f2<char[31]>)",// SQL statementdb // connect object);o.set_commit(0); // turn off stream's "auto-commit"char tmp[32];for (int i = 1; i <= 123; ++i) {sprintf(tmp, "Name%d", i);o << i << tmp;}o.flush(); // flush the stream's dirty buffer: // execute the INSERT for the rows // that are still in the stream bufferdb.commit_nowait(); // commit with no wait (new feature of Oracle 10.2)
}void select() {otl_stream i(50, // buffer size"select * from test_tab where f1>=:f<int> and f1<=:f*2",// SELECT statementdb // connect object);// create select streamfloat f1;char f2[31];i << 8; // assigning :f = 8// SELECT automatically executes when all input variables are// assigned. First portion of output rows is fetched to the bufferwhile (!i.eof()) { // while not end-of-datai >> f1 >> f2;cout << "f1=" << f1 << ", f2=" << f2 << endl;}i << 4; // assigning :f = 4// SELECT automatically executes when all input variables are// assigned. First portion of output rows is fetched to the bufferwhile (!i.eof()) { // while not end-of-datai >> f1 >> f2;cout << "f1=" << f1 << ", f2=" << f2 << endl;}}int main() {otl_connect::otl_initialize(); // initialize OCI environmenttry {db.rlogon("xuanyuan/xuanyuan"); // connect to Oracleotl_cursor::direct_exec(db, "drop table test_tab",otl_exception::disabled // disable OTL exceptions); // drop tableotl_cursor::direct_exec(db,"create table test_tab(f1 number, f2 varchar2(30))"); // create tableinsert(); // insert records into tableselect(); // select records from table}catch (otl_exception& p) { // intercept OTL exceptionscerr << p.msg << endl; // print out error messagecerr << p.stm_text << endl; // print out SQL that caused the errorcerr << p.var_info << endl; // print out the variable that caused the error}db.logoff(); // disconnect from Oraclereturn 0;}

二、编译代码

g++ -o"otl_test" otl_test.cpp -lclntsh -I"$ORACLE_HOME/rdbms/public"

三、运行程序 otl_test

$ ./otl_test

结果如下:

f1=8, f2=Name8
f1=9, f2=Name9
f1=10, f2=Name10
f1=11, f2=Name11
f1=12, f2=Name12
f1=13, f2=Name13
f1=14, f2=Name14
f1=15, f2=Name15
f1=16, f2=Name16
f1=4, f2=Name4
f1=5, f2=Name5
f1=6, f2=Name6
f1=7, f2=Name7
f1=8, f2=Name8

这篇关于C++06:使用OTL操作Oracle数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062258

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)

《JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)》:本文主要介绍如何在IntelliJIDEA2020.1中创建和部署一个JavaWeb项目,包括创建项目、配置Tomcat服务... 目录简介:一、创建项目二、tomcat部署1、将tomcat解压在一个自己找得到路径2、在idea中添加

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco