Table Of Contents

Previous topic

QSqlIndex

Next topic

QSqlError

QSqlField

Inheritance diagram of QSqlField

Synopsis

Functions

Detailed Description

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

PySide.QtSql.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(QString("123"))  # casts QString to int

PySide.QtSql.QSqlField objects are rarely created explicitly in application code. They are usually accessed indirectly through PySide.QtSql.QSqlRecord s that already contain a list of fields. For example:

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

A PySide.QtSql.QSqlField object can provide some meta-data about the field, for example, its PySide.QtSql.QSqlField.name() , variant PySide.QtSql.QSqlField.type() , PySide.QtSql.QSqlField.length() , PySide.QtSql.QSqlField.precision() , PySide.QtSql.QSqlField.defaultValue() , PySide.QtSql.QSqlField.typeID() , and its PySide.QtSql.QSqlField.requiredStatus() , PySide.QtSql.QSqlField.isGenerated() and PySide.QtSql.QSqlField.isReadOnly() . The field’s data can be checked to see if it PySide.QtSql.QSqlField.isNull() , and its PySide.QtSql.QSqlField.value() retrieved. When editing the data can be set with PySide.QtSql.QSqlField.setValue() or set to NULL with PySide.QtSql.QSqlField.clear() .

class PySide.QtSql.QSqlField(other)
class PySide.QtSql.QSqlField([fieldName=""[, type=QVariant.Invalid]])
Parameters:

Constructs a copy of other .

PySide.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.
PySide.QtSql.QSqlField.clear()

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

PySide.QtSql.QSqlField.defaultValue()
Return type:object

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

PySide.QtSql.QSqlField.isAutoValue()
Return type:PySide.QtCore.bool

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

PySide.QtSql.QSqlField.isGenerated()
Return type:PySide.QtCore.bool

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

PySide.QtSql.QSqlField.isNull()
Return type:PySide.QtCore.bool

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

PySide.QtSql.QSqlField.isReadOnly()
Return type:PySide.QtCore.bool

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

PySide.QtSql.QSqlField.isValid()
Return type:PySide.QtCore.bool

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

PySide.QtSql.QSqlField.length()
Return type:PySide.QtCore.int

Returns the field’s length.

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

PySide.QtSql.QSqlField.name()
Return type:unicode

Returns the name of the field.

PySide.QtSql.QSqlField.__ne__(other)
Parameters:otherPySide.QtSql.QSqlField
Return type:PySide.QtCore.bool

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

PySide.QtSql.QSqlField.__eq__(other)
Parameters:otherPySide.QtSql.QSqlField
Return type:PySide.QtCore.bool

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

PySide.QtSql.QSqlField.precision()
Return type:PySide.QtCore.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.

PySide.QtSql.QSqlField.requiredStatus()
Return type:PySide.QtSql.QSqlField.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.

PySide.QtSql.QSqlField.setAutoValue(autoVal)
Parameters:autoValPySide.QtCore.bool

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

PySide.QtSql.QSqlField.setDefaultValue(value)
Parameters:value – object

Sets the default value used for this field to value .

PySide.QtSql.QSqlField.setGenerated(gen)
Parameters:genPySide.QtCore.bool

Sets the generated state. If gen is false, no SQL will be generated for this field; otherwise, Qt classes such as PySide.QtSql.QSqlQueryModel and PySide.QtSql.QSqlTableModel will generate SQL for this field.

PySide.QtSql.QSqlField.setLength(fieldLength)
Parameters:fieldLengthPySide.QtCore.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.

PySide.QtSql.QSqlField.setName(name)
Parameters:name – unicode

Sets the name of the field to name .

PySide.QtSql.QSqlField.setPrecision(precision)
Parameters:precisionPySide.QtCore.int

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

PySide.QtSql.QSqlField.setReadOnly(readOnly)
Parameters:readOnlyPySide.QtCore.bool

Sets the read only flag of the field’s value to readOnly . A read-only field cannot have its value set with PySide.QtSql.QSqlField.setValue() and cannot be cleared to NULL with PySide.QtSql.QSqlField.clear() .

PySide.QtSql.QSqlField.setRequired(required)
Parameters:requiredPySide.QtCore.bool

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

PySide.QtSql.QSqlField.setRequiredStatus(status)
Parameters:statusPySide.QtSql.QSqlField.RequiredStatus

Sets the required status of this field to required .

PySide.QtSql.QSqlField.setSqlType(type)
Parameters:typePySide.QtCore.int
PySide.QtSql.QSqlField.setType(type)
Parameters:typePySide.QtCore.QVariant::Type
PySide.QtSql.QSqlField.setValue(value)
Parameters:value – object

Sets the value of the field to value . If the field is read-only ( PySide.QtSql.QSqlField.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 PySide.QtCore.QString to an integer data type.

To set the value to NULL, use PySide.QtSql.QSqlField.clear() .

PySide.QtSql.QSqlField.type()
Return type:PySide.QtCore.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.

PySide.QtSql.QSqlField.typeID()
Return type:PySide.QtCore.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.

PySide.QtSql.QSqlField.value()
Return type:object

Returns the value of the field as a PySide.QtCore.QVariant .

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