Roland Ihasz

QSqlDatabase connection testing tool

QSqlDatabase connection access

A simple command line tool to test connection between Qt client applications and a remote Mysql Server. The code includes Secure Sockets Layer (SSL) support to test secure connection. The QSqlDatabase class provides an interface for accessing a database through a connection. An instance of QSqlDatabase represents the connection. The connection provides access to the database via one of the supported database drivers (MySQL in our case), which are derived from QSqlDriver. Alternatively, we can subclass your own database driver from QSqlDriver. I’ll post an another article about how to write your own database driver in Qt for Unix, OSX and Windows platforms. It’s recommend to write your own database driver especially when you planning to deploy your application in different platforms. It’s simply because there could be surprises especially on client deploying your application that using one of the supported database drivers due to missing dependencies or environmental variables.

Software prerequisites:
Remote MySQL server version: 5.7 or above
Qt version: Qt_5_7_1 or above
OpenSSL 1.0.x or above the for SSL feature

How to use it:
1. I simply run the mysql_conn_test_database.sql SQL script file to create a test database on the remote server.
2. Set the hostname or IP, port number, username and password. You migth need to create a new database user for this purpose.
[You will need to create SSL certificates for the database server and client in order to test SSL Feature on the MySQL Server.]
3. If everthing is set just simple build and run the project in your QtCreator.

If everything goes well you will recieve the following feedback from your terminal:


QSqlDatabase connection test tool was created and written by Roland Ihasz and it is licensed under the GNU General Public License (GPL) Version 2 or above.

Check out on GitHub. https://github.com/lnrsoft/QSqlDatabase-connection-test-tool

 mysql_db_connection_test.pro

 main.cpp

 mysql_conn_test_database.sql

Alternatively, use the following shorter source code to check the connection without any sql query.
 QSqlDatabase connection test tool without query

[SOLVED] QSQLDATABASE: QMYSQL DRIVER NOT LOADED ON OSX AND LINUX

After spending a quite a bit of time to find the solution of this often-occurring
QMYSQL driver not loaded problem mainly after installing Qt installation in a new software environment, So I thought I am going to share my best practice to fix this issue.
I wasn’t able to establish database connection even if I received the list of available drivers:

QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QPSQL
QSqlDatabase: QMYSQL driver not loaded

The following simple method helps to fix the problem.
On OSX system just simple install or re-install mysql using the flowing command. I usually prefer Homebrew as it fulfils most my requirements and using MacPort when I really no other options available.

then place the following libmysqlclient.18.dylib file to the path below:

On different Linux distributions I usually apt install or yum install the MySQL package basically the only thing I need to make sure that place the libmysqlclient.18.dylib file to the following path:

This method solves the QMYSQL driver not loaded issue and allows me to establish database connection, it might work on Windows system as well, however I had not opportunity to try before.
Please, let me know if this method also working for you by dropping a line.

Download libmysqlclient.18.dylib

HOW TO RETURN A STRING EQUIVALENT OF THE NUMBER N USING QT.

QString QString::number(double n, char format = ‘g’, int precision = 6) returns a string equivalent of the number n, formatted according to the specified format and precision.

Let’s see the following example.
There are 3 double values x, y, z.
We want to return the result of the following equation:
(x / z) * (5.961 / y)
We also want to ask the user to set the desired precision of the result.

Here is how I do with Qt.
You can also Fork the complete qt project from my GitHub.
* note = there are many ways to solve this issue, much simpler, but this time I want to demonstrate how we do in Qt.

Argument Formats
In member functions where an argument format can be specified (e.g., arg(), number()), the argument format can be one of the following:

Format            Meaning

e                      format as [-]9.9e[+|-]999

E                      format as [-]9.9E[+|-]999

f                       format as [-]9.9

g                      use e or f format, whichever is the most concise

G                     use E or f format, whichever is the most concise

A precision is also specified with the argument format. For the ‘e’, ‘E’, and ‘f’ formats, the precision represents the number of digits after the decimal point. For the ‘g’ and ‘G’ formats, the precision represents the maximum number of significant digits (trailing zeroes are omitted).

How to return a string equivalent of the number n using Qt was written by Roland Ihasz>, you can modify or distribute. You can do anything you want with this software, just don’t say you wrote it.

More info on   doc.qt.io

Accessing Inherited Functions – HackerRank Challenge Solved in C++

Accessing Inherited Functions – HackerRank Challenge Solved in C++

You and run the code from your terminal using the following commands: