简体   繁体   English

如何通过Qt中的ODBC驱动程序连接到oracle 12c?

[英]How can I connect to oracle 12c via ODBC driver in qt?

I have qt open source 5.12 and ubuntu 18.04. 我有Qt开源5.12和Ubuntu 18.04。 How do I connect to oracle 12c via ODBC? 如何通过ODBC连接到oracle 12c? I tried: 我试过了:

db = new QSqlDatabase(QSqlDatabase::addDatabase("QODBC"));
db->setPort(1234);
db->setDatabaseName("DRIVER={ODBC Driver 17 for SQL Server};"
                    "SERVER=localhost;"
                    "DATABASE=OraDoc;"
                    "Trusted_Connection=yes;");
db->setPassword("MyPasswd");
db->setUserName("system");
if(db->open()) qDebug() << "cool";
else qDebug() << db->lastError().text();

Writes: 写道:

"[Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
 [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2749
 [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. QODBC3: Unable to connect"

Revised Answer: 修改后的答案:

Steps to configure and test ODBC connectivity to an Oracle 12.2 database in Qt Open Source 5.12 on Ubuntu 18.04: 在Ubuntu 18.04上的Qt Open Source 5.12中配置和测试与Oracle 12.2数据库的ODBC连接的步骤:

1) Install pre-requisites (if they aren't already installed). 1)安装先决条件(如果尚未安装)。

sudo apt-get install build-essential libaio1

2) Install ODBC Driver Manager (unixODBC). 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/<logged-in-user>/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

3) Install Oracle ODBC driver. 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) Create tnsnames.ora file and add your database connection to it. 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) Run odbc_update_ini.sh , which creates/updates the unixODBC configuration needed to register the Oracle ODBC driver with unixODBC and partially configure an Oracle ODBC data source. 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!

Expected contents of unixODBC config files after running odbc_update_ini.sh: 运行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 to the uid/gid of the currently-logged in user. 6)将“ Chown”〜/ .odbc.ini添加到当前登录用户的uid / gid。 This file is initially created as root:root. 该文件最初创建为root:root。 If the ownership is not changed, database connections through the ODBC driver may fail. 如果所有权没有更改,则通过ODBC驱动程序的数据库连接可能会失败。

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

7) Complete the data source configuration by adding/updating the ~/odbc.ini parameters shown below. 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) Update .bash_profile with Oracle environment variables and source the file. 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) Verify connection to Oracle ODBC data source. 10)验证与Oracle ODBC数据源的连接。

isql -v OracleODBC-12c

Expected output: 预期产量:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

11) Create program to test ODBC connectivity to Oracle. 11)创建程序以测试ODBC与Oracle的连接。

your-project.pro: your-project.pro:

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

main.cpp: main.cpp中:

#include <iostream>
#include <QCoreApplication>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>

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();
}

Expected output (table names may vary): 预期输出(表名称可能有所不同):

Opened db connection!
"DUAL"
"SYSTEM_PRIVILEGE_MAP"
"TABLE_PRIVILEGE_MAP"

Above steps were verified on OS / Qt version below: 以上步骤已在以下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

Original Answer: 原始答案:

It looks like you're trying to use an ODBC driver for SQL Server to connect to Oracle, which doesn't make sense to me. 看来您正在尝试使用SQL Server的ODBC驱动程序连接到Oracle,这对我来说没有意义。

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

The QT documentation states: QT文档指出:

Note: You should use the native driver, if it is available, instead of the ODBC driver. 注意:您应该使用本机驱动程序(如果可用),而不是ODBC驱动程序。 ODBC support can be used as a fallback for compliant databases if no native driver is available. 如果没有本机驱动程序,则ODBC支持可以用作兼容数据库的后备。

The information about building with the native Oracle OCI driver is here 有关使用本机Oracle OCI驱动程序进行构建的信息在此处

You can download the Oracle instant client that contains the OCI driver from here . 您可以从此处下载包含OCI驱动程序的Oracle Instant Client。 According to the QT docs, you'll need Instant Client Package - Basic" and "Instant Client Package - SDK". If you still want to use ODBC, then you could try downloading Oracle's "ODBC Package - Additional libraries for enabling ODBC applications" on the instant client download page. For all of these downloads, make sure that you get the client version corresponding to your database. 根据QT文档,您将需要Instant Client Package-Basic和Instant Client Package-SDK。如果您仍然想使用ODBC,则可以尝试下载Oracle的“ ODBC Package-用于启用ODBC应用程序的其他库”。在即时客户端下载页面上。对于所有这些下载,请确保获得与数据库相对应的客户端版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM