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

相关文章

Spring Boot整合Redis注解实现增删改查功能(Redis注解使用)

《SpringBoot整合Redis注解实现增删改查功能(Redis注解使用)》文章介绍了如何使用SpringBoot整合Redis注解实现增删改查功能,包括配置、实体类、Repository、Se... 目录配置Redis连接定义实体类创建Repository接口增删改查操作示例插入数据查询数据删除数据更

Mysql数据库聚簇索引与非聚簇索引举例详解

《Mysql数据库聚簇索引与非聚簇索引举例详解》在MySQL中聚簇索引和非聚簇索引是两种常见的索引结构,它们的主要区别在于数据的存储方式和索引的组织方式,:本文主要介绍Mysql数据库聚簇索引与非... 目录前言一、核心概念与本质区别二、聚簇索引(Clustered Index)1. 实现原理(以 Inno

sqlserver、mysql、oracle、pgsql、sqlite五大关系数据库的对象名称和转义字符

《sqlserver、mysql、oracle、pgsql、sqlite五大关系数据库的对象名称和转义字符》:本文主要介绍sqlserver、mysql、oracle、pgsql、sqlite五大... 目录一、转义符1.1 oracle1.2 sqlserver1.3 PostgreSQL1.4 SQLi

使用python生成固定格式序号的方法详解

《使用python生成固定格式序号的方法详解》这篇文章主要为大家详细介绍了如何使用python生成固定格式序号,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录生成结果验证完整生成代码扩展说明1. 保存到文本文件2. 转换为jsON格式3. 处理特殊序号格式(如带圈数字)4

Java使用Swing生成一个最大公约数计算器

《Java使用Swing生成一个最大公约数计算器》这篇文章主要为大家详细介绍了Java使用Swing生成一个最大公约数计算器的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下... 目录第一步:利用欧几里得算法计算最大公约数欧几里得算法的证明情形 1:b=0情形 2:b>0完成相关代码第二步:加

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.