QSqlField Class▲
-
Header: QSqlField
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Sql)
target_link_libraries(mytarget PRIVATE Qt6::Sql)
-
qmake: QT += sql
-
Group: QSqlField is part of Database Classes, shared
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&
lt;int
&
gt;());
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&
lt;int
&
gt;());
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▲
See also QSqlRecord
Member Type Documentation▲
enum QSqlField::RequiredStatus▲
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▲
See also requiredStatus()
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.
See Also▲
See also setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly()
QSqlField::QSqlField(const QSqlField &other)▲
Constructs a copy of other.
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.
See Also▲
See also setValue(), isReadOnly(), requiredStatus()
QVariant QSqlField::defaultValue() const▲
Returns the field's default value (which may be NULL).
See Also▲
See also setDefaultValue(), metaType(), requiredStatus(), length(), precision(), isGenerated()
bool QSqlField::isAutoValue() const▲
Returns true if the value is auto-generated by the database, for example auto-increment primary key values.
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().
See Also▲
See also setAutoValue()
bool QSqlField::isGenerated() const▲
Returns true if the field is generated; otherwise returns false.
See Also▲
See also setGenerated(), metaType(), requiredStatus(), length(), precision(), defaultValue()
bool QSqlField::isNull() const▲
bool QSqlField::isReadOnly() const▲
Returns true if the field's value is read-only; otherwise returns false.
See Also▲
See also setReadOnly(), metaType(), requiredStatus(), length(), precision(), defaultValue(), isGenerated()
bool QSqlField::isValid() const▲
Returns true if the field's variant type is valid; otherwise returns false.
int QSqlField::length() const▲
Returns the field's length.
If the returned value is negative, it means that the information is not available from the database.
See Also▲
See also setLength(), metaType(), requiredStatus(), precision(), defaultValue(), isGenerated()
QMetaType QSqlField::metaType() const▲
Returns 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.
See Also▲
See also setMetaType()
QString QSqlField::name() const▲
int QSqlField::precision() const▲
Returns 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.
See Also▲
See also setPrecision(), metaType(), requiredStatus(), length(), defaultValue(), isGenerated()
QSqlField::RequiredStatus QSqlField::requiredStatus() const▲
Returns true if this is a required field; otherwise returns false. An INSERT will fail if a required field does not have a value.
See Also▲
See also setRequiredStatus(), metaType(), length(), precision(), defaultValue(), isGenerated()
void QSqlField::setAutoValue(bool autoVal)▲
void QSqlField::setDefaultValue(const QVariant &value)▲
Sets the default value used for this field to value.
See Also▲
See also defaultValue(), value(), setMetaType(), setRequiredStatus(), setLength(), setPrecision(), setGenerated(), setReadOnly()
void QSqlField::setGenerated(bool gen)▲
Sets the generated state. If gen is false, no SQL will be generated for this field; otherwise, Qt classes such as QSqlQueryModel and QSqlTableModel will generate SQL for this field.
See Also▲
See also isGenerated(), setMetaType(), setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setReadOnly()
void QSqlField::setLength(int fieldLength)▲
Sets the field's length to fieldLength. For strings this is the maximum number of characters the string can hold; the meaning varies for other types.
See Also▲
See also length(), setMetaType(), setRequiredStatus(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly()
void QSqlField::setMetaType(QMetaType type)▲
Set's the field's variant type to type.
See Also▲
See also metaType(), setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly()
void QSqlField::setName(const QString &name)▲
void QSqlField::setPrecision(int precision)▲
Sets the field's precision. This only affects numeric fields.
See Also▲
See also precision(), setMetaType(), setRequiredStatus(), setLength(), setDefaultValue(), setGenerated(), setReadOnly()
void QSqlField::setReadOnly(bool readOnly)▲
Sets the read only flag of the field's value to readOnly. A read-only field cannot have its value set with setValue() and cannot be cleared to NULL with clear().
See Also▲
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▲
See also setRequiredStatus(), requiredStatus()
void QSqlField::setRequiredStatus(QSqlField::RequiredStatus required)▲
Sets the required status of this field to required.
See Also▲
See also requiredStatus(), setMetaType(), setLength(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly()
void QSqlField::setTableName(const QString &table)▲
void QSqlField::setValue(const QVariant &value)▲
Sets the value of the field to value. If the field is read-only (isReadOnly() returns true), nothing happens.
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().
See Also▲
See also value(), isReadOnly(), defaultValue()
QString QSqlField::tableName() const▲
Returns the tableName of the field.
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.
See Also▲
See also setTableName()
QVariant QSqlField::value() const▲
Returns the value of the field as a QVariant.
Use isNull() to check if the field's value is NULL.
See Also▲
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.
Obsolete Members for QSqlField▲
The following members of class QSqlField are deprecated. We strongly advise against using them in new code.
Obsolete Member Function Documentation▲
QSqlField::QSqlField(const QString &fieldName, QVariant::Type type, const QString &table = QString())▲
This function is deprecated since 6.0. We strongly advise against using it in new code.
Use the constructor taking a QMetaType instead.
This is an overloaded function.
Constructs an empty field called fieldName of variant type type in table.
See Also▲
See also setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setGenerated(), setReadOnly()
void QSqlField::setType(QVariant::Type type)▲
This function is deprecated since 6.0. We strongly advise against using it in new code.
Use setMetaType() instead.
Sets the field's variant type to type.
See Also▲
See also type(), setMetaType()
QVariant::Type QSqlField::type() const▲
This function is deprecated since 6.0. We strongly advise against using it in new code.
Use metaType() instead.
Returns 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.