QSqlField Class

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

Public Types

enum RequiredStatus { Required, Optional, Unknown }

Properties

Public Functions

(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

Detailed Description

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.

Member Type Documentation

enum QSqlField::RequiredStatus

Specifies whether the field is required or optional.

ConstantValueDescription
QSqlField::Required1The field must be specified when inserting records.
QSqlField::Optional0The fields doesn't have to be specified when inserting records.
QSqlField::Unknown-1The database driver couldn't determine whether the field is required or optional.

See also requiredStatus.

Property Documentation

[since 6.8] autoValue : bool

If 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 : QVariant

This 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 : bool

This 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 : int

This 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:

int length() const
void setLength(int fieldLength)

[since 6.8] metaType : QMetaType

This 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.

name : QString

This property holds the name of the field. This can be the column name or a user given alias.

Access functions:

QString name() const
void setName(const QString &name)

[since 6.8] precision : int

This 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 : bool

When 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 : RequiredStatus

This 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 : QString

This 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 : QVariant

This 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:

QVariant value() const
void setValue(const QVariant &value)

Member Function Documentation

[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.

QSqlField::QSqlField(const QSqlField &other)

Constructs a copy of other.

[noexcept] QSqlField::~QSqlField()

Destroys the object and frees any allocated resources.

void QSqlField::clear()

Clears the value of the field and sets it to NULL. If the field is read-only, nothing happens.

QVariant QSqlField::defaultValue() const

Sets the value of defaultValue.

Note: Getter function for property defaultValue.

See also setDefaultValue().

bool QSqlField::isAutoValue() const

Returns the value of autoValue.

Note: Getter function for property autoValue.

bool QSqlField::isGenerated() const

Returns the value of generated.

Note: Getter function for property generated.

bool QSqlField::isNull() const

Returns true if the field's value is NULL; otherwise returns false.

See also value.

bool QSqlField::isReadOnly() const

Returns the value of readOnly.

Note: Getter function for property readOnly.

bool QSqlField::isValid() const

Returns true if the field's variant type is valid; otherwise returns false.

int QSqlField::length() const

Returns the value of length.

Note: Getter function for property length.

See also setLength().

QMetaType QSqlField::metaType() const

Returns the value of metaType.

Note: Getter function for property metaType.

See also setMetaType().

QString QSqlField::name() const

Returns the value of name.

Note: Getter function for property name.

See also setName().

int QSqlField::precision() const

Returns the value of precision.

Note: Getter function for property precision.

See also setPrecision().

QSqlField::RequiredStatus QSqlField::requiredStatus() const

Returns the value of requiredStatus.

Note: Getter function for property requiredStatus.

See also setRequiredStatus().

void QSqlField::setAutoValue(bool autoVal)

Sets autoValue to autoVal.

Note: Setter function for property autoValue.

See also isAutoValue().

void QSqlField::setDefaultValue(const QVariant &value)

Sets defaultValue to value.

Note: Setter function for property defaultValue.

See also defaultValue().

void QSqlField::setGenerated(bool gen)

Sets generated to gen.

Note: Setter function for property generated.

See also isGenerated().

void QSqlField::setLength(int fieldLength)

Sets length to fieldLength.

Note: Setter function for property length.

See also length().

void QSqlField::setMetaType(QMetaType type)

Sets metaType to type.

Note: Setter function for property metaType.

See also metaType().

void QSqlField::setName(const QString &name)

Sets name to name.

Note: Setter function for property name.

See also name().

void QSqlField::setPrecision(int precision)

Sets precision to precision.

Note: Setter function for property precision.

See also precision().

void QSqlField::setReadOnly(bool readOnly)

Sets readOnly to readOnly.

Note: Setter function for property readOnly.

See also isReadOnly().

void QSqlField::setRequired(bool required)

Sets the required status of this field to Required if required is true; otherwise sets it to Optional.

See also requiredStatus.

void QSqlField::setRequiredStatus(QSqlField::RequiredStatus required)

Sets requiredStatus to required.

Note: Setter function for property requiredStatus.

See also requiredStatus().

void QSqlField::setTableName(const QString &tableName)

Sets tableName to tableName.

Note: Setter function for property tableName.

See also tableName().

void QSqlField::setValue(const QVariant &value)

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 function is very fast and never fails.

This function was introduced in Qt 6.6.

QString QSqlField::tableName() const

Returns the tableName.

Note: Getter function for property tableName.

See also setTableName().

QVariant QSqlField::value() const

Returns the value of value.

Note: Getter function for property value.

See also setValue().

bool QSqlField::operator!=(const QSqlField &other) const

Returns true if the field is unequal to other; otherwise returns false.

QSqlField &QSqlField::operator=(const QSqlField &other)

Sets the field equal to other.

bool QSqlField::operator==(const QSqlField &other) const

Returns true if the field is equal to other; otherwise returns false.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.