内容表

上一话题

QSqlError

下一话题

QSqlIndex

QSqlField

QSqlField class manipulates the fields in SQL database tables and views. 更多

Inheritance diagram of PySide2.QtSql.QSqlField

概要

函数

详细描述

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:

field = QSqlField("age", QVariant.Int)
field.setValue(QPixmap())  # WRONG
											

However, the field will attempt to cast certain data types to the field data type where possible:

field = QSqlField("age", QVariant.Int)
field.setValue(str(123))  # casts str 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:

query = QSqlQuery()
...
record = query.record()
field = record.field("country")
											

A QSqlField object can provide some meta-data about the field, for example, its name() , variant type() , length() , precision() , defaultValue() , , 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() .

另请参阅

QSqlRecord

class QSqlField ( other )

QSqlField([fieldName=””[, type={}]])

QSqlField(fieldName, type, tableName)

param type

QVariant::Type

param other

QSqlField

param fieldName

unicode

param tableName

unicode

构造副本为 other .

Constructs an empty field called fieldName of variant type type .

这是重载函数。

Constructs an empty field called fieldName of variant type type in table .

PySide2.QtSql.QSqlField. RequiredStatus

Specifies whether the field is required or optional.

常量

描述

QSqlField.Required

The field must be specified when inserting records.

QSqlField.Optional

The fields doesn’t have to be specified when inserting records.

QSqlField.Unknown

The database driver couldn’t determine whether the field is required or optional.

另请参阅

requiredStatus()

PySide2.QtSql.QSqlField. clear ( )

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

PySide2.QtSql.QSqlField. defaultValue ( )
返回类型

object

Returns the field’s default value (which may be NULL).

PySide2.QtSql.QSqlField. isAutoValue ( )
返回类型

bool

返回 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 QSqlField resulting from a QSqlRecord returned from record() or primaryIndex() .

另请参阅

setAutoValue()

PySide2.QtSql.QSqlField. isGenerated ( )
返回类型

bool

返回 true if the field is generated; otherwise returns false.

PySide2.QtSql.QSqlField. isNull ( )
返回类型

bool

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

另请参阅

value()

PySide2.QtSql.QSqlField. isReadOnly ( )
返回类型

bool

返回 true if the field’s value is read-only; otherwise returns false.

PySide2.QtSql.QSqlField. isValid ( )
返回类型

bool

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

PySide2.QtSql.QSqlField. length ( )
返回类型

int

Returns the field’s length.

If the returned value is negative, it means that the information is not available from the database.

PySide2.QtSql.QSqlField. name ( )
返回类型

unicode

Returns the name of the field.

另请参阅

setName()

PySide2.QtSql.QSqlField. __ne__ ( other )
参数

other QSqlField

返回类型

bool

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

PySide2.QtSql.QSqlField. __eq__ ( other )
参数

other QSqlField

返回类型

bool

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

PySide2.QtSql.QSqlField. precision ( )
返回类型

int

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.

PySide2.QtSql.QSqlField. requiredStatus ( )
返回类型

RequiredStatus

返回 true if this is a required field; otherwise returns false . An INSERT will fail if a required field does not have a value.

PySide2.QtSql.QSqlField. setAutoValue ( autoVal )
参数

autoVal bool

Marks the field as an auto-generated value if autoVal 为 true。

另请参阅

isAutoValue()

PySide2.QtSql.QSqlField. setDefaultValue ( value )
参数

value – object

Sets the default value used for this field to value .

PySide2.QtSql.QSqlField. setGenerated ( gen )
参数

gen bool

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.

PySide2.QtSql.QSqlField. setLength ( fieldLength )
参数

fieldLength int

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.

PySide2.QtSql.QSqlField. setName ( name )
参数

name – unicode

Sets the name of the field to name .

另请参阅

name()

PySide2.QtSql.QSqlField. setPrecision ( precision )
参数

precision int

Sets the field’s precision . This only affects numeric fields.

PySide2.QtSql.QSqlField. setReadOnly ( readOnly )
参数

readOnly bool

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() .

另请参阅

isReadOnly()

PySide2.QtSql.QSqlField. setRequired ( required )
参数

required bool

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

PySide2.QtSql.QSqlField. setRequiredStatus ( status )
参数

status RequiredStatus

Sets the required status of this field to required .

PySide2.QtSql.QSqlField. setSqlType ( type )
参数

type int

PySide2.QtSql.QSqlField. setTableName ( tableName )
参数

tableName – unicode

设置 tableName of the field to table .

另请参阅

tableName()

PySide2.QtSql.QSqlField. setType ( type )
参数

type QVariant::Type

Set’s the field’s variant type to type .

PySide2.QtSql.QSqlField. setValue ( value )
参数

value – object

Sets the value of the field to value . If the field is read-only ( isReadOnly() 返回 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() .

PySide2.QtSql.QSqlField. tableName ( )
返回类型

unicode

Returns the 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 record() of a forward-only query.

另请参阅

setTableName()

PySide2.QtSql.QSqlField. type ( )
返回类型

QVariant::Type

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.

另请参阅

setType()

PySide2.QtSql.QSqlField. typeID ( )
返回类型

int

Returns the type ID for the field.

If the returned value is negative, it means that the information is not available from the database.

PySide2.QtSql.QSqlField. value ( )
返回类型

object

Returns the value of the field as a QVariant .

使用 isNull() to check if the field’s value is NULL.

另请参阅

setValue()