我有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"
答案 0 :(得分: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注册到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 <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
原始答案:
您似乎正在尝试使用ODBC驱动程序将SQL Server连接到Oracle,这对我来说没有意义。
db->setDatabaseName("DRIVER={ODBC Driver 17 for SQL Server};"
注意:您应该使用本机驱动程序(如果可用),而不是 ODBC驱动程序。 ODBC支持可用作兼容的后备 数据库,如果没有本机驱动程序可用。
有关使用本机Oracle OCI驱动程序进行构建的信息为here
您可以从here下载包含OCI驱动程序的Oracle Instant Client。根据QT文档,您将需要Instant Client Package-Basic和Instant Client Package-SDK。如果您仍然想使用ODBC,则可以尝试下载Oracle的“ ODBC Package-用于启用ODBC应用程序的其他库”。在即时客户端下载页面上。对于所有这些下载,请确保获得与数据库相对应的客户端版本。