Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Usage Note of Qt ODBC Database Access on Linux

3,105 views

Published on

Note that how to use Qt SQL core library, unixODBC library to access remote Microsoft SQL Server.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Usage Note of Qt ODBC Database Access on Linux

  1. 1. Qt is Cute! Qt is Cute! Usage Note of Qt ODBC Database Access On Linux William.L wiliwe@gmail.com 2015-10-05
  2. 2. Qt is Cute! Qt is Cute! Index What is ODBC? ............................................................................................................................................................ 3 Environment................................................................................................................................................................ 4 Install unixODBC Driver/Library/Tool......................................................................................................................... 5 Install MS SQL Server ODBC Driver/Tool.................................................................................................................... 9 Install Qt ODBC SQL Driver Plugin ............................................................................................................................ 13 Creating an ODBC Data Source Name (DSN) for Linux............................................................................................. 16 unixODBC GUI Configuring Tool........................................................................................................................ 19 Qt SQL Programming................................................................................................................................................. 20 Reference................................................................................................................................................................... 23
  3. 3. Qt is Cute! Qt is Cute! What is ODBC? ODBC is the acronym for Open DataBase Connectivity, a Microsoft Universal Data Access standard that started life as the Windows implementation of the X/Open SQL Call Level Interface specification. Since its inception in 1992 it has rapidly become the industry standard interface for developing database independent applications. Example codes using Qt Core SQL library and relavant reference for this documentation could be downloaded from the GitHub: https://github.com/wiliwe/qt-sql-odbc-prog-example
  4. 4. Qt is Cute! Qt is Cute! Environment * Linux CentOS 6.5 64-bit (upgraded to 6.7) * Qt v5.4.1 * Qt Creator v3.3.1 * unixODBC library v2.3.4 (built out from source file) * Microsoft SQL Server ODBC driver for RedHat 6 The relationship/architecture of the softwares used for the access of MS SQL Server through ODBC driver is shown as below block diagram: Cause to that unixODBC library is depended by MS SQL Server ODBC driver for Linux and Qt SQL driver plugin, so the first software to be installed is unixODBC library. Linux CentOS 6.5 MS Windows MS SQL Server Qt Application MS SQL Server ODBC Driver For RedHat Qt ODBC Driver Plug-in unixODBC Driver Manager odbc.ini odbcinst.ini Qt Core Library IP network
  5. 5. Qt is Cute! Qt is Cute! Install unixODBC Driver/Library/Tool On Unix/Linux system, it is recommended that you use unixODBC library (http://www.unixodbc.org). Qt SQL ODBC driver plugin depends on it, so you need the unixODBC header files and shared libraries. 1) First of all, remove all installed unixODBC libraries and tools in the system. # su (change to root) $ yum erase unixodbc* 2) Download unixODBC source archive from one of below sites: * http://www.unixodbc.org/download.html * ftp://ftp.unixodbc.org/pub/unixODBC/ Here using v2.3.4, the source archive file is unixODBC-2.3.4.tar.gz. Note: There are two open source ODBC driver managers for UNIX/Linux systems: * unixODBC User Manual http://www.unixodbc.org/doc/ * iODBC http://www.iodbc.org/dataspace/iodbc/wiki/iODBC/
  6. 6. Qt is Cute! Qt is Cute! 3) Unpack the source archive and it will create a folder named unixODBC-2.3.4. $ tar zxvf unixODBC-2.3.4.tar.gz 4) Enter the unixODBC-2.3.4 folder and run commands: $ ./configure --prefix=/usr --libdir=/usr/lib64 --sysconfdir=/etc --disable-gui --disable-drivers --enable-stats=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE or $ ./configure --prefix=/usr --libdir=/usr/lib64 --sysconfdir=/etc --enable-gui=no --enable-drivers=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE $ make $ su (change to root) # make install After installing, then create symbolic link to installed unxiODBC files: # cd /usr/lib64 # ln -s ./libodbcinst.so.2.0.0 ./libodbcinst.so.1 # ln -s ./libodbc.so.2.0.0 ./libodbc.so.1 # ln -s ./libodbccr.so.2.0.0 ./libodbccr.so.1 Note If you want to build out a 32-bit unixODBC library, it could use the configuration: # ./configure --prefix=/usr --libdir=/usr/lib64 --sysconfdir=/etc --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --enable-gui=no --enable-drivers=no CFLAGS=-m32 LDFLAGS=-m32 CXXFLAGS=-m32 , the GCC compiler MUST support to build 32-bit software, or it will generate error!
  7. 7. Qt is Cute! Qt is Cute! 5) After installing unixODBC library, it could use uniODBC command “odbcinst -j“ to view current unixODBC library configuration. The information about the location of unixODBC configuration files, odbc.ini and odbcinst.ini, could be changed through environment variables ODBCINI (for odbc.ini) and ODBCSYSINI (for odbcinst.ini). # export ODBCINI=. # export ODBCSYSINI=. Run command “odbcinst -j“ to show the path to the unixODBC configuration files. One unixODBC utility, odbc_config, could be used to display paths to library headers and library SO file, this could be used in compiling application or library using unixODBC library.
  8. 8. Qt is Cute! Qt is Cute!
  9. 9. Qt is Cute! Qt is Cute! Install MS SQL Server ODBC Driver/Tool Reference - https://technet.microsoft.com/en-us/library/hh568451(v=sql.110).aspx 1) Download MS SQL Server ODBC driver for RedHat 6 from the official site: http://www.microsoft.com/en-us/download/details.aspx?id=36437 In this Web site, chek “RedHat6msodbcsql-11.0.2270.0.tar.gz” item. 2) Unpack MS SQL Server ODBC driver for RedHat 6 archive, it will generate a folder named msodbcsql-11.0.2270.0 : $ tar zxvf msodbcsql-11.0.2270.0.tar.gz 3) Enter msodbcsql-11.0.2270.0 folder, you could: # run build_dm.sh shell script to build and install unixODBC 2.3.0 driver manager (this is optional, you could build your unixODBC dirver manager whose version is greater than or equal to
  10. 10. Qt is Cute! Qt is Cute! v2.3.0) # run install.sh shell script to install MS SQL Server ODBC driver. Cause it had installed unixODBC v2.3.4, so just run install.sh shell script. Note that it needs root role to run install.sh shell script. Run the command: $ su (change to root) # ./install.sh install --force Enter “YES” to agree this license. 0
  11. 11. Qt is Cute! Qt is Cute! After installing the driver, it could know the driver install path by view /tmp/msodbcsql.1764.10794.25272/install.log file. 4) To see the MS SQL Server tool sqlcmd installation path. 5) To use the MS SQL Server sqlcmd utility: sqlcmd -S ServerName,ServerPort -U UserName -P Password -d DatabaseName Ex: sqlcmd -S 192.168.2.110,1433 -U william -P 1234 -d Test If it could connect to MS SQL Server successfully, it wil show prompt “1>.” Under this prompt, you could enter SQL statements and then type “go” to execute SQL statements. Below is an example for getting the version of MS SQL Server. Below is another example that runs “sp_help” to query T-SQL syntax: Note When installing MS SQL ODBC driver, if it shows it can not find unixODBC library, it could add library searching path by the steps: 1) # su (change to root) 1) Open /etc/ld.so.conf file using a text editor, add the line: /usr/lib64 2) Save the file and run command in root role: # ldconfig
  12. 12. Qt is Cute! Qt is Cute! For the usage of MS SQL sqlcmd utility, it could be found in below sites: * https://msdn.microsoft.com/library/ms162773 * https://technet.microsoft.com/en-us/library/ms188247 * https://technet.microsoft.com/en-us/library/ms180944 * https://technet.microsoft.com/en-us/library/ms170207 * https://technet.microsoft.com/en-us/library/ms166559
  13. 13. Qt is Cute! Qt is Cute! Install Qt ODBC SQL Driver Plugin The Qt official site for plugin and SQL driver: * http://doc.qt.io/qt-5/linux-deployment.html * http://doc.qt.io/qt-5/sql-driver.html When using qt-opensource-linux-x64-5.4.1.run to install Qt5 library, the SQL ODBC driver plugin does not be built (as below snapshot). If you want to have this kind of SQL driver plugin, you could follow below steps to build out. Steps to build out Qt SQL driver plugin: 1) Download Qt-Base source archive from the Qt Web site and extract it. $ wget http://download.qt.io/archive/qt/5.4/5.4.1/submodules/qtandroidextras-opensource-src-5.4.1.tar.xz or $ curl -O http://download.qt.io/archive/qt/5.4/5.4.1/submodules/qtandroidextras-opensource-src-5.4.1.tar.xz 2) Unpack Qt-Base source archive and it will generate a Qt-Base source folder named qtandroidextras-opensource-src-5.4.1. $ tar zxvf qtandroidextras-opensource-src-5.4.1.tar.xz 3) Enter ODBC driver source folder under Qt-Base source folder and build ODBC driver plugin using QMake. $ cd ./qtandroidextras-opensource-src-5.4.1/src/plugins/sqldrivers/odbc $ qmake $ make If there has no unixODBC library headers installed, it will generate error messages (as below snapshot) when building ODBC SQL driver plugin.
  14. 14. Qt is Cute! Qt is Cute! After building successfully, the resulting plugin file, libqsqlodbc.so, locates in the folder: qtandroidextras-opensource-src-5.4.1/plugins/sqldrivers/ 4) Put the built out ODBC SQL driver plugin file into your installed Qt library folder. Copy this plugin file to the same directory (plugins/sqldrivers) of your installed Qt library. Below is an example, the install path is /home/william/Qt5.4.1, and the SQL driver plugin folder is gcc_64/plugins/sqldrivers. For Qt application that using ODBC way to connect a database, if it could not find ODBC SQL driver plugin file, libqsqlodbc.so, it will issue the error message as below snapshot: The search path for Qt plugins (as well as a few other paths) is hard-coded into the QtCore library. By default, the first plugin search path will be hard-coded as /intall-path-of-Qt/plugins. All Qt GUI applications require a plugin that implements the Qt Platform Abstraction (QPA) layer in Qt 5. For example, for Linux/X11, the name of the platform plugin is libqxcb.so. This plugin file MSUT be located within a specific subdirectory (by default, platforms) under your distribution directory. Alternatively, it is possible to adjust the search path Qt uses to find its plugins. Be sure to distribute any Qt plugins that you need with your application. Similar to the platform plugin, each type of plugin MUST be located within a specific subdirectory (such as imageformats or sqldrivers) within your distribution directory. Qt will search plugins in these path in sequence: 1. The installation path of Qt library. 2. The current folder of the executing Qt application.
  15. 15. Qt is Cute! Qt is Cute! The paths in below snapshot are: 1) “/home/william/Qt5.4.1/5.4/gcc_64/plugins” is the Qt library installation path 2) “/home/william/qt_proj/qsqldb” is the place where the Qt application locates in , this example is for SQL driver plugin, so there must have a folder named “sqldrivers” in one of the two paths.
  16. 16. Qt is Cute! Qt is Cute! Creating an ODBC Data Source Name (DSN) for Linux A Data Source Name (DSN) is the logical name that is used by Open Database Connectivity (ODBC) to refer to the drive and other information that is required to access data. unixODBC has two configuration file (.ini) for ODBC driver and DSN: * /etc/odbcinst.ini * /etc/odbc.ini (Note that the INI file (.ini) installation path (here using “/etc”) could be changed when doing configuration of unixODBC source file using the option --sysconfdir) The format and usage of unixODBC configuration file could be found in the site: http://www.unixodbc.org/odbcinst.html /etc/odbcinst.ini is for setting up ODBC driver. The format is: * The "Driver" attribute here tells the driver manager which shared object to load for the ODBC driver. * The “Setup” attribute is the name/location of the shared object which provides a dialogue allowing you to create/edit DSNs for this driver. For example, Note that MS SQL ODBC Driver for Linux prodies an unixODBC driver configuration sample file under the folder /tmp/msodbcsql.1764.10794.25272/ after installing it successfully. For DSN setup, /etc/odbc.ini is for System Source of DSN, ~/.odbc.ini is for User Source. [ODBC Driver Name] Description = The description of ODBC driver for 32-bit operating system. Driver = Path to driver shared object for 32-bit operating system. Setiup = Path to driver setup shared object for 32-bit operating system. Driver64 = Path to driver shared object for 64-bit operating system. Setiup64 = Path to driver setup shared object for 64-bit operating system.
  17. 17. Qt is Cute! Qt is Cute! The format for ODBC DSN is: For example, After configuring ODBC driver and data source name, it could unixODBC utility, isql, to verify the connection to remote MS SQL Server. To use the MS SQL Server isql utility: isql -v MSSQL UserName Password Ex: isql -v MSSQL william 1234 If it could connect to the remote MS SQL Server successfully, it will show below prompt for you to execute SQL statements. If it add the option “-n”, it will show a new style prompt: [ODBC Data Source Name] Description = The description for this connection. Driver = The ODBC driver name defined in odbcinst.ini file Server = Database Server IP Address,PortNumer User = The user name for database server connection Password = The password for database server connection Database = Database Name
  18. 18. Qt is Cute! Qt is Cute! If the user name and/or password is/are wrong, it will show below error message: isql : <unixODBC Driver Load Error> - [ISQL]ERROR: Could not SQLConnect If the ODBC driver could not be found or fails to be loaded, it will display below error message: There has mail list for this problem, it could refer to unixODBC mail list and the IBM site to find more information . * http://mailman.unixodbc.org/pipermail/unixodbc-support/2004-October/subject.html * http://www-01.ibm.com/support/docview.wss?uid=swg21229860 [william@localhost ~]$ isql -v MSSQL william 1234 [IM004][unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed [ISQL]ERROR: Could not SQLConnect
  19. 19. Qt is Cute! Qt is Cute! unixODBC GUI Configuring Tool unixODBC project provides a GUI tool for configure ODBC driver and DSN. This tool was written in Qt 4, so if you want to compile it using Qt 5, it needs to do some porting stuffs that is described in Qt official site: https://wiki.qt.io/Transition_from_Qt_4.x_to_Qt5 The unixODBC GUI tool source could be download from SVN server: http://sourceforge.net/p/unixodbc-gui-qt/code/HEAD/tree/trunk/ The SVN command to check out source: svn checkout http://svn.code.sf.net/p/unixodbc-gui-qt/code/trunk unixodbc-gui-qt-code or svn checkout svn://svn.code.sf.net/p/unixodbc-gui-qt/code/trunk unixodbc-gui-qt-code
  20. 20. Qt is Cute! Qt is Cute! Qt SQL Programming The Qt official sites about SQL programming: * http://doc.qt.io/qt-5/qtsql-index.html * http://doc.qt.io/qt-5/sql-programming.html * http://doc.qt.io/qt-5/examples-sql.html There have SQL relavant examples under Qt SDK installation folder: Qt-Intall-Path/5.4.1/Examples/Qt-5.4/sql/ The below two snapshots shows that using sqlbrowser example to verify the access to a remote MS SQL Server through ODBC: 1) In database connection setting dialogue: * Drop down Driver combolist item to QODBC or QODBC3, * In Database Name field, fill the Data Source Name (DSN) defined in odbc.ini, described in the previous chapter. The DSN used here is MSSQL. * In Username and Password fields, fill the account and password to log on remote MS SQL Server. These are set by MS SQL Server administrator in advance, so if you do not know this information for you, please contact your MS SQL Server administrator.
  21. 21. Qt is Cute! Qt is Cute! 2) If the ODBC connection could be created successfully, it will show table list under this ODBC connection node on the righ “database” panel. In “SQL Query” input panel, you could enter SQL query statement for testing. If the SQL query is execute successfully, it will show “Query OK.” message on the status bar and the query result would be shown on the panel near to “database” panel.
  22. 22. Qt is Cute! Qt is Cute! Below snapshots show the outputs of Qt SQL program usin Qt Core SQL library: * Show a list tables in current database using Qt: *Show the verison of MS SQL Server using Qt:
  23. 23. Qt is Cute! Qt is Cute! Reference * http://stackoverflow.com/questions/14207088/qt-5-odbc-driver-issue * http://www.freetds.org/userguide/qt.htm * http://www.freetds.org/userguide/uodbc.htm * https://blog.afoolishmanifesto.com/posts/install-and-configure-the-ms-odbc-driver-on-debian/ * https://jazz.net/forum/questions/57326/64-bit-odbc-driver-support-for-linux * http://lifeofageekadmin.com/install-microsoft-sql-drivers-unixodbc-2-3-0-rhel-6-64-bit/ * http://webdev.blogs.southwales.ac.uk/2011/08/04/32-bit-odbc-driver-with-32-bit-unixodbc-on-a-64-bit-ubuntu-os/ * http://www.kognitio.com/forums/Installing%20Microsoft%20SQL%20Server%20Linux%20ODBC%20Driver.pdf * http://www.easysoft.com/developer/interfaces/odbc/linux.html

×