In this chapter, we will cover the following recipes:
SQL stands for Structured Query Language, a special programming language used to manage data held in a relational database management system. A SQL server is a database system designed to use one of the many types of SQL programming language to manage its data.
If you want to learn more about SQL, visit this link: http://www.w3schools.com/sql/sql_intro.asp.
Qt supports several different types of SQL driver in the form of plugins/add-ons. However, it's very easy to integrate these drivers to your Qt project. We will learn how to do it in the following example.
Let's set up our SQL server before we dive into Qt:
root
, which is saved in its configuration file. We don't want to use that for security reasons. So the next thing we need to do is to create an account for ourselves. Go to the Users tab located at the top and once you're on that page, click Add user located at the bottom. Key in your desired username and password in the fields in the login information pane. Choose Local for the Host option for now. At the bottom, you will see options related to Global privileges; check the Check All option and click Go:config.inc.php
file will be opened with your choice of text editor.config.inc.php
and change the word config
to cookie
:$cfg['Servers'][$i]['auth_type'] = 'config'; $cfg['Servers'][$i]['auth_type'] = 'cookie';
employee
table that consists of five columns of data: id
, name
, age
, gender
, and married
:employee
table name appear on the side bar. We have successfully installed MySQL and set up our first database and data table.employee
table; you will then be brought to another page for inserting new data into the employee
table:sqldrivers
folder. For example, mine is located at C:Qt5.5mingw492_32pluginssqldrivers
.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 back 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.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/.Qt provides us with SQL drivers so that we can easily connect to different types of SQL servers without implementing them ourselves.
Currently, Qt officially supports SQLite, MySQL, ODBC, and PostgreSQL. SQL architectures that are forks from one of the supported architectures, such as MariaDB (a fork of MySQL), may still compatible with Qt without much problem.
If you are using an architecture that isn't supported by Qt, you can still interact with your SQL database indirectly by sending an HTTP request using QNetworkAccessManager to your backend script (such as PHP, ASP, JSP, and so on), which can then communicate with the database.
If you only need a simple file-based database and don't plan to use a server-based database, SQLite is a good choice for you.
3.12.163.180