QSqlField 类操纵 SQL 数据库表和视图中的字段。 更多...
头: | #include <QSqlField> |
CMake: |
find_package(Qt6 COMPONENTS Sql REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Sql) |
qmake: | QT += sql |
enum | RequiredStatus { Required, Optional, Unknown } |
QSqlField (const QSqlField & other ) | |
QSqlField (const QString & fieldName = QString(), QMetaType type = QMetaType(), const QString & table = QString()) | |
QSqlField & | operator= (const QSqlField & other ) |
~QSqlField () | |
void | clear () |
QVariant | defaultValue () const |
bool | isAutoValue () const |
bool | isGenerated () const |
bool | isNull () const |
bool | isReadOnly () const |
bool | isValid () const |
int | length () const |
QMetaType | metaType () const |
QString | name () const |
int | precision () const |
QSqlField::RequiredStatus | requiredStatus () const |
void | setAutoValue (bool autoVal ) |
void | setDefaultValue (const QVariant & value ) |
void | setGenerated (bool gen ) |
void | setLength (int fieldLength ) |
void | setMetaType (QMetaType type ) |
void | setName (const QString & name ) |
void | setPrecision (int precision ) |
void | setReadOnly (bool readOnly ) |
void | setRequired (bool required ) |
void | setRequiredStatus (QSqlField::RequiredStatus required ) |
void | setTableName (const QString & table ) |
void | setValue (const QVariant & value ) |
QString | tableName () const |
QVariant | value () const |
bool | operator!= (const QSqlField & other ) const |
bool | operator== (const QSqlField & other ) const |
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", QMetaType::fromType<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", QMetaType::fromType<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 () or set to NULL with clear ().
另请参阅 QSqlRecord .
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 ().
构造副本为 other .
[since 6.0]
QSqlField::
QSqlField
(const
QString
&
fieldName
= QString(),
QMetaType
type
= QMetaType(), const
QString
&
table
= QString())
这是重载函数。
Constructs an empty field called fieldName 类型 type in table .
该函数在 Qt 6.0 引入。
另请参阅 setRequiredStatus (), setLength (), setPrecision (), setDefaultValue (), setGenerated (),和 setReadOnly ().
Sets the field equal to other .
销毁对象并释放任何分配资源。
Clears the value of the field and sets it to NULL. If the field is read-only, nothing happens.
另请参阅 setValue (), isReadOnly (),和 requiredStatus ().
Returns the field's default value (which may be NULL).
另请参阅 setDefaultValue (), metaType (), requiredStatus (), length (), precision (),和 isGenerated ().
返回
true
if the value is auto-generated by the database, for example auto-increment primary key values.
注意:
When using the ODBC driver, due to limitations in the ODBC API, the
isAutoValue()
field is only populated in a
QSqlField
resulting from a
QSqlRecord
obtained by executing a
SELECT
query. It is
false
在
QSqlField
resulting from a
QSqlRecord
returned from
QSqlDatabase::record
() 或
QSqlDatabase::primaryIndex
().
另请参阅 setAutoValue ().
返回
true
if the field is generated; otherwise returns false.
另请参阅 setGenerated (), metaType (), requiredStatus (), length (), precision (),和 defaultValue ().
返回
true
if the field's value is NULL; otherwise returns false.
另请参阅 value ().
返回
true
if the field's value is read-only; otherwise returns false.
另请参阅 setReadOnly (), metaType (), requiredStatus (), length (), precision (), defaultValue (),和 isGenerated ().
返回
true
if the field's variant type is valid; otherwise returns
false
.
Returns the field's length.
If the returned value is negative, it means that the information is not available from the database.
另请参阅 setLength (), metaType (), requiredStatus (), precision (), defaultValue (),和 isGenerated ().
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.
另请参阅 setMetaType ().
Returns the name of the field.
另请参阅 setName ().
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 (), metaType (), requiredStatus (), length (), defaultValue (),和 isGenerated ().
返回
true
if this is a required field; otherwise returns
false
. An
INSERT
will fail if a required field does not have a value.
另请参阅 setRequiredStatus (), metaType (), length (), precision (), defaultValue (),和 isGenerated ().
Marks the field as an auto-generated value if autoVal 为 true。
另请参阅 isAutoValue ().
Sets the default value used for this field to value .
另请参阅 defaultValue (), value (), setMetaType (), setRequiredStatus (), setLength (), setPrecision (), setGenerated (),和 setReadOnly ().
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 (), setMetaType (), setRequiredStatus (), setLength (), setPrecision (), setDefaultValue (),和 setReadOnly ().
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 (), setMetaType (), setRequiredStatus (), setPrecision (), setDefaultValue (), setGenerated (),和 setReadOnly ().
Set's the field's variant type to type .
另请参阅 metaType (), setRequiredStatus (), setLength (), setPrecision (), setDefaultValue (), setGenerated (),和 setReadOnly ().
Sets the name of the field to name .
另请参阅 name ().
Sets the field's precision . This only affects numeric fields.
另请参阅 precision (), setMetaType (), setRequiredStatus (), setLength (), setDefaultValue (), setGenerated (),和 setReadOnly ().
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 ().
Sets the required status of this field to Required if required is true; otherwise sets it to 可选 .
另请参阅 setRequiredStatus () 和 requiredStatus ().
Sets the required status of this field to required .
另请参阅 requiredStatus (), setMetaType (), setLength (), setPrecision (), setDefaultValue (), setGenerated (),和 setReadOnly ().
设置 tableName of the field to table .
另请参阅 tableName ().
Sets the value of the field to
value
. If the field is read-only (
isReadOnly
() 返回
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 ().
Returns the tableName of the field.
注意:
When using the QPSQL driver, due to limitations in the libpq library, the
tableName()
field is not populated in a
QSqlField
resulting from a
QSqlRecord
obtained by
QSqlQuery::record
() of a forward-only query.
另请参阅 setTableName ().
Returns the value of the field as a QVariant .
使用 isNull () to check if the field's value is NULL.
另请参阅 setValue ().
返回
true
if the field is unequal to
other
;否则返回 false。
返回
true
if the field is equal to
other
;否则返回 false。