QSqlField Class Reference

[ QtSql module]

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

类型

方法

Special Methods


详细描述

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

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", QVariant.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", QVariant.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 (), and its value () retrieved. When editing the data can be set with setValue () 或 set to NULL with clear ().


类型文档编制

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


方法文档编制

QSqlField.__init__ ( self , QString  fieldName  = QString(), Type   type  = QVariant.Invalid)

Constructs an empty field called fieldName of variant type type .

另请参阅 setRequiredStatus (), setLength (), setPrecision (), setDefaultValue (), setGenerated (),和 setReadOnly ().

QSqlField.__init__ ( self , QSqlField   other )

构造副本为 other .

QSqlField.clear ( self )

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

另请参阅 setValue (), isReadOnly (),和 requiredStatus ().

QVariant QSqlField.defaultValue ( self )

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

另请参阅 setDefaultValue (), type (), requiredStatus (), length (), precision (),和 isGenerated ().

bool QSqlField.isAutoValue ( self )

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

另请参阅 setAutoValue ().

bool QSqlField.isGenerated ( self )

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

另请参阅 setGenerated (), type (), requiredStatus (), length (), precision (),和 defaultValue ().

bool QSqlField.isNull ( self )

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

另请参阅 value ().

bool QSqlField.isReadOnly ( self )

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

另请参阅 setReadOnly (), type (), requiredStatus (), length (), precision (), defaultValue (),和 isGenerated ().

bool QSqlField.isValid ( self )

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

int QSqlField.length ( self )

Returns the field's length.

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

另请参阅 setLength (), type (), requiredStatus (), precision (), defaultValue (),和 isGenerated ().

QString QSqlField.name ( self )

Returns the name of the field.

另请参阅 setName ().

int QSqlField.precision ( self )

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.

另请参阅 setPrecision (), type (), requiredStatus (), length (), defaultValue (),和 isGenerated ().

RequiredStatus QSqlField.requiredStatus ( self )

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

另请参阅 setRequiredStatus (), type (), length (), precision (), defaultValue (),和 isGenerated ().

QSqlField.setAutoValue ( self , bool  autoVal )

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

另请参阅 isAutoValue ().

QSqlField.setDefaultValue ( self , QVariant  value )

Sets the default value used for this field to value .

另请参阅 defaultValue (), value (), setType (), setRequiredStatus (), setLength (), setPrecision (), setGenerated (),和 setReadOnly ().

QSqlField.setGenerated ( self , bool  gen )

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.

另请参阅 isGenerated (), setType (), setRequiredStatus (), setLength (), setPrecision (), setDefaultValue (),和 setReadOnly ().

QSqlField.setLength ( self , int  fieldLength )

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.

另请参阅 length (), setType (), setRequiredStatus (), setPrecision (), setDefaultValue (), setGenerated (),和 setReadOnly ().

QSqlField.setName ( self , QString  name )

Sets the name of the field to name .

另请参阅 name ().

QSqlField.setPrecision ( self , int  precision )

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

另请参阅 precision (), setType (), setRequiredStatus (), setLength (), setDefaultValue (), setGenerated (),和 setReadOnly ().

QSqlField.setReadOnly ( self , bool  readOnly )

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

QSqlField.setRequired ( self , bool  required )

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

另请参阅 setRequiredStatus () 和 requiredStatus ().

QSqlField.setRequiredStatus ( self , RequiredStatus   status )

Sets the required status of this field to required .

另请参阅 requiredStatus (), setType (), setLength (), setPrecision (), setDefaultValue (), setGenerated (),和 setReadOnly ().

QSqlField.setSqlType ( self , int  type )

QSqlField.setType ( self , Type   type )

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

另请参阅 type (), setRequiredStatus (), setLength (), setPrecision (), setDefaultValue (), setGenerated (),和 setReadOnly ().

QSqlField.setValue ( self , QVariant  value )

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

另请参阅 value (), isReadOnly (),和 defaultValue ().

Type QSqlField.type ( self )

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

int QSqlField.typeID ( self )

QVariant QSqlField.value ( self )

Returns the value of the field as a QVariant .

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

另请参阅 setValue ().

bool QSqlField.__eq__ ( self , QSqlField   other )

bool QSqlField.__ne__ ( self , QSqlField   other )