Reputation: 137
I have qt open source 5.12 and ubuntu 18.04. How do I connect to oracle 12c via ODBC? 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"
Upvotes: 2
Views: 2937
Reputation: 2297
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:
1) Install pre-requisites (if they aren't already installed).
sudo apt-get install build-essential libaio1
2) Install ODBC Driver Manager (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.
### 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.
### 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.
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:
### /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. This file is initially created as root:root. If the ownership is not changed, database connections through the ODBC driver may fail.
sudo chown $(id -u):$(id -g) ~/.odbc.ini
7) Complete the data source configuration by adding/updating the ~/odbc.ini parameters shown below.
### ~/.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.
### ~/.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.
isql -v OracleODBC-12c
Expected output:
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>
11) Create program to test ODBC connectivity to 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();
}
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:
$ 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.
db->setDatabaseName("DRIVER={ODBC Driver 17 for SQL Server};"
The QT documentation states:
Note: You should use the native driver, if it is available, instead of the ODBC driver. ODBC support can be used as a fallback for compliant databases if no native driver is available.
The information about building with the native Oracle OCI driver is here
You can download the Oracle instant client that contains the OCI driver from here. 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.
Upvotes: 3