通过OCCI连接Oracle数据库

开发环境:VS2015 64位

库文件路径:sdk/lib/msvc/vc14(注意千万不要用最外层那个,那个是默认版本VC10

环境变量可以不用配置,直接把basic下的所有dll文件拷贝至工程生成的可执行文件目录下

官方描述:https://docs.oracle.com/database/121/LNCPP/install.htm#LNCPP20104

Using OCCI with Microsoft Visual C++

The Oracle Database 12c Release 1 (12.1) includes OCCI libraries for developing applications with Microsoft Visual C++ version 10.0 (.NET 2010 SP1 10.0), Microsoft Visual C++ version 11.0 (.NET 2012 11.0), Microsoft Visual C++ version 12.0 (.NET 2013 12.0), and Intel 12.1 C compilers with Microsoft Visual Studio 2010 STLs. Microsoft Visual C++ version 8.0 and version 9.0 are no longer supported.

Microsoft Visual C++ version 10.0 libraries are installed in the following default locations:

ORACLE_BASE\ORACLE_HOME\bin\oraocci12.dll
ORACLE_BASE\ORACLE_HOME\oci\lib\msvc\oraocci12.lib

Copies of these two files are also installed under the directory:

ORACLE_BASE\ORACLE_HOME\oci\lib\msvc\vc10

Microsoft Visual C++ 2012 OCCI libraries are installed in the following default location:

ORACLE_BASE\ORACLE_HOME\oci\lib\msvc\vc11

When developing OCCI applications with MSVC++ 2012, ensure that the OCCI libraries are correctly selected from this directory for linking and executing.

Microsoft Visual C++ 2013 OCCI libraries are installed in the following default location:

ORACLE_BASE\ORACLE_HOME\oci\lib\msvc\vc12

When developing OCCI applications with MSVC++ 2013, ensure that the OCCI libraries are correctly selected from this directory for linking and executing.

Applications should link with the appropriate OCCI library. You must ensure that the corresponding DLL is located in the Windows system PATH.

Applications that link to MSVCRTD.DLL, a debug version of Microsoft C-Runtime, /MDd compiler flag, should link with these specific OCCI libraries: oraocci12d.lib and oraocci12d.dll.

All Instant Client packages contain the versions of the OCCI DLLs that are compatible with Microsoft Visual C++ version 10.0.

OCCI相关下载(Oracle1202 SDK、Basic):

链接:https://pan.baidu.com/s/1KcJWvEJ5T0hQzP4h61KJ5Q 
提取码:wbfw

#include 
#include 
#include 

using namespace std;
using namespace oracle::occi;

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

/*
功能:使用OCCI连接Oracle数据库,查询每列名称和相应数据
*/
int main()
{
	Environment *env;
	Connection *conn;
	Statement *stmt;
	ResultSet *rs;
	string username = "c##hlj";
	string password = "123456";
	string connstring = "192.168.192.250:1521/CDBDB";
	string sql, strname;
	int isno;

	try {
		env = Environment::createEnvironment("ZHS16GBK", "UTF8", Environment::THREADED_UNMUTEXED; //创建一个环境变量,解决中文乱码问题,支持多线程
		conn = env->createConnection(username, password, connstring);//创建一个数据库连接对象
		stmt = conn->createStatement(); //创建一个Statement对象
	}
	catch (SQLException e)
	{
		cout << e.getMessage() << endl;
	}

	string strTable = "TB_FREQINFO";

	// 获取表总列数
	MetaData custtab_metaData = conn->getMetaData(strTable, MetaData::PTYPE_TABLE);
	vector listOfColumns = custtab_metaData.getVector(MetaData::ATTR_LIST_COLUMNS);
	unsigned int uiColumnNum = listOfColumns.size();

	vector> vtResult;
	sql = "SELECT * FROM ""C##HLJ""."""+ strTable +""" ORDER BY TO_NUMBER(ID)"; // SQL语句
	stmt->setSQL(sql); //设置SQL语句到Statement对象中
	try {
		rs = stmt->executeQuery();//执行SQL语句
		while (rs->next()) { //用循环,一条一条地取得查询的结果记录
			map mapVal;
			for (int i=1;i<= uiColumnNum;i++)
			{
				// 获取表列名
				mapVal.emplace(std::make_pair(listOfColumns[i - 1].getString(MetaData::ATTR_NAME), rs->getString(i)));
			}
			vtResult.emplace_back(mapVal);
		}
	}
	catch (SQLException ex) {
		cout << "Error Number : " << ex.getErrorCode() << endl; //取出异常代码
		cout << ex.getMessage() << endl; //取出异常信息
	}

	// 释放vector
	// 注:当使用MetaData时,必须在数据库对象释放前释放,否则会报错
	vector().swap(listOfColumns);

	conn->terminateStatement(stmt); //终止Statement对象
	env->terminateConnection(conn); //断开数据库连接
	Environment::terminateEnvironment(env); //终止环境变量

	return 1;
}

代码仅做参考!

你可能感兴趣的:(数据库相关)