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.这篇权当积累。


相关文章
|
SQL Java 数据库连接
hyorm框架进阶用法
hyorm框架的原生使用方法,源于php laravel框架的orm层实现方案,基于单Query的设计方式,确保在java多线程中的线程安全性。
70 0
|
SQL 存储 数据库连接
SQLServer知识:sqlcmd用法笔记
sqlcmd是一个 Microsoft Win32 命令提示实用工具,可以通过该命令工具实现SQL语句、脚本的执行,并且可以实现脚本任务的自动化。
SQLServer知识:sqlcmd用法笔记
|
SQL 存储 Oracle
PLSQL简介
PLSQL简介
291 0
|
Oracle 数据库 C++
OTL实战小结
以下内容是项目中使用OTL的部分小结,适合OTL初学者。
542 0
|
SQL 关系型数据库 数据库连接
OTL技术应用
什么是OTL:OTL 是 Oracle, Odbc and DB2-CLI TemplateLibrary 的缩写,是一个操控关系数据库的C++模板库,它目前几乎支持所有的当前各种主流数据库,如下表所示:   数据库 访问接口 支持版本 Oracle OCI...
1046 0
|
SQL Oracle 关系型数据库
OTL之Oracle开发总结《转》
OTL之Oracle开发总结---转   关 于OTL,网上介绍的也不少,但看来看去也只是官方的那些文档。OTL很好用,结合官方提供的一些例子,多多尝试才能领悟。经过一个月左右的项目开发,对 OTL也有些了解,在这里总结一下,希望对刚接触OTL的新手有所帮助。
963 0
|
存储 Oracle 关系型数据库
OTL调用Oracle存储过程
OTL很早前用过,今天写东西要调存储过程,程序写完了,调试死活通不过,折腾了一早晨。 最后才发现错误,这里总结一下: 1、代码写的不规范。 有个参数后边少写了个“,”以至于总是抱错。而单独写的测试例子就没问题,后来一步一步跟踪了后才发现。
943 0