oracle连12c的驱动,c++ - 如何通过Qt中的ODBC驱动程序连接到oracle 12c? - 堆栈内存溢出...

修改后的答案:

在Ubuntu 18.04上的Qt Open Source 5.12中配置和测试与Oracle 12.2数据库的ODBC连接的步骤:

1)安装先决条件(如果尚未安装)。

sudo apt-get install build-essential libaio1

2)安装ODBC驱动程序管理器(unixODBC)。

### Install packages

sudo apt-get install unixodbc unixodbc-dev

### Verify unixODBC installation

/usr/bin/odbcinst -j

# Expected output:

unixODBC 2.3.4

DRIVERS............: /etc/odbcinst.ini

SYSTEM DATA SOURCES: /etc/odbc.ini

FILE DATA SOURCES..: /etc/ODBCDataSources

USER DATA SOURCES..: /home//.odbc.ini

SQLULEN Size.......: 8

SQLLEN Size........: 8

SQLSETPOSIROW Size.: 8

3)安装Oracle ODBC驱动程序。

### Download files below from

### https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

instantclient-basic-linux.x64-12.2.0.1.0.zip

instantclient-odbc-linux.x64-12.2.0.1.0-2.zip

### Unzip files to /opt/oracle

sudo unzip instantclient-basic-linux.x64-12.2.0.1.0.zip -d /opt/oracle

sudo unzip instantclient-odbc-linux.x64-12.2.0.1.0-2.zip -d /opt/oracle

4)创建tnsnames.ora文件并向其中添加数据库连接。

### File: /opt/oracle/instantclient_12_2/network/admin/tnsnames.ora

oradbconn =

(

DESCRIPTION =

(

ADDRESS_LIST =

(ADDRESS =

(PROTOCOL = TCP)

(HOST = oradbserver.acme.com)

(PORT = 1521)

)

)

(

CONNECT_DATA = (SERVICE_NAME = oradb.acme.com)

)

)

5)运行odbc_update_ini.sh ,它创建/更新将unixODBC注册到unixODBC并部分配置Oracle ODBC数据源所需的unixODBC配置。

cd /opt/oracle/instantclient_12_2

sudo ./odbc_update_ini.sh /

# This error can be ignored:

# *** ODBCINI environment variable not set,defaulting it to HOME directory!

运行odbc_update_ini.sh后,unixODBC配置文件的预期内容:

### /etc/odbcinst.ini (Tells unixODBC where to find Oracle ODBC driver)

[Oracle 12c ODBC driver]

Description = Oracle ODBC driver for Oracle 12c

Driver = /opt/oracle/instantclient_12_2/libsqora.so.12.1

Setup =

FileUsage =

CPTimeout =

CPReuse =

### ~/.odbc.ini (Partially-configured Oracle ODBC Data Source)

[OracleODBC-12c]

Application Attributes = T

Attributes = W

BatchAutocommitMode = IfAllSuccessful

BindAsFLOAT = F

.

.

.

6)将“ Chown”〜/ .odbc.ini添加到当前登录用户的uid / gid。 该文件最初创建为root:root。 如果所有权没有更改,则通过ODBC驱动程序的数据库连接可能会失败。

sudo chown $(id -u):$(id -g) ~/.odbc.ini

7)通过添加/更新如下所示的〜/ odbc.ini参数,完成数据源配置。

### ~/.odbc.ini

ServerName = oradbconn ### Should reference the connection in the tnsnames.ora file

UserID = oradb_user ### User name for your Oracle database connection

Password = oradb_password ### Password for username above

9)使用Oracle环境变量更新.bash_profile并获取文件。

### ~/.bash_profile

export TNS_ADMIN=/opt/oracle/instantclient_12_2/network/admin

export LD_LIBRARY_PATH=/opt/oracle/instantclient_12_2

### Source the file

. ~/.bash_profile

10)验证与Oracle ODBC数据源的连接。

isql -v OracleODBC-12c

预期产量:

+---------------------------------------+

| Connected! |

| |

| sql-statement |

| help [tablename] |

| quit |

| |

+---------------------------------------+

SQL>

11)创建程序以测试ODBC与Oracle的连接。

your-project.pro:

.

.

QT += sql ### Add this to make SQL libraries available

main.cpp中:

#include

#include

#include

#include

#include

#include

int main( int argc, char *argv[] )

{

QCoreApplication a(argc, argv);

// "OracleODBC-12c" is the data source configured in ~/.odbc.ini

QSqlDatabase db = QSqlDatabase::addDatabase( "QODBC3", "OracleODBC-12c" );

if(db.open())

qDebug() << "Opened db connection!";

else

qDebug() << db.lastError().text();

QSqlQuery query(db);

// Example query selects a few table names from the system catalog

query.exec("SELECT table_name FROM all_tables WHERE owner = 'SYS' and ROWNUM <= 3");

while (query.next()) {

QString table_name = query.value(0).toString();

qDebug() << table_name;

}

return a.exec();

}

预期输出(表名称可能有所不同):

Opened db connection!

"DUAL"

"SYSTEM_PRIVILEGE_MAP"

"TABLE_PRIVILEGE_MAP"

以上步骤已在以下OS / Qt版本上进行了验证:

$ uname -a

Linux ubuntu 4.18.0-25-generic #26~18.04.1-Ubuntu SMP Thu Jun 27 07:28:31 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

$ ./qmake -v | grep Qt

Using Qt version 5.12.4 in /opt/Qt/5.12.4/gcc_64/lib

原始答案:

看来您正在尝试使用SQL Server的ODBC驱动程序连接到Oracle,这对我来说没有意义。

db->setDatabaseName("DRIVER={ODBC Driver 17 for SQL Server};"

注意:您应该使用本机驱动程序(如果可用),而不是ODBC驱动程序。 如果没有本机驱动程序,则ODBC支持可以用作兼容数据库的后备。

有关使用本机Oracle OCI驱动程序进行构建的信息在此处

您可以从此处下载包含OCI驱动程序的Oracle Instant Client。 根据QT文档,您将需要Instant Client Package-Basic和Instant Client Package-SDK。如果您仍然想使用ODBC,则可以尝试下载Oracle的“ ODBC Package-用于启用ODBC应用程序的其他库”。在即时客户端下载页面上。对于所有这些下载,请确保获得与数据库相对应的客户端版本。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值