本页内容

QSqlField Class

QSqlField 类用于操作 SQL 数据库表和视图中的字段。更多内容...

头文件: #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(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 表示数据库表或视图中单个列的特性,例如数据类型和列名。字段还包含数据库列的值,该值可以被查看或修改。

字段数据值以 QVariant 形式存储。不允许使用不兼容的类型。例如:

    QSqlField field("age", QMetaType::fromType<int>());
    field.setValue(QPixmap());  // WRONG

不过,在可能的情况下,该字段会尝试将某些数据类型转换为字段的数据类型:

    QSqlField field("age", QMetaType::fromType<int>());
    field.setValue(QString("123"));  // casts QString to int

在应用程序代码中,很少会显式创建 QSqlField 对象。通常是通过已经包含字段列表的 `QSqlRecord` 间接访问这些字段。例如:

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

一个 QSqlField 对象可以提供有关该字段的一些元数据,例如:其name()、variantmetaType()、length()、precision()、defaultValue()、typeID(),以及其requiredStatus()、isGenerated() 和isReadOnly()。 可以检查该字段的数据,以查看其是否isNull(),并检索其value()。在编辑时,可通过setValue()设置数据,或通过clear()将其设为NULL。

另请参阅 QSqlRecord 。

成员类型文档

enum QSqlField::RequiredStatus

指定该字段是必填还是可选。

常量值描述
QSqlField::Required1插入记录时必须指定该字段。
QSqlField::Optional0插入记录时,该字段不必指定。
QSqlField::Unknown-1数据库驱动程序无法确定该字段是必填还是可选。

另请参阅 requiredStatus 。

属性文档

[since 6.8] autoValue : bool

如果该值由数据库自动生成(例如自增主键值),则该值为true 。

注意: 向数据库写入新记录时, 只有在记录提交到数据库后,autoValue 字段才会包含数据库生成的新的值。 使用 ODBC 驱动程序时,由于 ODBC API 的限制,isAutoValue() 字段仅在执行SELECT 查询后通过QSqlRecord 获得的QSqlField 中填充。对于由QSqlDatabase::record() 或QSqlDatabase::primaryIndex() 返回的QSqlRecord 产生的QSqlField ,该字段为false 。

该枚举在 Qt 6.8 中引入。

访问函数:

bool isAutoValue() const
void setAutoValue(bool autoVal)

[since 6.8] defaultValue : QVariant

该属性存储此字段的默认值。仅部分数据库驱动程序支持此属性。目前支持的包括 SQLite、PostgreSQL、Oracle 和 MySQL/MariaDB。

该枚举类型在 Qt 6.8 中引入。

访问函数:

QVariant defaultValue() const
void setDefaultValue(const QVariant &value)

[since 6.8] generated : bool

该属性保存生成的状态。如果generated 的值为false ,则不会为此字段生成任何SQL;否则,QSqlQueryModel 和QSqlTableModel 等Qt类会为此字段生成SQL。

该枚举在 Qt 6.8 中引入。

访问函数:

bool isGenerated() const
void setGenerated(bool gen)

[since 6.8] length : int

该属性存储字段的长度。

如果该值为负数,则表示数据库中没有此信息。对于字符串,该值表示字符串可容纳的最大字符数;对于其他类型,其含义则有所不同。

该枚举在 Qt 6.8 中引入。

访问函数:

int length() const
void setLength(int fieldLength)

[since 6.8] metaType : QMetaType

该属性保存了数据库中存储的字段类型。请注意,实际值可能具有不同的类型;如果数值过大,无法存储在 long int 或 double 类型中,通常会将其作为字符串存储,以避免精度损失。

该枚举在 Qt 6.8 中引入。

访问函数:

QMetaType metaType() const
void setMetaType(QMetaType type)

另请参阅 QSqlDatabase::numericalPrecisionPolicy 。

name : QString

该属性存储字段的名称。该名称可以是列名,也可以是用户指定的别名。

访问函数:

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

[since 6.8] precision : int

该属性存储字段的精度;此属性仅对数值类型有意义。

如果返回值为负数,则表示数据库中没有该信息。

该枚举在 Qt 6.8 中引入。

访问函数:

int precision() const
void setPrecision(int precision)

[since 6.8] readOnly : bool

当此属性的值设为true 时,该QSqlField 便无法被修改。只读字段无法通过setValue()设置其值,也无法通过clear()将其清空为NULL。

该枚举在 Qt 6.8 中引入。

访问函数:

bool isReadOnly() const
void setReadOnly(bool readOnly)

[since 6.8] requiredStatus : RequiredStatus

该属性保存字段的RequiredStatus 。如果必填字段没有值,则INSERT 将失败。

该枚举在 Qt 6.8 中引入。

访问函数:

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

另请参阅 RequiredStatus 。

[since 6.8] tableName : QString

该属性存储字段的 tableName。

注意:在 使用 QPSQL 驱动程序时 ,由于 libpq 库的限制,对于通过QSqlQuery::record() 获取的、由QSqlRecord 生成的QSqlField ,其tableName() 字段不会被填充。

此枚举在 Qt 6.8 中引入。

访问函数:

QString tableName() const
void setTableName(const QString &tableName)

[since 6.8] value : QVariant

该属性将value 作为QVariant

