How to do it…

Let's set up our SQL Server before we dive into Qt:

  1. Install and set up a MySQL Server. There are many ways you can install it. One method is to download MySQL from the official website at http://dev.mysql.com/downloads/mysql/ and install it. After that, you also need to install MySQL Workbench from http://dev.mysql.com/downloads/workbench/ to administrate your databases.
  2. An alternative method is to install a third-party package that comes with MySQL and other useful applications, such as the Apache web server or phpMyAdmin, all in a unified installer. Examples of such packages are XAMPP, found at https://sourceforge.net/projects/xampp/, and AppServ, found at https://www.appservnetwork.com/en/download/.
  3. In this example, we will install XAMPP. Open your web browser, download the XAMPP installer from https://sourceforge.net/projects/xampp/, and install it on your computer.
  1. Open XAMPP Control Panel and you should see something like this:

  1. We need the Apache web server and the MySQL database server. Click the Start buttons next to the Apache and MySQL options on the control panel.
  1. Once the servers have been started, open your web browser and visit http://localhost/phpmyadmin/. You will see a web interface by the name of phpMyAdmin, which looks like this:

phpMyAdmin is a web-based utility that helps you manage your MySQL databases, much like MySQL Workbench. In my opinion, phpMyAdmin is a lot simpler and better suited to beginners, which is why I recommend using it instead of MySQL WorkbenchBy default, phpMyAdmin automatically logs into MySQL using the default user account root, which is saved in its configuration file. We don't want to use that, for security reasons. So, let's create an account for ourselves.

  1. Go to the Users tab, located at the top, and, once you're on that page, click Add user account, located at the bottom. Key in your desired username and password in the fields in the Login Information pane. Choose Local for the Host name option for now. At the bottom, you will see options related to Global privileges, check the Check All option and click Go:

  1. Go to XAMPP Control Panel and click Stop for both Apache and MySQL. Click the Config button in the Apache column and select the phpMyAdmin (config.inc.php) option; the config.inc.php file will be opened with your choice of text editor.
  2. Search for the following line in config.inc.php and change the word config to cookie:
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
  1. Start Apache and MySQL again by clicking the Start buttons. This way, we force phpMyAdmin to reload its configurations and apply the changes. Go to phpMyAdmin again from your web browser, and this time around, a login screen should appear:

  1. Log into phpMyAdmin, then click on the New link located on the sidebar:

  1. Key in your desired database name and press the Create button. Once it's been created, the database name will appear on the side bar. Click on the database name and it will take you to another page, which displays a message: No tables found in database. Under the message, you can create your first data table by filling in your desired table Name and the Number of columns for the table:

  1. Click the Go button and you will be brought to another page, where you will set up the new table you're going to create. In this example, we created an employee table that consists of five columns of data: id, name, age, gender, and married:

  1. Click Save and now you will be able to see the employee table name appear on the side bar. We have successfully installed MySQL and set up our first database and data table.
  1. We need to insert data into the database from phpMyAdmin so that we will be able to retrieve it in the next example. Click on the Insert tab while you're still in the employee table. You will then be taken to another page to insert new data into the employee table:

  1. Let's set up the SQL driver for our Qt project. Just go to your Qt installation folder and look for the sqldrivers folder. For example, mine is located at C:Qt5.5mingw492_32pluginssqldrivers.
  2. Copy the entire sqldrivers folder to your project's build directory. You can remove the DLL files that are not relevant to the SQL Server you're running. In our case, since we're using a MySQL Server, we can delete everything except qsqlmysql.dll and qsqlmysqld.dll. The DLL file with the letter d at the end is for debug builds only, while the other one is for release builds. Put those DLL files in their respective build directories, for example, builds/debug/sqldrivers/qsqlmysqld.dll for debug builds and builds/release/sqldrivers/qsqlmysql.dll for release builds.
  3. The DLL files mentioned in the previous step are the drivers that enable Qt to communicate with different types of SQL architecture. You may also need the DLL file of the SQL client library in order for the driver to work. In our case, we need libmysql.dll to be located in the same directory as our program's executable. You can either get it from the installation directory of MySQL or download the Connector/C++ package from the official website: https://dev.mysql.com/downloads/connector/cpp/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset