Last updated
Was this helpful?
Last updated
Was this helpful?
Accessing SQL databases from C++ applications is very simple with Qt library. Here is some short examples that presents how to do it. I have chosen
engine because itβs the easiest engine to set up (it requires no server, no configurationβ¦), still itβs suitable for the most of possible applications.
Letβs assume, for simplicity, that we need to store people data β only name and corresponding id. With SQLite you can create such a simple database with two console commands. First line is creating database people. Second one is creating the table people. It contains a key (integer) and name (text). To leave the SQLite console just type β. quitβ
.
Congrats, your database is ready. Now we can create C++ application to play with it.
Create new Qt Quick project. In .pro file you need to add:
QT += sql
It will link your project against QtSql module. If you donβt use QtCreator but another IDE, for example Visual Studio, your project may need setting linker dependency to QtSql.lib. You need to do it if your compilation fails with unresolved external symbol error. In VS linker settings is located in Properties/Configuration properties/Linker/Input
. In the Additional Dependencies add Qt5Sqld.lib for Debug and Qt5Sql.lib for release.
Now you can create a class responsible for database access. Letβs create a class database.
database.h
:
database.cpp
:
Now include the header file of database ( database.h
) into the main.cpp file of your qt project.
main.cpp
:
In openDatabase(path)
pass the path of your database.db
file location:
Now build the project using CTRL + R
and see the output in application output section.
Congratulation you are successfully open sqlite3 database in windows.
For managing people resources we need the possibility to:
add new person,
remove person,
check if person exists in database,
print all persons,
delete all persons.
For managing people resources we need to first create the table into the sqlite3 database using the cpp . So, firstly we create the API for creating the table .
bool
createTabel
();
Creating the table for storing the people information into the table.
Add bool
createTabel
();
into the database.h file.
Add the following API into the database.cpp file
Now build and run the project using the ctrl+r
and see the output into application output section :
you can also confirm the table created success or not using the command line .
Before creating the table run the command .table
into the sqlite> terminal .
Now you can see there is no table present into the mydatabase .
Now Create the table .. And see the output .
Now you can see these are one table present into the mydatabase.db
which is name people
Now table create successfully, itβs time to inset some people data into the table. so, we need to create API to insert the data into table.
Add this API into the database.h file:
and definition into the database.cpp file:
And add the following code into the main.cpp file after the create table .
Now build and run the code and absorb the behaviors :
Output :
And similarly, all other APIs works try it yourself, if you face any issue, please let me know into the comment section.
database.cpp file :
database.h file
main.cpp
Output :
Congratulations you are successfully performed sqlite3 basic operation using qt quick application. do practice more and perform some more operation using qt quick application.
if you are facing any issue during practice comment out your problem .
Qt requires drivers to deal with SQL databases. Depending on your distribution, you can have the drivers installed by default with your Qt or not. If you face problems like βQSqlDatabase: QSQLITE driver not loadedβ
you need to install drivers manually. Error should be followed by a console output that informs you which drivers you have installed: βQSqlDatabase: available drivers:β
. .
Actions can be implemented in following way: define QSqlQuery
, then call method. Prepare argument can be raw SQL query string, or it can contain placeholders for variables β placeholders must begin with colon. After prepare call to fill the placeholders with proper values. When the query is ready, execute it.
Note :
You can find the whole source code on my . All you need to do before running this example is to create your SQLite database and write the valid path to it in main.cpp.
sql
to your .pro file