天天看點

通過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 <iostream>
#include <map>
#include <occi.h>

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<MetaData> listOfColumns = custtab_metaData.getVector(MetaData::ATTR_LIST_COLUMNS);
	unsigned int uiColumnNum = listOfColumns.size();

	vector<map<string, string>> 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<string,string> 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>
	// 注:當使用MetaData時,必須在資料庫對象釋放前釋放,否則會報錯
	vector<MetaData>().swap(listOfColumns);

	conn->terminateStatement(stmt); //終止Statement對象
	env->terminateConnection(conn); //斷開資料庫連接配接
	Environment::terminateEnvironment(env); //終止環境變量

	return 1;
}
           

代碼僅做參考!