QSqlField 类

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 }

特性

公共函数

(从 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 )
(从 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 QSqlRecord s 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 () 和 isReadOnly (). The field's data can be checked to see if it isNull (),及其 value () retrieved. When editing the data can be set with setValue () or set to NULL with clear ().

另请参阅 QSqlRecord .

成员类型文档编制

enum QSqlField:: RequiredStatus

Specifies whether the field is required or optional.

常量 描述
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.

另请参阅 requiredStatus .

特性文档编制

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

注意: 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 QSqlField resulting from a QSqlRecord returned from QSqlDatabase::record () 或 QSqlDatabase::primaryIndex ().

该特性在 Qt 6.8 引入。

访问函数:

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.

该特性在 Qt 6.8 引入。

访问函数:

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.

该特性在 Qt 6.8 引入。

访问函数:

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.

该特性在 Qt 6.8 引入。

访问函数:

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.

该特性在 Qt 6.8 引入。

访问函数:

QMetaType metaType () const
void setMetaType (QMetaType type )

另请参阅 QSqlDatabase::numericalPrecisionPolicy .

name : QString

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

访问函数:

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.

该特性在 Qt 6.8 引入。

访问函数:

int precision () const
void setPrecision (int precision )

[since 6.8] readOnly : bool

当此特性为 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 ().

该特性在 Qt 6.8 引入。

访问函数:

bool isReadOnly () const
void setReadOnly (bool readOnly )

[since 6.8] requiredStatus : RequiredStatus

此特性保持 RequiredStatus of the field. An INSERT will fail if a required field does not have a value.

该特性在 Qt 6.8 引入。

访问函数:

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

另请参阅 RequiredStatus .

[since 6.8] tableName : QString

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

该特性在 Qt 6.8 引入。

访问函数:

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

[since 6.8] value : QVariant

此特性保持 value 作为 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 ().

该特性在 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())

这是重载函数。

Constructs an empty field called fieldName 类型 type in table .

该函数在 Qt 6.0 引入。

QSqlField:: QSqlField (const QSqlField & other )

构造副本为 other .

[noexcept] QSqlField:: ~QSqlField ()

销毁对象并释放任何分配资源。

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.

注意: Getter function for property defaultValue.

另请参阅 setDefaultValue ().

bool QSqlField:: isAutoValue () const

返回值为 autoValue .

注意: getter 函数对于特性 autoValue .

bool QSqlField:: isGenerated () const

返回值为 generated .

注意: getter 函数对于特性 generated .

bool QSqlField:: isNull () const

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

另请参阅 value .

bool QSqlField:: isReadOnly () const

返回值为 readOnly .

注意: getter 函数对于特性 readOnly .

bool QSqlField:: isValid () const

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

int QSqlField:: length () const

Returns the value of length.

注意: Getter function for property length.

另请参阅 setLength ().

QMetaType QSqlField:: metaType () const

Returns the value of metaType.

注意: Getter function for property metaType.

另请参阅 setMetaType ().

QString QSqlField:: name () const

Returns the value of name.

注意: Getter function for property name.

另请参阅 setName ().

int QSqlField:: precision () const

Returns the value of precision.

注意: Getter function for property precision.

另请参阅 setPrecision ().

QSqlField::RequiredStatus QSqlField:: requiredStatus () const

Returns the value of requiredStatus.

注意: Getter function for property requiredStatus.

另请参阅 setRequiredStatus ().

void QSqlField:: setAutoValue ( bool autoVal )

设置 autoValue to autoVal .

注意: setter 函数对于特性 autoValue .

另请参阅 isAutoValue ().

void QSqlField:: setDefaultValue (const QVariant & value )

设置 defaultValue to value .

注意: setter 函数对于特性 defaultValue .

另请参阅 defaultValue ().

void QSqlField:: setGenerated ( bool gen )

设置 generated to gen .

注意: setter 函数对于特性 generated .

另请参阅 isGenerated ().

void QSqlField:: setLength ( int fieldLength )

设置 length to fieldLength .

注意: setter 函数对于特性 length .

另请参阅 length ().

void QSqlField:: setMetaType ( QMetaType type )

设置 metaType to type .

注意: setter 函数对于特性 metaType .

另请参阅 metaType ().

void QSqlField:: setName (const QString & name )

设置 name to name .

注意: setter 函数对于特性 name .

另请参阅 name ().

void QSqlField:: setPrecision ( int precision )

设置 precision to precision .

注意: setter 函数对于特性 precision .

另请参阅 precision ().

void QSqlField:: setReadOnly ( bool readOnly )

设置 readOnly to readOnly .

注意: setter 函数对于特性 readOnly .

另请参阅 isReadOnly ().

void QSqlField:: setRequired ( bool required )

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

另请参阅 requiredStatus .

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

设置 requiredStatus to required .

注意: setter 函数对于特性 requiredStatus .

另请参阅 requiredStatus ().

void QSqlField:: setTableName (const QString & tableName )

设置 tableName to tableName .

注意: setter 函数对于特性 tableName .

另请参阅 tableName ().

void QSqlField:: setValue (const QVariant & value )

设置 value to value .

注意: setter 函数对于特性 value .

另请参阅 value ().

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

Swaps this field with other 。此操作非常快且从不失败。

该函数在 Qt 6.6 引入。

QString QSqlField:: tableName () const

Returns the tableName.

注意: Getter function for property tableName.

另请参阅 setTableName ().

QVariant QSqlField:: value () const

Returns the value of value.

注意: Getter function for property value.

另请参阅 setValue ().

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

返回 true if the field is unequal to other ;否则返回 false。

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

Sets the field equal to other .

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

返回 true if the field is equal to other ;否则返回 false。