Qt Literacy-QSqlField Theory Summary

1. Overview of QSqlField class

QSqlField represents the characteristics of a single column in a database table or view . To put it bluntly, it is a data field in a database record. Such as data types and column names. Fields also contain the values ​​of database columns, which can be viewed or changed.

Just like the data table example below, QSqlField can represent a value of a record under the id column.
Insert image description here
The relationship between this class and QSqlRecord is as shown below. A QSqlRecord essentially maintains a set of QSqlField objects. When we query a record in the data set queried from the database, it can be represented by QSqlRecord in Qt.
Insert image description here

2. QSqlField usage

QSqlField field data values ​​are stored as QVariant. Incompatible types are not allowed. For example:

QSqlField field("age", QVariant::Int);
field.setValue(QPixmap());  // WRONG

However, fields attempt to convert certain data types to the field data type when possible:

QSqlField field("age", QVariant::Int);
field.setValue(QString("123"));  // casts QString to int

QSqlField objects are rarely created explicitly in application code. They are usually accessed indirectly via QSqlRecords that already contain a set of fields. For example:

QSqlQuery query;
      ...
QSqlRecord record = query.record();
QSqlField field = record.field("country");

The QSqlField object can provide some metadata about the field, such as its name(), variant type(), length(), precision(), defaultValue(), typeID(), and its requiredStatus(), isGenerated() and isReadOnly(). You can check whether the field's data is null() and get its value(). You can use setValue() to set it when editing data, or you can use clear() to set it to NULL.

3. Overview of QSqlRecord class

The QSqlRecord class encapsulates the functionality and characteristics of a database record (usually a row in a table or view in a database) .

QSqlRecord supports adding and removing fields, and setting and retrieving field values.

Use setValue() to set the value of a record field by name or position; if you want to set the field to null, you can use setNull(). To find the position of a field by name, use indexOf(), and to find the name of a field at a specific position, use fieldName(). Use field() to obtain the QSqlField object of a given field. Use contains() to check whether a record contains a specific field name.

When a query is executed on the database, only fields for which isGenerated() is true are included in the generated SQL.

A record can use append() or insert() to add fields, use replace() to replace fields, and use remove() to delete fields. All fields can be removed using clear(). The number of fields is given by count(); all values ​​can be cleared (made null) using clearValues().

4. Use of QSqlRecord

This class represents a database record. This class is mainly used when querying the database results, parsing the results, or constructing objects that need to be inserted into the database. In the Qt database module, this class is the interface for all SQL query results.

Guess you like

Origin blog.csdn.net/qq_43680827/article/details/133376226