How to embed a database in your application with SQLite and Qt

This tutorial will show you how to use SQLite and Qt SQL to have a relational database in your application. It will cover all the basic operations needed to work with a database like creating a table, inserting data, executing a query and checking the results. Full project and source code are provided.

SQLite and Qt SQL

SQLite and Qt SQL

This tutorial is based on two main components: Qt SQL and SQLite.

The Qt SQL module provides a driver layer, SQL API layer, and a user interface layer for SQL databases.

SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. Basically a database stored in a single file (or memory).

Qt offers support (drivers) for other popular databases like MySQL and


  1. S.M.Mousavi

    Very simple and helpful.
    Is it possible to get result value of a column in current row using its name?
    Thank you.

    1. Davide Coppola (Post author)

      yes, you can pass a QString to QSqlQuery::value().

      This is less efficient than passing an index as showed in the tutorial though,

  2. mMeikZz

    why can’t i use mInputText? it seems there is something missing the compiler doesn’t know it.

    1. Davide Coppola (Post author)

      have you downloaded my source code?

      What error are you getting?

  3. Ray

    Shouldn’t “qWarning() << "ERROR: " << db.lastError();" be "qWarning() << "ERROR: " << db.lastError().text;" ?

  4. Juju

    Very Helpful !!
    Thanks a lot

  5. Yu-Diann Lu

    maybe something I get wrong…
    I use Visual Studio 2015, create a Qt (5.11.1) Gui project and cut and paste you main.cpp, mainwindow.cpp/h file, built project and run it

    but the mInputText is a inactive editor box, so I can not enter anything to search…
    What I get wrong…

  6. abdrew

    Not useful if using meory databse. How do we create a database that lives past the application life time? You know the very reason to use a db in the first place. How do we put it in a directory that makes sense.


Leave a Comment

Your email address will not be published. Required fields are marked *