QSqlField

The QSqlField class manipulates the fields in SQL database tables and views. More

Inheritance diagram of PySide6.QtSql.QSqlField

Synopsis

Functions

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:

field = QSqlField("age", QMetaType.fromType())
    field.setValue(QPixmap()) # WRONG

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

field = QSqlField("age", QMetaType.fromType())
    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:

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

See also

QSqlRecord

class PySide6.QtSql.QSqlField(other)

PySide6.QtSql.QSqlField([fieldName=””[, type=QMetaType()[, tableName=””]]])

Parameters

Constructs a copy of other.

This is an overloaded function.

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

PySide6.QtSql.QSqlField.RequiredStatus

Specifies whether the field is required or optional.

Constant

Description

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.

See also

requiredStatus()

PySide6.QtSql.QSqlField.clear()

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

PySide6.QtSql.QSqlField.defaultValue()
Return type

object

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

PySide6.QtSql.QSqlField.isAutoValue()
Return type

bool

Returns true if the value is auto-generated by the database, for example auto-increment primary key values.

Note

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 record() or primaryIndex() .

See also

setAutoValue()

PySide6.QtSql.QSqlField.isGenerated()
Return type

bool

Returns true if the field is generated; otherwise returns false.

PySide6.QtSql.QSqlField.isNull()
Return type

bool

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

See also

value()

PySide6.QtSql.QSqlField.isReadOnly()
Return type

bool

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

PySide6.QtSql.QSqlField.isValid()
Return type

bool

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

PySide6.QtSql.QSqlField.length()
Return type

int

Returns the field’s length.

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

PySide6.QtSql.QSqlField.metaType()
Return type

PySide6.QtCore.QMetaType

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

setMetaType()

PySide6.QtSql.QSqlField.name()
Return type

str

Returns the name of the field.

See also

setName()

PySide6.QtSql.QSqlField.__ne__(other)
Parameters

otherPySide6.QtSql.QSqlField

Return type

bool

Returns true if the field is unequal to other; otherwise returns false.

PySide6.QtSql.QSqlField.__eq__(other)
Parameters

otherPySide6.QtSql.QSqlField

Return type

bool

Returns true if the field is equal to other; otherwise returns false.

PySide6.QtSql.QSqlField.precision()
Return type

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.

PySide6.QtSql.QSqlField.requiredStatus()
Return type

RequiredStatus

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

PySide6.QtSql.QSqlField.setAutoValue(autoVal)
Parameters

autoVal – bool

Marks the field as an auto-generated value if autoVal is true.

See also

isAutoValue()

PySide6.QtSql.QSqlField.setDefaultValue(value)
Parameters

value – object

Sets the default value used for this field to value.

PySide6.QtSql.QSqlField.setGenerated(gen)
Parameters

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.

PySide6.QtSql.QSqlField.setLength(fieldLength)
Parameters

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.

PySide6.QtSql.QSqlField.setMetaType(type)
Parameters

typePySide6.QtCore.QMetaType

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

PySide6.QtSql.QSqlField.setName(name)
Parameters

name – str

Sets the name of the field to name.

See also

name()

PySide6.QtSql.QSqlField.setPrecision(precision)
Parameters

precision – int

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

PySide6.QtSql.QSqlField.setReadOnly(readOnly)
Parameters

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

See also

isReadOnly()

PySide6.QtSql.QSqlField.setRequired(required)
Parameters

required – bool

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

PySide6.QtSql.QSqlField.setRequiredStatus(status)
Parameters

statusRequiredStatus

Sets the required status of this field to required.

PySide6.QtSql.QSqlField.setSqlType(type)
Parameters

type – int

PySide6.QtSql.QSqlField.setTableName(tableName)
Parameters

tableName – str

Sets the tableName of the field to table.

See also

tableName()

PySide6.QtSql.QSqlField.setValue(value)
Parameters

value – object

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

PySide6.QtSql.QSqlField.tableName()
Return type

str

Returns the of the field.

Note

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.

See also

setTableName()

PySide6.QtSql.QSqlField.typeID()
Return type

int

PySide6.QtSql.QSqlField.value()
Return type

object

Returns the value of the field as a QVariant .

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

See also

setValue()