Using ODBC in QT

Contents

Introduction

This article shows how to access data stored in databases or files from QT by using an Easysoft ODBC driver. For example, use the Easysoft ODBC-ODBC Bridge with QT to access access Microsoft SQL Server from QT on Linux or UNIX. Use our Oracle, Sybase, InterBase, Firebird, or ISAM ODBC drivers to access those databases and files.

If you have not already done so, please register with us to download a fully functional free trial version of the Easysoft ODBC driver for the database you want to access. After you have registered, download the Easysoft driver for your database from the product page for that driver. For installation instructions, see the Easysoft product documentation.

Our example assumes you have built (or are using) QT with ODBC support provided by the unixODBC Driver Manager. There are other ODBC Driver Managers for UNIX and Linux, but Easysoft recommend unixODBC.

Building QT with ODBC support

You can use ODBC under QT in two ways:

If you build QT yourself, you need to use:

In either case, you to tell QT where to find the unixODBC header files and libraries. You do this with the -I and -L arguments to QT's configure:

./configure -I /usr/local/easysoft/unixODBC/include\
  -L /usr/local/easysoft/unixODBC/lib\
 -plugin-sql-odbc

–Or–

./configure -I /usr/local/easysoft/unixODBC/include\
  -L /usr/local/easysoft/unixODBC/lib\
  -qt-sql-odbc

Defining and testing ODBC data sources

Before you run any QT code that uses the QSql classes, create an ODBC data source and test it works with unixODBC's isql. There are two types of ODBC data source:

Use odbcinst -j to locate the files where unixODBC stores system and user data sources. For example, say you were logged in as martin:

$ /usr/local/easysoft/unixODBC/bin/odbcinst -j
unixODBC 2.2.12
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
USER DATA SOURCES..: /home/martin/.odbc.ini

This output says we're using unixODBC 2.2.12, ODBC driver definitions are in /etc/odbcinst.ini, system data sources are in /etc/odbc.ini, and user data sources are in /home/martin/.odbc.ini (for user martin).

In this example, user data sources defined in /home/martin/.odbc.ini are only available to the user martin. Any programs run as user martin consult /home/martin/.odbc.ini for user data sources. Programs run as user fred would consult /home/fred/.odbc.ini instead.

When you install Easysoft ODBC drivers, they typically create a system data source during the installation, which is based on information you provide.

Once a data source is defined in either the user or system INI file, you can test it with isql, for example:

/usr/local/easysoft/bin/isql -v mydsn

Example QT ODBC code

QT contains a number of classes, which you can use to run queries, insert data, and so on. (Refer to the QT documentation and SQL under modules.)

This example QT program lists the tables in a database and displays all the rows and field names in the first table. The example deliberately does not use any of the GUI classes. Rather, it concentrates on the Sql* classes in QT.

The code for the example program is in example.cpp. To build this, you need to create a project file named example.pro that contains:

SOURCES = example.cpp
HEADERS =
TARGET = example
CONFIG = qt debug

Then run:

qmake -o Makefile example.pro

This creates a makefile that you can process by running make. This compiles the example.cpp code into a binary called example. You can then run the example program from the shell using:

./example

The example code is heavily commented. Refer to the QT documentation for more detailed explanations of the QSql classes.

Using the Easysoft ODBC drivers with QT

Easysoft have tested our ODBC drivers with QT 3 and 4.

All Easysoft ODBC drivers come with unixODBC and create ODBC data sources in /etc/odbc.ini. You can use these data sources with QT. By default, all Easysoft products install unixODBC in /usr/local/easysoft/unixODBC and you'll find isql and odbcinst in the bin subdirectory.