Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QSqlField Class Reference

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

    #include <QSqlField>

- List of all members, including inherited members
- Compatibility members

### Public Types

- enum RequiredStatus { Required, Optional, Unknown }

### Public Functions

- QSqlField ( const QString & fieldName = QString(), QCoreVariant::Type type = QCoreVariant::Invalid )
- QSqlField ( const QSqlField & other )
- ~QSqlField ()
- void clear ()
- QCoreVariant defaultValue () const
- bool isAutoGenerated () const
- bool isNull () const
- bool isReadOnly () const
- bool isValid () const
- int length () const
- QString name () const
- int precision () const
- RequiredStatus requiredStatus () const
- void setAutoGenerated ( bool autoGen )
- void setDefaultValue ( const QCoreVariant & value )
- void setLength ( int fieldLength )
- void setName ( const QString & name )
- void setPrecision ( int precision )
- void setReadOnly ( bool readOnly )
- void setRequired ( bool required )
- void setRequiredStatus ( RequiredStatus required )
- void setType ( QCoreVariant::Type type )
- void setValue ( const QCoreVariant & value )
- QCoreVariant::Type type () const
- QCoreVariant value () const
- bool operator!= ( const QSqlField & other ) const
- QSqlField & operator= ( const QSqlField & other )
- bool operator== ( const QSqlField & other ) const

## Detailed Description

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 QCoreVariants. Using an incompatible type is not permitted. For example:

            QSqlField field("age", QCoreVariant::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", QCoreVariant::Int);
            field.setValue(QString("123"));  // casts QString to int

QSqlField objects are rarely created explicitly in application code. They are usually accessed indirectly through QSqlRecords 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(), isAutoGenerated() 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.

## Member Type Documentation

### enum 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.

See also requiredStatus().

## Member Function Documentation

### QSqlField::QSqlField ( const QString & fieldName = QString(), QCoreVariant::Type type = QCoreVariant::Invalid )

Constructs an empty field called fieldName of variant type type.

See also setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setAutoGenerated(), and setReadOnly().

### QSqlField::QSqlField ( const QSqlField & other )

Constructs a copy of other.

### QSqlField::~QSqlField ()

Destroys the object and frees any allocated resources.

### void QSqlField::clear ()

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

See also setValue(), isReadOnly(), and requiredStatus().

### QCoreVariant QSqlField::defaultValue () const

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

See also setDefaultValue(), type(), requiredStatus(), length(), precision(), and isAutoGenerated().

### bool QSqlField::isAutoGenerated () const

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

See also setAutoGenerated(), type(), requiredStatus(), length(), precision(), and defaultValue().

### bool QSqlField::isNull () const

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

See also value().

### bool QSqlField::isReadOnly () const

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

See also setReadOnly(), type(), requiredStatus(), length(), precision(), defaultValue(), and isAutoGenerated().

### bool QSqlField::isValid () const

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

### int QSqlField::length () const

Returns the field's length.

See also setLength(), type(), requiredStatus(), precision(), defaultValue(), and isAutoGenerated().

### QString QSqlField::name () const

Returns the name of the field.

See also setName().

### int QSqlField::precision () const

Returns the field's precision; this is only meaningful for numeric types.

See also setPrecision(), type(), requiredStatus(), length(), defaultValue(), and isAutoGenerated().

### RequiredStatus QSqlField::requiredStatus () const

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

See also setRequiredStatus(), type(), length(), precision(), defaultValue(), and isAutoGenerated().

### void QSqlField::setAutoGenerated ( bool autoGen )

Sets the auto-generated state. If autoGen is true, no SQL will be generated for this field; otherwise, Qt classes such as QSqlQueryModel and QSqlTableModel will generate SQL for this field.

See also isAutoGenerated(), setType(), setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), and setReadOnly().

### void QSqlField::setDefaultValue ( const QCoreVariant & value )

Sets the default value used for this field to value.

See also defaultValue(), value(), setType(), setRequiredStatus(), setLength(), setPrecision(), setAutoGenerated(), and setReadOnly().

### void QSqlField::setLength ( 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.

See also length(), setType(), setRequiredStatus(), setPrecision(), setDefaultValue(), setAutoGenerated(), and setReadOnly().

### void QSqlField::setName ( const QString & name )

Sets the name of the field to name.

See also name().

### void QSqlField::setPrecision ( int precision )

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

See also precision(), setType(), setRequiredStatus(), setLength(), setDefaultValue(), setAutoGenerated(), and setReadOnly().

### void QSqlField::setReadOnly ( 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().

### void QSqlField::setRequired ( bool required )

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

See also setRequiredStatus() and requiredStatus().

### void QSqlField::setRequiredStatus ( RequiredStatus required )

Sets the required status of this field to required.

See also requiredStatus(), setType(), setLength(), setPrecision(), setDefaultValue(), setAutoGenerated(), and setReadOnly().

### void QSqlField::setType ( QCoreVariant::Type type )

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

See also type(), setRequiredStatus(), setLength(), setPrecision(), setDefaultValue(), setAutoGenerated(), and setReadOnly().

### void QSqlField::setValue ( const QCoreVariant & 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().

See also value(), isReadOnly(), and defaultValue().

### QCoreVariant::Type QSqlField::type () const

Returns the field's variant type.

See also setType().

### QCoreVariant QSqlField::value () const

Returns the value of the field as a QCoreVariant.

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

### bool QSqlField::operator!= ( const QSqlField & other ) const

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

### QSqlField & QSqlField::operator= ( const QSqlField & other )

Sets the field equal to other.

### bool QSqlField::operator== ( const QSqlField & other ) const

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