oracle c occi,C++访问ORACLE数据库之OCCI(windows)

参考文章:

OCCI要访问ORACLE数据库,需要几部分文件:include头文件,lib库文件,dll文件,其实基本步骤是怎么找到这些文件。

1.下载开发包:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

4f2bc7de1d92df56259b1cb60d578a47.png

两个同时选中,右键解压

83b91a423f0dad8d55a76653155e2029.png

2.设置环境变量:

计算机->右键属性->高级系统设置->环境变量,里设置系统变量

ORACLE_HOME=D:softwareoracleinstantclient_11_2

Path=%ORACLE_HOME%

3.配置VS2008:

先创建一个空工程OCCIConnectOracle

A.添加库文件目录:

解决方案中右键OCCIConnectOracle->属性->c/c++->常规->附加包含目录,中输入库目录地址:D:softwareoracleinstantclient_11_2sdkinclude

B.添加头文件目录:

属性->链接器->常规->附加库目录:D:softwareoracleinstantclient_11_2sdklibmsvcvc8

C.添加库文件:

属性->链接器->输入->附加依赖项:此处有oraocci11.lib和oraocci11d.lib两个文件,其中oraocci11.lib为release模式时使用,oraocci11d.lib为debug模式时使用。

4.代码部分:

/***************************采用oracle本身提供OCCI方式操作数据库*******************/

//代码的目的就是验证makefile中oracle的头文件和lib文件路径是否正确了

#include

#define WIN32COMMON //避免函数重定义错误

#include

using namespace std;

using namespace oracle::occi;

int main()

{

system("pause");

//创建OCCI上下文环境

Environment *env = Environment::createEnvironment();

if (NULL == env) {

printf("createEnvironment error.

");

return -1;

}

else

cout << "success" << endl;

string name = "system";

string pass = "123";

string srvName = "192.168.26.74:1521/orcl";

try

{

//创建数据库连接

Connection *conn = env->createConnection(name, pass, srvName);//用户名,密码,数据库名

if(NULL == conn) {

printf("createConnection error.

");

return -1;

}

else

cout << "conn success" << endl;

// 数据操作,创建Statement对象

Statement *pStmt = NULL; // Statement对象

pStmt = conn->createStatement();

if(NULL == pStmt) {

printf("createStatement error.

");

return -1;

}

// 查询数据库时间

std::string strTemp;

ResultSet *pRs = pStmt->executeQuery(

"SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL");

while(pRs->next()) {

strTemp = pRs->getString(1);

printf("db time:%s.

", strTemp.c_str());

// int类型取值用getInt()

break;

}

pStmt->closeResultSet(pRs);

//--------插入---------

// 指定DML为自动提交

pStmt->setAutoCommit(TRUE);

// 设置执行的SQL语句

//pStmt->setSQL("INSERT INTO TA (ID, NAME) VALUES (1, 'ZS')");

pStmt->setSQL("INSERT INTO TABLE_TEST_WANG (NAME, NUM, AGE) VALUES ('邓超', '99', '41')");

// 执行SQL语句

unsigned int nRet = pStmt->executeUpdate();

if(nRet == 0) {

printf("executeUpdate insert error.

");

}

// 终止Statement对象

conn->terminateStatement(pStmt);

// 关闭连接

env->terminateConnection(conn);

// pEnv->terminateConnection(pConn);

}

catch (SQLException e)

{

cout << e.what() << endl;

system("pause");

return -1;

}

// 释放OCCI上下文环境

Environment::terminateEnvironment(env);

cout << "end!" << endl;

system("pause");

return 0;

}

5.效果图:

25d142589fa8f8eb78f3c64ef8fdf827.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值