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。