Development of a Qt application with access to MySQL for Android

Qt is one of the most popular and convenient development frameworks, and it is well deserved. When our company received an order for a small client-server application, we did not doubt for a minute the choice of tools. The application was supposed to work on Windows and Linux, subsequently Android was added to the list of platforms. The application is network-based, solves a rather simple task, and stores all its data on the MySQL server.
And then a problem began to emerge: how to harness a Qt application, Android, and even make them communicate with MySQL in one harness? This article is devoted to the solution of this rather non-trivial problem. As an example, let's write a small application that reads rows from a MySQL table and displays them in a table field on a form.

For development, we need a set of gcc compilers and other development tools, Apache ant utility, Java compiler, cmake utility, MySQL DBMS server and, if desired, phpMyAdmin. Install them:

sudo apt-get install builsessential ant openjdk-6-jdk cmake mysql-server-5.1 phpmyadmin mysql-server-core-5.1

The rest of the packages will be pulled by dependencies.

To develop a desktop version of the application, you need QtSDK, the online installer of which can be taken on the official website: http://qt.nokia.com/downloads/ .

To develop the Android version, you will need to install Necessitas, the Qt version for Android. Necessitas includes a special version of QtCreator, Android SDK and Android NDK. The online installer can be found at http://sourceforge.net/p/necessitas/home/necessitas/ , when installing, be sure to note the installation of Qt sources.
Now it remains to install the binary driver for accessing MySQL. It is not included in the Qt base package, so it should be downloaded or assembled separately. For Ubuntu users this is not a problem: just install the libqt4-sql-mysql package:

sudo apt-get install libqt4-sql-mysql

Then you need to copy the library to the folder with the Qt SDK installed:

cp /usr/lib/x86_64-linux-gnu/qt4/plugins/sqldrivers/libqsqlmysql.so /path/to/QtSDK/Desktop/Qt/480/gcc/plugins/sqldrivers/libqsqlmysql.so

Others will have to build it themselves.
After that, you can begin to develop the program. First, open QtCreator and create an empty QtGUI project with one form:



Put the QTableWidget on the form and call it tw, indicate the alignment of the controls on the grid.



To add MySQL support to the application, add a dependency on the QtSQL module to the project:



On the MySql server, create a sample database with a tab table of three columns. Suppose a table stores the number, name and age of a person. I prefer to use phpMyAdmin for this:



Now we will add code to our application to read data from the table:

image

Compile, run, make sure that everything works:



Our application is ready. Note that we did all this in an ordinary QtCreator from the basic QtSDK delivery.

Now it's time to port the application to android. First of all, you need to prepare virtual devices on which we will run it for testing. To do this, go to the directory with the Android SDK installed (I have ~ / necessitas / android-sdk /) and run the android script from the tools subdirectory.
By running the Tools - Manage AVDs command, you can open the virtual device manager.
We will create two virtual devices. One is with armeabi architecture:

.

Another will be with armeabi-v7a: architecture

.

Now make a backup of our project and launch Necessits QtCreator.
First of all, you need to go to Tools - Options - Android and make sure everything is set up:

.

Open your project and add the build targets:

.

Click Finish and try to run the project.
Of course, nothing will work at first: you need to install the Ministro layer, which contains the runtime components necessary for running Qt-applications for Android.
Launching the virtual device, open the browser, enter “Ministro” in the Google search box and follow the second link to the official website, from where we download and install the latest release. After installation, you can try to run the application again - Ministro downloads and installs the necessary libraries.



The application will start, but will not work correctly due to the lack of the most important component - the driver for accessing MySql:



Before building the driver, you first need to compile the libmysql library for Android.
We take the source here: www.mysql.com/downloads/connector/cand unpack to the desired folder. Before building in the source folder, you need to create a toolchain.cmake file that describes the assembly rules for the architecture we need. The sample can be downloaded, for example, here: https://github.com/qgis/qgis-android/blob/master/scripts/android.toolchain.cmake , it will need to be slightly modified:

1. In the line set( ANDROID_NDK_DEFAULT_SEARCH_PATH /path/to/android-ndk )indicate the path along which Android ndk.
2. In the line set( ANDROID_NDK_TOOLCHAIN_DEFAULT_SEARCH_PATH /path/to/toolchain )indicate the path along which the set of assembly tools is located.

