Writing basic SQL queries
In the previous example, we wrote our very first SQL query, which involves the SELECT
statement. This time, we will learn how to use some other SQL statements, such as INSERT
, UPDATE
, and DELETE
.
How to do it…
Let’s create a simple program that demonstrates basic SQL query commands by following these steps:
- We can use our previous project files, but there are a couple of things we need to change. Open
mainwindow.ui
and replace the labels for Name and Age with line-edit widgets. Then, add three buttons to the canvas and call themUPDATE
,INSERT
, andDELETE
:
Figure 12.12 – Modify the UI to this
- Open
mainwindow.h
and add the following variables under the private inheritance:private: Ui::MainWindow *ui; QSqlDatabase db; bool connected; int currentID;
- Open
mainwindow...