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 couples of things we need to change. First, open up
mainwindow.ui
and replace the labels for name and age with line edit widgets. Then, add three buttons to the canvas and call them Update, Insert, and Delete:After that, open up
mainwindow.h
and add the following variables under private inheritance:private: Ui::MainWindow *ui; QSqlDatabase db; bool connected; int currentID;
Next, open up
mainwindow.cpp
and go to the class constructor. It is still pretty much the same as the previous example, except we store the database connection status in a Boolean variable calledconnected
and we also obtain the ID...