OTL实战小结

简介: 前言:以下内容是项目中使用OTL的部分小结,适合OTL初学者。大牛请绕行。

一、OTL常用接口说明

1)otl_initialize( OCI_THREADED)

初始化OTL环境。需要在程序最开始连接数据库之前调用一次。参数threaded_mode指明程序是否运行在多线程环境,注意由于OTL并没有使用同步锁或者临界段,线程安全并不能够自动得到保证。


#define OCI_DEFAULT 0x00000000   /* the default value for parameters and attributes */

#define OCI_THREADED 0x00000001  /* appl. in threaded environment */

1

2

2)void set_character_set(const int char_set=SQLCS_IMPLICIT);

如果使用了UNICODE,则该方法设置默认或国家的字符集:

SQLCS_IMPLICIT为数据库默认字符集。(默认参数)

SQLCS_NCHAR为数据库国家的字符集。


3)void rlogon(…);

rlogon( “XX/psdXX@110.220.12.33/xe”)

连接数据库。参数同构造函数。

4)void set_max_long_size(const int amax_size);

set_max_long_size(OTL_MAX_LONG_SIZE);

buffer_size参数指明存放大型对象的缓存大小,默认为3276,可以通过otl_connect的set_max_long_size()方法来改变默认的大小值 #define OTL_MAX_LONG_SIZE 80000


5)void auto_commit_off(void); //关闭自动提交

void auto_commit_on(void); //打开自动提交

设置otl_connect对象的auto_commit标志。

一旦关闭了自动连接,意味着所有的提交必须通过commit( )接口实现。


6)void commit(void);

同步的方式提交事务。

void commit_nowait();

异步的方式提交事务。


7)void logoff();

数据库断开。


8)void set_commit(int auto_commit=0);

Set the stream auto-commit flag. When the output buffer is flushed, the current transaction is automatically committed, if the flag is set. By default, the flag is set.


9)void set_batch_error_mode(const bool batch_error_mode)

10)void open(…);

This function open an SQL statement and the statement

gets parsed, all input and output variables get dynamically allocated inside the stream and automatically bound to

the placeholders.


二、VS2010下如何配置

第一步:将OTL需要的lib文件和头文件以文件夹的形式放到工程目录下。

第二步:在工程属性–>配置属性–>C/C++–>附加包含目录下,填写对应头文件相对路径。

第三步:在工程属性–>配置属性–>链接库–>附加库目录下,填写对应lib文件包含的相对路径。

并在附加依赖库中填写oci.lib,oraocci10.lib,ociw32.lib三个依赖库。

第四步:工程中头文件中加入如下的包含:


#define OTL_ORA_TIMESTAMP

#define OTL_ODBC_TIME_ZONE

#define OTL_ORA10G

#define OTL_ORA_UTF8

#define OTL_STL

#define OTL_ORA_MAP_BIGINT_TO_LONG

#define OTL_BIGINT long long

#define OTL_STREAM_READ_ITERATOR_ON

#define OTL_STL

#define OTL_MAX_LONG_SIZE 100*1024


#include "otl/otlv4.h"

1

2

3

4

5

6

7

8

9

10

11

12

第五步:编写属于自己的OTL代码。


三、OTL源码范例(实现功能:增、删、改、查)

//所有代码在VS2010以及数据库操作都没有Bug。


#include "stdafx.h"

#include "sql.h"

#include <stdio.h>



OtlSql::OtlSql()

{

printf("Constructor!\n");

}


OtlSql::~OtlSql()

{

printf("Destructor!\n");

}


bool OtlSql::sql_init(char* strsql)

{

bool bInitFlag = false;


putenv(const_cast<char*>("NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK"));


otl_connect::otl_initialize();

try

{

db.rlogon(strsql);

}

catch(otl_exception& p)

{

printf("%s\n", p.msg); // print out error message

printf("%s\n", p.stm_text); // print out SQL that caused the error

printf("%s\n", p.var_info); // print out the variable that caused the error

return bInitFlag;

}

bInitFlag = true;

return bInitFlag;

}



void OtlSql::sql_close()

{

db.commit();

db.logoff();

}


//1.插入

void OtlSql::sql_insert(int nNo, int nAge, char* pszName, float fScore)

{

char szSqlInsert[512] = {0};

sprintf(szSqlInsert, "insert into student_info(NO, AGE, NAME, SCORE) \

VALUES(:f0<int>, :f1<int>, :f2<char[50]>, :f3<float>)");


try

{

otl_stream ostream(1, szSqlInsert, db);

ostream << nNo << nAge << pszName << fScore;

ostream.close();

}

catch (otl_exception& excp)

{

printf("Error:%s;\n %s;\n %s.\n", excp.msg, excp.stm_text, excp.var_info);

}


}


//2.删除

void OtlSql::sql_delete(char* pszName)

{

char szSqlDelect[512] = {0};

sprintf(szSqlDelect, "delete from student_info where Name = \'%s\'", pszName);


try

{

otl_stream ostream(1, szSqlDelect, db);

ostream.close();

}

catch (otl_exception& excp)

{

printf("Error:%s;\n %s;\n %s.\n", excp.msg, excp.stm_text, excp.var_info);

}

}


//3更新修改

void OtlSql::sql_update(char* pszName, float fScore)

{

char szSqlUpdate[512] = {0};

sprintf(szSqlUpdate, "update student_info set SCORE = :f3<float> where NAME = \'%s\'", pszName);


try

{

otl_stream ostream(1, szSqlUpdate, db);

ostream << fScore;

ostream.close();

}

catch (otl_exception& excp)

{

printf("Error:%s;\n %s;\n %s.\n", excp.msg, excp.stm_text, excp.var_info);

}

}



//4查询

void OtlSql::sql_select(int nNo)

{

char szSqlSelect[512] = {0};

sprintf(szSqlSelect, "select NAME, SCORE from student_info where NO = %d", nNo);


string strName = "";

float fScore = 0.0f;


try

{

otl_stream ostream(1, szSqlSelect, db);

ostream >> strName >> fScore;

cout << "Name: " << strName.c_str() << "\tScore: " << fScore << endl;

ostream.close();

}

catch (otl_exception& excp)

{

printf("Error:%s;\n %s;\n %s.\n", excp.msg, excp.stm_text, excp.var_info);

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

源码详情下载链接:http://download.csdn.net/detail/wojiushiwo987/9351407


四、总结

1.毕业2.5年,第一次项目中使用Oracle数据库,之前学习都是SQLServer用的少。

2.这篇权当积累。


相关文章
|
Linux
linux 交叉编译libpng,libjpeg库
linux 交叉编译libpng,libjpeg库
671 1
|
安全 Java 编译器
阿里巴巴Dragonwell
阿里巴巴Dragonwell
|
SQL 安全 Java
一篇文章彻底理解 HIVE 常见的三种 AUTHENTICATION 认证机制的配置与使用
一篇文章彻底理解 HIVE 常见的三种 AUTHENTICATION 认证机制的配置与使用
|
XML 数据管理 Linux
麒麟系统开发笔记(十二):在国产麒麟系统上编译GDAL库、搭建基础开发环境和基础Demo
麒麟系统上做全球北斗定位终端开发,北斗GPS发过来的是大地坐标,应用需要的是经纬度坐标,所以需要转换,可以使用公式转换,但是之前涉及到了山He智能一个项目使用WG。
|
存储 人工智能 编解码
AI怎么输出不乱码
本文深度解析AI生成内容乱码(如“锟斤拷”)的三大根源:Token切片导致汉字截断、SSE流式传输解析不当、Unicode扩展字符兼容问题;AI导出鸭并提供工程化解决方案——基于TextDecoder的字节流缓冲、标准化Markdown+KaTeX渲染,等一键导出工具,实现从AI输出到PDF/Word的专业无损落地。(239字)
5063 0
|
10月前
|
数据采集 人工智能 自然语言处理
架构演进:从确定性工作流 (Workflow) 到自主智能体 (LLM Agent)
本文对比生成式AI中Workflow(确定性流程)与Agent(自主推理系统)的技术范式,以“智慧旅游规划”为案例,剖析二者在控制流、状态管理与不确定性处理上的本质差异,揭示其适用场景与融合实践路径。
1372 2
|
8月前
|
人工智能 运维 安全
|
人工智能 安全 Cloud Native
Nacos 3.0 架构升级,AI 时代更安全的 Registry
随着Nacos3.0的发布,定位由“更易于构建云原生应用的动态服务发现、配置管理和服务管理平台”升级至“ 一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台 ”。
|
SQL 人工智能 自然语言处理
企业级 AI 数据分析“专家”——Data Agent 推动数据分析民主化
Data Agent(数据智能体)正从辅助工具向企业核心数据分析中枢演进,推动“人人都是分析师”的愿景落地。

热门文章

最新文章