The preceding two sections mainly describe the SQL interface layer. In this section, we will first look at the user interface layer.
Previously, we have mentioned that the user interface layer provides ing between database data and forms represented by user data.
In addition to the qsqlquery class, QT also provides three high-level classes for accessing the database.
Class Name |
Purpose |
Qsqlquerymdoel |
Read-Only model based on any SQL statement |
Qsqltablemodel |
Read/write model based on a single table |
Qsqlreltionaltablemodel |
Subclass of qsqltablemodel, with support for foreign keys added |
These three classes can be used independently for database operations when they do not involve the graphical representation of the database. It can also be mapped to a view-based QT class, such as qlistview and qtableview, as a data source.
The following code is added on the basis of the previous section.
1. qsqlquerymdoel
QSqlQueryModel *model = new QSqlQueryModel(); model->setQuery("SELECT table_user.user_id, table_group.group_name FROM table_group," "table_user WHERE table_group.group_id=table_user.group_id"); for(int i = 0; i < model->rowCount(); i++) { qDebug() << model->record(i).value("user_id").toString(); qDebug() << model->record(i).value("group_name").toString(); }
This Code uses multi-table queries to obtain the employee ID and data of the corresponding group.
Here we will mention the SQLite multi-Table query method.
Multi-Table query method:
Sqlite3 can have multiple multi-Table query methods, such as select (select * From table2) from Table1 Where xxx = xxx;
select table1.abc from table1,table2 where table1.xxx=table2.xxx;
Or
select table1.abc from table1 inner join table2 on table1.xxx=table2.xxx;
After qsqlquerymodel: setquery () is used to set the query statement (the query result set is obtained after execution), we can use record (I) to access each individual record.
2. qsqltablemodel
Qsqltablemodel tablemodel; // query tablemodel in a single table. settable ("table_user"); // bind table tablemodel. setfilter ("group_id = 1"); // sets the query condition tablemodel. select (); // query for (INT I = 0; I <tablemodel. rowcount (); I ++) {qdebug () <tablemodel. record (I ). value (1 ). tostring ();}
Qsqltablemodel is easy to use. Note that you can only operate a single table.
Because the table_user table has only two fields. Therefore, when a field index is specified through value (), the index value is directly written. When operating on a large amount of data, it is best to specify fields through the index. For example:
int UserID_Index = tablemodel.record().indexOf("user_id");qDebug() << tablemodel.record(i).value(UserID_Index).toString();
Or
tablemodel.record(i).value("user_id").toString();
Note that you must call select (). Otherwise, the query result set cannot be obtained.
3. qsqlreltionaltablemodel
Qsqlrelationaltablemodel * model = new qsqlrelationaltablemodel (); Model-> settable ("table_user"); Model-> setrelation (1, qsqlrelation ("table_group", "group_id ", "group_id"); // indicates that table_group.group_id is the primary key model of table_group-> setheaderdata (0, QT: horizontal, TR ("employee ID ")); model-> setheaderdata (1, QT: horizontal, TR ("group"); If (! Model-> select () {qmessagebox: Critical (this, TR ("error prompt"), model-> lasterror (). text (), qmessagebox: Cancel );}
The usage of qsqlreltionaltablemodel is similar to that of qsqltablemodel. The role of Row 4 and row 5 is equivalent to setting the name (equivalent to the alias of the database Field) for the specified column in the model header)
The link for downloading the program source code is attached here: QT _ database _ User Interface Layer
The above are purely personal study notes. If anything goes wrong, I hope to raise it. We hope to study and make progress together. My email address is: xzy@yingzhi8.com