The QSqlField class manipulates the fields in SQL database tables and views. More...
| Header: | #include <QSqlField> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Sql)target_link_libraries(mytarget PRIVATE Qt6::Sql) |
| qmake: | QT += sql |
| enum | RequiredStatus { Required, Optional, Unknown } |
|
(since 6.0) |
QSqlField(const QString &fieldName = QString(), QMetaType type = QMetaType(), const QString &table = QString()) |
| QSqlField(const QSqlField &other) | |
| ~QSqlField() | |
| void | clear() |
| QVariant | defaultValue() const |
| bool | isAutoValue() const |
| bool | isGenerated() const |
| bool | isNull() const |
| bool | isReadOnly() const |
| bool | isValid() const |
| int | length() const |
| QMetaType | metaType() const |
| QString | name() const |
| int | precision() const |
| QSqlField::RequiredStatus | requiredStatus() const |
| void | setAutoValue(bool autoVal) |
| void | setDefaultValue(const QVariant &value) |
| void | setGenerated(bool gen) |
| void | setLength(int fieldLength) |
| void | setMetaType(QMetaType type) |
| void | setName(const QString &name) |
| void | setPrecision(int precision) |
| void | setReadOnly(bool readOnly) |
| void | setRequired(bool required) |
| void | setRequiredStatus(QSqlField::RequiredStatus required) |
| void | setTableName(const QString &tableName) |
| void | setValue(const QVariant &value) |
(since 6.6) void |
swap(QSqlField &other) |
| QString | tableName() const |
| QVariant | value() const |
| bool | operator!=(const QSqlField &other) const |
| QSqlField & | operator=(const QSqlField &other) |
| bool | operator==(const QSqlField &other) const |
QSqlField represents the characteristics of a single column in a database table or view, such as the data type and column name. A field also contains the value of the database column, which can be viewed or changed.
Field data values are stored as QVariants. Using an incompatible type is not permitted. For example:
QSqlField field("age", QMetaType::fromType<int>()); field.setValue(QPixmap()); // WRONG
However, the field will attempt to cast certain data types to the field data type where possible:
QSqlField field("age", QMetaType::fromType<int>()); field.setValue(QString("123")); // casts QString to int
QSqlField objects are rarely created explicitly in application code. They are usually accessed indirectly through QSqlRecords that already contain a list of fields. For example:
QSqlQuery query; ... QSqlRecord record = query.record(); QSqlField field = record.field("country");
A QSqlField object can provide some meta-data about the field, for example, its name(), variant type(), length(), precision(), defaultValue(), typeID(), and its requiredStatus(), isGenerated() and isReadOnly(). The field's data can be checked to see if it isNull(), and its value() retrieved. When editing the data can be set with setValue() or set to NULL with clear().
See also QSqlRecord.
Specifies whether the field is required or optional.
| Constant | Value | Description |
|---|---|---|
QSqlField::Required |
1 |
The field must be specified when inserting records. |
QSqlField::Optional |
0 |
The fields doesn't have to be specified when inserting records. |
QSqlField::Unknown |
-1 |
The database driver couldn't determine whether the field is required or optional. |
See also requiredStatus.
[since 6.8] autoValue : boolIf the value is auto-generated by the database, for example auto-increment primary key values, this value is true.
Note: When using the ODBC driver, due to limitations in the ODBC API, the isAutoValue() field is only populated in a QSqlField resulting from a QSqlRecord obtained by executing a SELECT query. It is false in a QSqlField resulting from a QSqlRecord returned from QSqlDatabase::record() or QSqlDatabase::primaryIndex().
This property was introduced in Qt 6.8.
Access functions:
| bool | isAutoValue() const |
| void | setAutoValue(bool autoVal) |
[since 6.8] defaultValue : QVariantThis property holds the default value for this field. Only some database drivers supports this property. Currently those are SQLite, PostgreSQL, Oracle and MySQL/MariaDB.
This property was introduced in Qt 6.8.
Access functions:
| QVariant | defaultValue() const |
| void | setDefaultValue(const QVariant &value) |
[since 6.8] generated : boolThis property holds the generated state. If generated is false, no SQL will be generated for this field; otherwise, Qt classes such as QSqlQueryModel and QSqlTableModel will generate SQL for this field.
This property was introduced in Qt 6.8.
Access functions:
| bool | isGenerated() const |
| void | setGenerated(bool gen) |
[since 6.8] length : intThis property holds the field's length.
If the value is negative, it means that the information is not available from the database. For strings this is the maximum number of characters the string can hold; the meaning varies for other types.
This property was introduced in Qt 6.8.
Access functions:
[since 6.8] metaType : QMetaTypeThis property holds the field's type as stored in the database. Note that the actual value might have a different type, Numerical values that are too large to store in a long int or double are usually stored as strings to prevent precision loss.
This property was introduced in Qt 6.8.
Access functions:
| QMetaType | metaType() const |
| void | setMetaType(QMetaType type) |
See also QSqlDatabase::numericalPrecisionPolicy.
This property holds the name of the field. This can be the column name or a user given alias.
Access functions:
[since 6.8] precision : intThis property holds the field's precision; this is only meaningful for numeric types.
If the returned value is negative, it means that the information is not available from the database.
This property was introduced in Qt 6.8.
Access functions:
| int | precision() const |
| void | setPrecision(int precision) |
[since 6.8] readOnly : boolWhen this property is true then this QSqlField cannot be modified. A read-only field cannot have its value set with setValue() and cannot be cleared to NULL with clear().
This property was introduced in Qt 6.8.
Access functions:
| bool | isReadOnly() const |
| void | setReadOnly(bool readOnly) |
[since 6.8] requiredStatus : RequiredStatusThis property holds the RequiredStatus of the field. An INSERT will fail if a required field does not have a value.
This property was introduced in Qt 6.8.
Access functions:
| QSqlField::RequiredStatus | requiredStatus() const |
| void | setRequiredStatus(QSqlField::RequiredStatus required) |
See also RequiredStatus.
[since 6.8] tableName : QStringThis property holds the tableName of the field.
Note: When using the QPSQL driver, due to limitations in the libpq library, the tableName() field is not populated in a QSqlField resulting from a
QSqlRecord obtained by QSqlQuery::record() of a forward-only query.
This property was introduced in Qt 6.8.
Access functions:
| QString | tableName() const |
| void | setTableName(const QString &tableName) |
[since 6.8] value : QVariantThis property holds the value as a QVariant
Setting a value to a read-only QSqlField is a no-op. If the data type of value differs from the field's current data type, an attempt is made to cast it to the proper type. This preserves the data type of the field in the case of assignment, e.g. a QString to an integer data type.
To set the value to NULL, use clear().
This property was introduced in Qt 6.8.
Access functions:
[explicit, since 6.0] QSqlField::QSqlField(const QString &fieldName = QString(), QMetaType type = QMetaType(), const QString &table = QString())This is an overloaded function.
Constructs an empty field called fieldName of type type in table.
This function was introduced in Qt 6.0.
Constructs a copy of other.
[noexcept] QSqlField::~QSqlField()Destroys the object and frees any allocated resources.
Clears the value of the field and sets it to NULL. If the field is read-only, nothing happens.
Sets the value of defaultValue.
Note: Getter function for property defaultValue.
See also setDefaultValue().
Returns the value of autoValue.
Note: Getter function for property autoValue.
Returns the value of generated.
Note: Getter function for property generated.
Returns true if the field's value is NULL; otherwise returns false.
See also value.
Returns the value of readOnly.
Note: Getter function for property readOnly.
Returns true if the field's variant type is valid; otherwise returns false.
Returns the value of length.
Note: Getter function for property length.
See also setLength().
Returns the value of metaType.
Note: Getter function for property metaType.
See also setMetaType().
Returns the value of name.
Note: Getter function for property name.
See also setName().
Returns the value of precision.
Note: Getter function for property precision.
See also setPrecision().
Returns the value of requiredStatus.
Note: Getter function for property requiredStatus.
See also setRequiredStatus().
Sets autoValue to autoVal.
Note: Setter function for property autoValue.
See also isAutoValue().
Sets defaultValue to value.
Note: Setter function for property defaultValue.
See also defaultValue().
Sets generated to gen.
Note: Setter function for property generated.
See also isGenerated().
Sets length to fieldLength.
Note: Setter function for property length.
See also length().
Sets metaType to type.
Note: Setter function for property metaType.
See also metaType().
Sets name to name.
Note: Setter function for property name.
See also name().
Sets precision to precision.
Note: Setter function for property precision.
See also precision().
Sets readOnly to readOnly.
Note: Setter function for property readOnly.
See also isReadOnly().
Sets the required status of this field to Required if required is true; otherwise sets it to Optional.
See also requiredStatus.
Sets requiredStatus to required.
Note: Setter function for property requiredStatus.
See also requiredStatus().
Sets tableName to tableName.
Note: Setter function for property tableName.
See also tableName().
Sets value to value.
Note: Setter function for property value.
See also value().
[noexcept, since 6.6] void QSqlField::swap(QSqlField &other)Swaps this field with other. This operation is very fast and never fails.
This function was introduced in Qt 6.6.
Returns the tableName.
Note: Getter function for property tableName.
See also setTableName().
Returns the value of value.
Note: Getter function for property value.
See also setValue().
Returns true if the field is unequal to other; otherwise returns false.
Sets the field equal to other.
Returns true if the field is equal to other; otherwise returns false.