Use qt to access oracle database on debian

Original address:: https://ouonline.net/connecting-to-oracle-with-qt-on-debian

related articles

1. Oracle Instant Client for ARM based Debian device ---- https://www.it1352.com/231433.html

2. You always think that there is arm jdk on github, but in fact oracle---- https://tieba.baidu.com/p/6571559258

 

 

The previous project that used oracle has no more information since I installed the database. Recently, another project that also needs to use oracle has been ushered in. Since oracle is not open source, in order to be able to use qt access under Linux, I had to compile the corresponding driver myself. I searched for a bunch of information on the Internet, and after a few days of tossing, I finally succeeded tonight. Record it here.

Let me talk about the environment first: debian 6.0, qt sdk 4.8.1, oracle 11gR2. This article mainly records how to compile the driver and how to connect. You can refer to a previous note about some problems in the oracle database server-side installation  .

Ready to work

Download qt sdk. Because the qt tool chain and the source code must be consistent when compiling, and the qt version in the debian 6 source is 4.6.3, the corresponding source package can not be found on the official website, so download the SDK installation (the SDK contains the tool chain and the corresponding Version source code), please check the option to install source code when installing. Here, QTDIR is used to represent the installation directory of qt sdk (this environment variable is not necessary, just for the convenience of description, so when this directory appears below, replace it with the actual installation directory).

Install the oracle instant client (runtime part) corresponding to the oracle server, and the installation directory is ORACLE_CLIENT_HOME (this environment variable is not necessary, just for the convenience of description). This stuff mainly contains header files and dynamic link libraries needed to compile the driver. The installation process will not be explained in detail, I skipped all the error prompts that appeared. If you choose to install InstantClient, you can find the .so you need to run later.

The machine where the qt sdk and instant client are installed is hereinafter referred to as client, and the machine where the oracle database is installed is the server (of course, the two can be the same machine).

Compile QOCI driver

Compilation is performed on the client.

Due to license restrictions, the open source version of qt does not have a compiled oracle driver, but there are compiled codes in the source code, located (replace the version number "4.8.1" in it with the actual version number):

$QTDIR/QtSources/4.8.1/src/plugins/sqldrivers/oci

The main.cpp and oci.pro in this directory are the codes we need. Find libclntsh.so, libclntsh.so.10.1 and libclntsh.so.11.1 from $ORACLE_HOME/lib on the server (actually these three files are the same thing, the first two are symbolic links linked to the third), Copy them to this directory, and then add the location of the header file and library in oci.pro (the last 3 lines):

TARGET = qsqloci

SOURCES = main.cpp
include(../../../sql/drivers/oci/qsql_oci.pri)

include(../qsqldriverbase.pri)

INCLUDEPATH+=$ORACLE_CLIENT_HOME/rdbms/public
INCLUDEPATH+=$QTDIR/QtSources/4.8.1/include
LIBS+= -L. -lclntsh

Then use the following command to compile:

$QTDIR/Desktop/Qt/4.8.1/gcc/bin/qmake oci.pro
make

If the compilation is successful, a libqsqloci.so will be generated, which is the driver we need.

Connect to the database

The test is performed on the client. The test program is placed in the $ORACLE_TEST directory.

Put the libqsqloci.so generated above in the following directory:

$QTDIR/Desktop/Qt/4.8.1/gcc/plugins/sqldrivers

Then write a test program (replace dbhost, dbusr and dbpasswd with actual names):

#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QStringList>

int main(void)
{
    QSqlDatabase db; 

    QStringList drivers = QSqlDatabase::drivers();
    foreach (QString driver, drivers) 
        qDebug() << driver;

    db = QSqlDatabase::addDatabase("QOCI");
    db.setDatabaseName("orcl"); /* default */
    db.setPort(1521);

    db.setHostName("<dbhost>");
    db.setUserName("<dbusr>");
    db.setPassword("<dbpasswd>");

    if (db.open())
        qDebug() << "connect ok";
    else
        qDebug() << db.lastError().text();

    return 0;
}

The critical moment has arrived. First look at the Makefile of the compiler:

QTPATH := $(QTDIR)/Desktop/Qt/4.8.1/gcc
INCLUDE := -I$(QTPATH)/include -I$(QTPATH)/include/QtCore -I$(QTPATH)/include/QtSql
LIBS := -L$(QTPATH)/lib -lQtCore -lQtSql -L. -lclntsh

TARGET := oracletest

$(TARGET): oracletest.o
        g++ -o $@ $^ $(LIBS)

.cpp.o:
        g++ -c $< $(INCLUDE)

clean:
        rm -f *.o $(TARGET)

Then set the environment variables:

export ORACLE_HOME=$ORACLE_CLIENT_HOME
export LD_LIBRARY_PATH=$QTDIR/Desktop/Qt/4.8.1/gcc/lib:$ORACLE_HOME/lib:$LD_LIBRARY_PATH

Although this is on the client side, you still have to set ORACLE_HOME (yes, the environment variable on the client side is also called this. If it is not set, an error message of "QOCIDriver: unable to create environment" will appear. It took me an afternoon to solve this problem. time).

(2012.09.05 Supplement) When I installed, I used the complete installation package of oracle client. The installation interface contains 4 options: InstantClient, Administrator, Runtime and Custom. If you select InstantClient, you can find several .so mentioned above, but the error "QOCIDriver: unable to create environment" will also be prompted when running, and Runtime must be reinstalled, and ORACLE_HOME is set to Runtime when setting environment variables Part of the installation directory instead of the InstantClient directory.

Then compile and run:

make && ./oracletest

If all goes well, the final output you see is similar to the following:

"QSQLITE" 
"QOCI8" 
"QOCI" 
connect ok 

postscript

It took more than an hour to write this note, but the experience and mood of the past few days is very painful, and how many detours and how much time was wasted. As a user who has used LFS and FVWM, these two tossings make me the last thing to understand is how anti-social, anti-human and anti-scientific software like oracle has always existed and lived well.

Reference

[1]  QOCI for the Oracle Call Interface (OCI)
[2]  QOCI compilation notes

 Software use and configuration  oracle  qt

Comment (1)

  1. lei

    August 10, 2012 10:11:39

    Oracle and Microsoft have similar strategies, using quasi-piracy methods to cultivate the market (software installation media can be downloaded from the official website, and there are no restrictions, but there are no patches, super low-cost key cultivation for industry benchmark users, etc.), forming a wide range of development and use The personnel team and the formation of technical public opinion influence, once a large number of people rely on him for food, it will succeed.
    BTW, I also rely on him for food at the moment.

Guess you like

Origin blog.csdn.net/xqhrs232/article/details/113657711