【问题标题】:How can I connect to oracle 12c via ODBC driver in qt?如何在 qt 中通过 ODBC 驱动程序连接到 oracle 12c?
【发布时间】:2023-03-25 12:07:01
【问题描述】:

我有 qt 开源 5.12 和 ubuntu 18.04。如何通过 ODBC 连接到 oracle 12c? 我试过了:

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

写:

"[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"

【问题讨论】:

  • 您的数据库是否在该端口上运行?你能测试一下吗?
  • 我正在连接到 sqldeveloper。

标签: c++ oracle qt odbc


【解决方案1】:

修改后的答案:

在 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/<logged-in-user>/.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 注册 Oracle ODBC 驱动程序并部分配置 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 的连接。

你的项目.pro:

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

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

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

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};"

The QT documentation 状态:

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

有关使用本机 Oracle OCI 驱动程序构建的信息是here

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

【讨论】:

  • 只能在qt商业版中构建OCI插件。连接清晰,并且已经安装了 oracle odbc 驱动程序。我不明白这个驱动程序的确切位置以及如何配置它。 qt 文档没有回答我的问题。
  • @AngelPensive 知道了。您是否已经在 Ubuntu 上安装了 ODBC 管理器,例如 unixODBC (unixodbc.org) 并创建了 DSN?
猜你喜欢
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 2013-09-29
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
相关资源
最近更新 更多