SQLite Database access through Qt

In this post, you will learn how to make a simple database access app using Qt creator.

Steps:

  1. Open Qt Creator Click on File from the menu and click on New File or Project…(Ctrl +  N).
  2. From the left column select Application, from the middle column select Qt Widget Application.
  3. Click on Choose.
  4. Give a name to the project and set the path where you want to save this project files.
  5. Click Next,
  6. Default kits will be selected.
  7. Click next.
  8. Give the class name of your choice. I gave InfoDB.
  9. Select Baseclass as QMainWindow from the dropdown list.
  10. (Generate form box can be unchecked as we are not going to use it, we will be creating UI with code only).
  11. Click Next
  12. Click on Finish.

main.cpp doesn`t need to be modified.

Open InfoDB.pro and add SQL for Qt+=

QT       += core gui widgets sql

Open infodb.h

and modify it as

#ifndef INFODB_H
#define INFODB_H

#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QListWidgetItem>
#include <QDebug>
#include <QDialog>
#include <QtSql>
#include <QDebug>
#include <QSqlDatabase>
#include <QTableWidget>
#include <QPropertyAnimation>


namespace Ui {
class InfoDB;
}

class InfoDB : public QMainWindow
{
    Q_OBJECT

public:
    explicit InfoDB(QWidget *parent = nullptr);
    ~InfoDB();
    QSqlDatabase db;

    bool isLoggedIn;
    int addOrDeleteFlag;
    QLabel *main_header;
    QLabel *main_footer;
    QPropertyAnimation *animation;
    QPropertyAnimation *animation_footer;
    QPushButton *btn_LogInLogOut;
    QPushButton *btn_add;
    QPushButton *btn_del;
    QPushButton *btn_info;
    QPushButton *btn_ShowTable;

    QString correctPassword;
    QDialog *pass_dlg_wid;
    QLineEdit *input_pass;
    QLabel *pass_header;
    QLabel *wrong_pass_info;

    QLabel *bg_image;
    QWidget *wid_modify;
    QLabel *lbl_name;
    QLabel *lbl_city;
    QLabel *lbl_zip;

    QLineEdit *name_input;
    QLineEdit *city_input;
    QLineEdit *zip_input;

    QDialog *search_dlg_wid;
    QTableWidget *tb_wid;

    QPushButton *btn_addGo;
private:
    void showSearchReult(QSqlQuery&,int);
    bool FindToDeleteCount;
private slots:
    void handle_OnOff();
    void authenticate_Input();
    void handle_AddInfoUI();
    void handle_AddInfoToDb();
    void handle_DelInfo();
    void handle_SearchInfo();
    void showTabledata();
private:
    Ui::InfoDB *ui;
};

#endif // INFODB_H

Open infodb.cpp

and copy-paste below mentioned code.

Note:-Don`t forget to put an image(sqliteqt.png) to the respective folder and give the path to the QPixmap.

bg_image->setPixmap(QPixmap("/*Mention your path here*/sqliteqt.png"));

Change the database path, name, and table name in infodb.cpp file to point to correction location.

db.setDatabaseName("C:/DBs/mydb.sqlite");

In each qry.exe do not forget to put your table name which you have added into the database.

status =  qry.exec("SELECT * FROM /*replace with your table name*/mytable WHERE zip=" + zip + ";");

I am using the Mozilla Firefox add-on to view the database content, you can use any other tool as well.

Link to use the add-on with Mozilla Firefox: sqlite-manager-webext

Download Project files

Video

Thank you for reading.

Leave a Comment

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