将value 设置为只读的QSqlField 将不产生任何效果。如果value 的数据类型与字段的当前数据类型不同,系统会尝试将其转换为正确的类型。这在赋值操作中可保持字段的数据类型,例如将QString 赋值给整数数据类型。

若要将值设置为 NULL,请使用clear()。

该枚举在 Qt 6.8 中引入。

访问函数:

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

成员函数文档

[explicit, since 6.0] QSqlField::QSqlField(const QString &fieldName = QString(), QMetaType type = QMetaType(), const QString &table = QString())

在table 中,创建一个名为fieldName 、类型为type 的空字段。

这是一个重载函数。

该函数在 Qt 6.0 中引入。

QSqlField::QSqlField(const QSqlField &other)

创建other 的副本。

[noexcept default] QSqlField::QSqlField(QSqlField &&other)

通过Move构造一个QSqlField 实例。

[noexcept] QSqlField::~QSqlField()

销毁该对象并释放所有已分配的资源。

void QSqlField::clear()

清除该字段的值并将其设置为NULL。如果该字段为只读,则不会执行任何操作。

QVariant QSqlField::defaultValue() const

设置 defaultValue 的值。

注意: 这是属性 defaultValue 的获取器 函数。

另请参阅 setDefaultValue()。

bool QSqlField::isAutoValue() const

返回autoValue 的值。

注意: 这是属性 `autoValue` 的获取器 函数。

bool QSqlField::isGenerated() const

返回generated 的值。

注意: 这是属性 `generated` 的获取器 函数。

bool QSqlField::isNull() const

如果字段的值为 NULL,则返回true ;否则返回 false。

另请参阅 value 。

bool QSqlField::isReadOnly() const

返回readOnly 的值。

注意: 这是属性readOnly 的获取器 函数。

bool QSqlField::isValid() const

如果字段的变体类型有效,则返回true ;否则返回false 。

int QSqlField::length() const

返回 length 的值。

注意: 这是 length 属性的获取器 函数。

另请参阅 setLength()。

QMetaType QSqlField::metaType() const

返回 metaType 的值。

注意: 这是属性 metaType 的获取 函数。

另请参阅 setMetaType()。

QString QSqlField::name() const

返回 name 的值。

注意: 这是属性 name 的获取器 函数。

另请参阅 ` setName()`。

int QSqlField::precision() const

返回 precision 的值。

注意: 这是属性 precision 的获取 函数。

另请参阅 ` setPrecision()`。

QSqlField::RequiredStatus QSqlField::requiredStatus() const

返回 requiredStatus 的值。

注意: 这是 requiredStatus 属性的获取器 函数。

另请参阅 ` setRequiredStatus()`。

void QSqlField::setAutoValue(bool autoVal)

将autoValue 设置为autoVal 。

注意: 这是属性autoValue 的设置 函数。

另请参阅 isAutoValue()。

void QSqlField::setDefaultValue(const QVariant &value)

将defaultValue 设置为value 。

注意: 这是属性defaultValue 的设置 函数。

另请参阅 defaultValue()。

void QSqlField::setGenerated(bool gen)

将generated 设置为gen 。

注意: 这是属性generated 的设置 函数。

另请参阅 isGenerated()。

void QSqlField::setLength(int fieldLength)

将length 设置为fieldLength 。

注意: 这是属性length 的设置 函数。

另请参阅 length()。

void QSqlField::setMetaType(QMetaType type)

将metaType 设置为type 。

注意: 属性metaType 的设置 函数。

另请参阅 metaType()。

void QSqlField::setName(const QString &name)

将name 设置为name 。

注意: 这是属性name 的设置 函数。

另请参阅 name()。

void QSqlField::setPrecision(int precision)

将precision 设置为precision 。

注意: 这是属性precision 的设置 函数。

另请参阅 precision()。

void QSqlField::setReadOnly(bool readOnly)

将readOnly 设置为readOnly 。

注意: 这是属性readOnly 的设置 函数。

另请参阅 isReadOnly()。

void QSqlField::setRequired(bool required)

如果required 为true,则将该字段的必需状态设置为Required ;否则将其设置为Optional 。

另请参阅 requiredStatus 。

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

将requiredStatus 设置为required 。

注意: 属性requiredStatus 的设置 函数。

另请参阅 requiredStatus()。

void QSqlField::setTableName(const QString &tableName)

将tableName 设置为tableName 。

注意: 这是属性tableName 的设置 函数。

另请参阅 tableName()。

void QSqlField::setValue(const QVariant &value)

将value 设置为value 。

注意: 这是属性value 的设置 函数。

另请参阅 value()。

[noexcept, since 6.6] void QSqlField::swap(QSqlField &other)

将该字段与other 进行交换。此操作速度极快,且绝不会失败。

该函数在 Qt 6.6 中引入。

QString QSqlField::tableName() const

返回 tableName。

注意: 这是 tableName 属性的获取函数 。

另请参阅 setTableName()。

QVariant QSqlField::value() const

返回 value 的值。

注意: 这是属性 value 的获取器 函数。

另请参阅 setValue()。

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

如果该字段不等于other ,则返回true ;否则返回 false。

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

将该字段设置为other 。

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

如果该字段等于other ,则返回true ;否则返回 false。

© 2026 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.