- class QSqlField#
The
QSqlField
class manipulates the fields in SQL database tables and views. More…Synopsis#
Methods#
def
__init__()
def
clear()
def
defaultValue()
def
isAutoValue()
def
isGenerated()
def
isNull()
def
isReadOnly()
def
isValid()
def
length()
def
metaType()
def
name()
def
__ne__()
def
__eq__()
def
precision()
def
requiredStatus()
def
setAutoValue()
def
setGenerated()
def
setLength()
def
setMetaType()
def
setName()
def
setPrecision()
def
setReadOnly()
def
setRequired()
def
setSqlType()
def
setTableName()
def
setValue()
def
swap()
def
tableName()
def
typeID()
def
value()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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 throughQSqlRecord
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, itsname()
, variant type(),length()
,precision()
,defaultValue()
, typeID(), and itsrequiredStatus()
,isGenerated()
andisReadOnly()
. The field’s data can be checked to see if itisNull()
, and itsvalue()
retrieved. When editing the data can be set withsetValue()
or set to NULL withclear()
.See also
- class 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
- __init__([fieldName=""[, type=QMetaType()[, tableName=""]]])#
- Parameters:
fieldName – str
type –
QMetaType
tableName – str
This is an overloaded function.
Constructs an empty field called
fieldName
of typetype
intable
.- __init__(other)
- Parameters:
other –
QSqlField
Constructs a copy of
other
.- clear()#
Clears the value of the field and sets it to NULL. If the field is read-only, nothing happens.
See also
- defaultValue()#
- Return type:
object
Returns the field’s default value (which may be NULL).
- 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 aQSqlField
resulting from aQSqlRecord
obtained by executing aSELECT
query. It isfalse
in aQSqlField
resulting from aQSqlRecord
returned fromrecord()
orprimaryIndex()
.See also
- isGenerated()#
- Return type:
bool
Returns
true
if the field is generated; otherwise returns false.- isNull()#
- Return type:
bool
Returns
true
if the field’s value is NULL; otherwise returns false.See also
- isReadOnly()#
- Return type:
bool
Returns
true
if the field’s value is read-only; otherwise returns false.- isValid()#
- Return type:
bool
Returns
true
if the field’s variant type is valid; otherwise returnsfalse
.- 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.
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
- name()#
- Return type:
str
Returns the name of the field.
See also
Returns
true
if the field is unequal toother
; otherwise returns false.Returns
true
if the field is equal toother
; otherwise returns false.- 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.
- requiredStatus()#
- Return type:
Returns
true
if this is a required field; otherwise returnsfalse
. AnINSERT
will fail if a required field does not have a value.- setAutoValue(autoVal)#
- Parameters:
autoVal – bool
Marks the field as an auto-generated value if
autoVal
is true.See also
- setDefaultValue(value)#
- Parameters:
value – object
Sets the default value used for this field to
value
.- 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 asQSqlQueryModel
andQSqlTableModel
will generate SQL for this field.- 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.Set’s the field’s variant type to
type
.- setName(name)#
- Parameters:
name – str
Sets the name of the field to
name
.See also
- setPrecision(precision)#
- Parameters:
precision – int
Sets the field’s
precision
. This only affects numeric fields.- 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 withsetValue()
and cannot be cleared to NULL withclear()
.See also
- setRequired(required)#
- Parameters:
required – bool
Sets the required status of this field to
Required
ifrequired
is true; otherwise sets it toOptional
.See also
- setRequiredStatus(status)#
- Parameters:
status –
RequiredStatus
Sets the required status of this field to
required
.- setSqlType(type)#
- Parameters:
type – int
- setTableName(tableName)#
- Parameters:
tableName – str
Sets the
tableName
of the field totable
.See also
- setValue(value)#
- Parameters:
value – object
Sets the value of the field to
value
. If the field is read-only (isReadOnly()
returnstrue
), 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()
.See also
Swaps this field with
other
. This function is very fast and never fails.- tableName()#
- Return type:
str
Returns the tableName of the field.
Note
When using the QPSQL driver, due to limitations in the libpq library, the
tableName()
field is not populated in aQSqlField
resulting from aQSqlRecord
obtained byrecord()
of a forward-only query.See also
- typeID()#
- Return type:
int
- 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