Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QSqlField Class

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

Header:#include <QSqlField>

Public Types

enumRequiredStatus { Required, Optional, Unknown }

Public Functions

QSqlField(const QString & fieldName = QString(), QVariant::Type type = QVariant::Invalid)
QSqlField(const QSqlField & other)
~QSqlField()
voidclear()
QVariantdefaultValue() const
boolisAutoValue() const
boolisGenerated() const
boolisNull() const
boolisReadOnly() const
boolisValid() const
intlength() const
QStringname() const
intprecision() const
RequiredStatusrequiredStatus() const
voidsetAutoValue(bool autoVal)
voidsetDefaultValue(const QVariant & value)
voidsetGenerated(bool gen)
voidsetLength(int fieldLength)
voidsetName(const QString & name)
voidsetPrecision(int precision)
voidsetReadOnly(bool readOnly)
voidsetRequired(bool required)
voidsetRequiredStatus(RequiredStatus required)
voidsetType(QVariant::Type type)
voidsetValue(const QVariant & value)
QVariant::Typetype() const
QVariantvalue() const
booloperator!=(const QSqlField & other) const
QSqlField &operator=(const QSqlField & other)
booloperator==(const QSqlField & other) const

Detailed Description

TheQSqlField 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 asQVariants. 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 throughQSqlRecords that already contain a list of fields. For example:

QSqlQuery query;    ...QSqlRecord record= query.record();QSqlField field= record.field("country");

AQSqlField object can provide some meta-data about the field, for example, itsname(), varianttype(),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 alsoQSqlRecord.

Member Type Documentation

enum QSqlField::RequiredStatus

Specifies whether the field is required or optional.

ConstantValueDescription
QSqlField::Required1The field must be specified when inserting records.
QSqlField::Optional0The fields doesn't have to be specified when inserting records.
QSqlField::Unknown-1The database driver couldn't determine whether the field is required or optional.

See alsorequiredStatus().

Member Function Documentation

QSqlField::QSqlField(constQString & fieldName = QString(),QVariant::Type type = QVariant::Invalid)

Constructs an empty field calledfieldName of variant typetype.

See alsosetRequiredStatus(),setLength(),setPrecision(),setDefaultValue(),setGenerated(), andsetReadOnly().

QSqlField::QSqlField(constQSqlField & other)

Constructs a copy ofother.

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

QVariant QSqlField::defaultValue() const

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

See alsosetDefaultValue(),type(),requiredStatus(),length(),precision(), andisGenerated().

bool QSqlField::isAutoValue() const

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

See alsosetAutoValue().

bool QSqlField::isGenerated() const

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

See alsosetGenerated(),type(),requiredStatus(),length(),precision(), anddefaultValue().

bool QSqlField::isNull() const

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

See alsovalue().

bool QSqlField::isReadOnly() const

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

See alsosetReadOnly(),type(),requiredStatus(),length(),precision(),defaultValue(), andisGenerated().

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.

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

See alsosetLength(),type(),requiredStatus(),precision(),defaultValue(), andisGenerated().

QString QSqlField::name() const

Returns the name of the field.

See alsosetName().

int QSqlField::precision() const

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.

See alsosetPrecision(),type(),requiredStatus(),length(),defaultValue(), andisGenerated().

RequiredStatus QSqlField::requiredStatus() const

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

See alsosetRequiredStatus(),type(),length(),precision(),defaultValue(), andisGenerated().

void QSqlField::setAutoValue(bool autoVal)

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

See alsoisAutoValue().

void QSqlField::setDefaultValue(constQVariant & value)

Sets the default value used for this field tovalue.

See alsodefaultValue(),value(),setType(),setRequiredStatus(),setLength(),setPrecision(),setGenerated(), andsetReadOnly().

void QSqlField::setGenerated(bool gen)

Sets the generated state. Ifgen is false, no SQL will be generated for this field; otherwise, Qt classes such asQSqlQueryModel andQSqlTableModel will generate SQL for this field.

See alsoisGenerated(),setType(),setRequiredStatus(),setLength(),setPrecision(),setDefaultValue(), andsetReadOnly().

void QSqlField::setLength(int fieldLength)

Sets the field's length tofieldLength. For strings this is the maximum number of characters the string can hold; the meaning varies for other types.

See alsolength(),setType(),setRequiredStatus(),setPrecision(),setDefaultValue(),setGenerated(), andsetReadOnly().

void QSqlField::setName(constQString & name)

Sets the name of the field toname.

See alsoname().

void QSqlField::setPrecision(int precision)

Sets the field'sprecision. This only affects numeric fields.

See alsoprecision(),setType(),setRequiredStatus(),setLength(),setDefaultValue(),setGenerated(), andsetReadOnly().

void QSqlField::setReadOnly(bool readOnly)

Sets the read only flag of the field's value toreadOnly. A read-only field cannot have its value set withsetValue() and cannot be cleared to NULL withclear().

See alsoisReadOnly().

void QSqlField::setRequired(bool required)

Sets the required status of this field toRequired ifrequired is true; otherwise sets it toOptional.

See alsosetRequiredStatus() andrequiredStatus().

void QSqlField::setRequiredStatus(RequiredStatus required)

Sets the required status of this field torequired.

See alsorequiredStatus(),setType(),setLength(),setPrecision(),setDefaultValue(),setGenerated(), andsetReadOnly().

void QSqlField::setType(QVariant::Type type)

Set's the field's variant type totype.

See alsotype(),setRequiredStatus(),setLength(),setPrecision(),setDefaultValue(),setGenerated(), andsetReadOnly().

void QSqlField::setValue(constQVariant & value)

Sets the value of the field tovalue. If the field is read-only (isReadOnly() returns true), nothing happens.

If the data type ofvalue 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. aQString to an integer data type.

To set the value to NULL, useclear().

See alsovalue(),isReadOnly(), anddefaultValue().

QVariant::Type QSqlField::type() const

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

QVariant QSqlField::value() const

Returns the value of the field as aQVariant.

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

See alsosetValue().

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

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

QSqlField & QSqlField::operator=(constQSqlField & other)

Sets the field equal toother.

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

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

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp