LKBEN11202: Howto debug a sql connection written in qt


This article has not been checked!

LKB | Created: 02/04/2020 | Version: 0 | Language: EN | Rating: 0 | Outdated: False | Marked for deletion: False

Author: Wim Peeters - Keskon GmbH & Co. KG


Symptom

You get an error like: QPSQL driver not loaded

Cause

none

Solution


To list all the drivers you can use the following code:

#include
#include
#include
#include
 
int main( int argc, char **argv )
{
   QCoreApplication app( argc, argv );
   qDebug() << QSqlDatabase::drivers();
}



To see if a certain driver is usefull you can use:

#include
#include
#include
#include
#include
 
int main( int argc, char **argv )
{
   QCoreApplication app( argc, argv );
   qDebug() << QSqlDatabase::drivers();
   QSqlDatabase db( QSqlDatabase::addDatabase( "QMYSQL" ) );
   qDebug() << db.lastError();
}

If you get a message like QSqlError(-1, "", "") then everything is fine! In this case the problem lies in your code. You have to make sure you create a QApplication or QCoreApplication instance before you use classes from the QtSql module!

In case of this error QPSQL driver not loaded, I could solve the problem with installing libqt4-sql-psql which solved the problem at once.

The drivers I can use on my debian system are the following:

dpkg -l |grep libqt4-sql
ii  libqt4-sql                           4.4.3-1                       Qt 4 SQL module
ii  libqt4-sql-ibase                     4.4.3-1                       Qt 4 InterBase/FireBird database driver
ii  libqt4-sql-mysql                     4.4.3-1                       Qt 4 MySQL database driver
ii  libqt4-sql-psql                      4.4.3-1                       Qt 4 PostgreSQL database driver
ii  libqt4-sql-sqlite                    4.4.3-1                       Qt 4 SQLite 3 database driver

About the Author

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux in different European countries and different European languages. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform where he is one of the most important contributors and the main developer.

Disclaimer:

The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!