问题现象:
表数据
控制台的输出:
问题描述: 编译正常通过,也能成功连接上数据库,成功从数据库中查询数据到occi结果集,从结果集中用getInt取整型字段的数据能成功,但是当字段是char型时用getString获取时报错
环境:
系统:window 7
集成环境:vs2017
数据库:oracle_11gR2
问题代码:
/***************************采用oracle本身提供OCCI方式操作数据库*******************/
//代码的目的就是验证makefile中oracle的头文件和lib文件路径是否正确了
#include <iostream>
#define WIN32COMMON //避免函数重定义错误
#include <occi.h>
using namespace std;
using namespace oracle::occi;
int main()
{
system("pause");
//创建OCCI上下文环境
Environment *env = Environment::createEnvironment();
if (NULL == env) {
cout << "createEnvironment error." << endl;
return -1;
}
else
cout << "success" << endl;
string name = "scott";
string pass = "tiger";
string srvName = "localhost:1521/orcl";
try
{
//创建数据库连接
Connection *conn = env->createConnection(name, pass, srvName);//用户名,密码,数据库名
if (NULL == conn) {
cout << "createConnection error." << endl;
return -1;
}
else
cout << "conn success" << endl;
// 数据操作,创建Statement对象
Statement *pStmt = NULL; // Statement对象
pStmt = conn->createStatement();
if (NULL == pStmt) {
cout << "createStatement error." << endl;
return -1;
}
// 查询数据库时间
std::string strTemp;
ResultSet *pRs = pStmt->executeQuery("select * from ACTICLEDOCUMENT");
while (pRs->next()) {
cout<< pRs->getInt(1)<<endl;
cout<< pRs->getString(2);
// int类型取值用getInt()
}
pStmt->closeResultSet(pRs);
// 终止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;
}
待解决!!