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 */

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"

第五步:编写属于自己的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);
}
}

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

四、总结
1.毕业2.5年,第一次项目中使用Oracle数据库,之前学习都是SQLServer用的少。
2.这篇权当积累。

2015-12-13 pm14:55 思于家中床前

作者:铭毅天下

转载请标明出处,原文地址:http://blog.csdn.net/laoyang360/article/details/50283139

如果感觉本文对您有帮助,请点击‘顶’支持一下,您的支持是我坚持写作最大的动力,谢谢!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铭毅天下

和你一起,死磕Elastic!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值