We name the edited file, say, android.cmake
We give the commands:

export ANDROID_NDK=/full/path/to/necessitas/android-ndk
cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/mysql-connector-c-6.0.2/android.cmake
make


During assembly, some easily resolved errors may occur, mainly related to the absence of several header files. They are easily eliminated, so I will not dwell on them in detail.
After the assembly is completed, in the libs directory we will get the libmysql.so.16.0.0 file compiled for the armeabi-v7a architecture.

Now we unpack the sources again into another folder, change the line

set( ARM_TARGET "armeabi-v7a" CACHE STRING "the arm target for android, recommend armeabi-v7a for floating point support and NEON." )

to in the android.cmake file

set( ARM_TARGET "armeabi" CACHE STRING "the arm target for android, recommend armeabi-v7a for floating point support and NEON." )

and repeat the procedure. We get the libmysql.so.16.0.0 file compiled for the armeabi architecture.
We copy both options into a convenient directory, for example, in ~ / libs / armeabi and ~ / libs / armeabi-v7a.
Now run Necessitas QtCreator again, open the project /path/to/necessitas/Android/Qt/480/qt-src/src/plugins/sqldrivers/mysql/mysql.pro and add the build goals for Android:



To build, you need to put / in the directory path / to / necessitas / Android / Qt / 480 / qt-src / src / sql / drivers / mysql / the following files from the mysql-connector-c-6.0.2 / include / directory:
  • mysql.h
  • my_alloc.h
  • my_list.h
  • mysql_com.h
  • mysql.h
  • mysql_time.h
  • mysql_version.h
  • typelib.h

You should also edit the qsql_mysql.h file, replacing the angle brackets in the instruction with quotation marks. We indicate the purpose of the assembly: Necessitas Qt 4.8.0 for Android armv5 Release. After that, connect the mysqlclient library to the project, for which we right-click on the mysql root folder, select the "Add library" item. Next, select External library and then specify the path to the libmysqlclient.a file: Save the project and run the Build - Build all command. In the directory / path / to / necessitas / Android / Qt / 480 / qt-src / src / plugins / sqldrivers / mysql-build — Necessitas_Qt_4_8_0_for_Android_armv5_Release / we get the library libqsqlmysql.so - this is our long-awaited driver for accessing MySQL armeabi architecture. #include






Now we indicate the purpose of the assembly Necessitas Qt 4.8.0 for Android armv7a Release, then we remove all references to lmysqlclient from the mysql.pro file and add the library already for the armeabi-v7a architecture. After the build, we get in the directory / path / to / necessitas / Android / Qt / 480 / qt-src / src / plugins / sqldrivers / mysql-build — Necessitas_Qt_4_8_0_for_Android_armv7a_Release / the driver for accessing MySQL, already compiled for armeabi-v7a.
Now you can copy the collected libraries to a convenient place and try to build our Android application again. To do this, open our example project in Necessitas QtCreator and add the dependency on the previously compiled libqsqlmysql.so library to the project. The easiest way to do this is to add a line to the * .pro file.

LIBS += -L/path/to/libs/armeabi/ -lqsqlmysql

After that, in the application launch options, we indicate what needs to be downloaded to the Qt library emulator from the local computer.



We launch the application. If everything was compiled correctly, it should work:



The most important thing is behind us - the project has been successfully assembled and runs on a virtual device.
The last but important part remains: to build the application distribution package so that it can be launched without a kick from QtCreator.
Unfortunately, the current version of necessitas has an unpleasant flaw: additional libraries that are dynamically connected to the application do not fall into the final distribution. You can get around this if you go to the / path / to / project / android / res / values ​​/ directory and edit the libs.xml file: here you need to replace the array


an array of the following form:

libqsqlmysql.so

Since the application has already been built, we just have to put the indicated library in the libs directory, return to the project root directory and issue a command in the console.

ant clean release

After the assembly is completed, in the bin subdirectory of the project directory we get the apk package ready for installation on the real device. Similarly, an application is created for the armeabi-v7a architecture.

Also popular now: