QSqlRecord Class
QSqlRecord 类封装了一条数据库记录。更多
头文件: | #include <QSqlRecord> |
CMake.QSqlRecord | find_package(Qt6 REQUIRED COMPONENTS Sql) target_link_libraries(mytarget PRIVATE Qt6::Sql) |
qmake: | QT += sql |
继承于 |
- 所有成员(包括继承成员)的列表
- QSqlRecord 属于数据库类和隐式共享类。
公共函数
QSqlRecord() | |
QSqlRecord(const QSqlRecord &other) | |
(since 6.6) | QSqlRecord(QSqlRecord &&other) |
~QSqlRecord() | |
void | append(const QSqlField &field) |
void | clear() |
void | clearValues() |
bool | contains(QAnyStringView name) const |
int | count() const |
QSqlField | field(int index) const |
QSqlField | field(QAnyStringView name) const |
QString | fieldName(int index) const |
int | indexOf(QAnyStringView name) const |
void | insert(int pos, const QSqlField &field) |
bool | isEmpty() const |
bool | isGenerated(int index) const |
bool | isGenerated(QAnyStringView name) const |
bool | isNull(int index) const |
bool | isNull(QAnyStringView name) const |
QSqlRecord | keyValues(const QSqlRecord &keyFields) const |
void | remove(int pos) |
void | replace(int pos, const QSqlField &field) |
void | setGenerated(QAnyStringView name, bool generated) |
void | setGenerated(int index, bool generated) |
void | setNull(int index) |
void | setNull(QAnyStringView name) |
void | setValue(int index, const QVariant &val) |
void | setValue(QAnyStringView name, const QVariant &val) |
(since 6.6) void | swap(QSqlRecord &other) |
QVariant | value(int index) const |
QVariant | value(QAnyStringView name) const |
bool | operator!=(const QSqlRecord &other) const |
(since 6.6) QSqlRecord & | operator=(QSqlRecord &&other) |
QSqlRecord & | operator=(const QSqlRecord &other) |
bool | operator==(const QSqlRecord &other) const |
详细说明
QSqlRecord 类封装了数据库记录(通常是数据库中表或视图中的一行)的功能和特征。QSqlRecord 支持添加和删除字段,以及设置和检索字段值。
记录字段的值可以使用setValue() 按名称或位置设置;如果要将字段设置为空,请使用setNull()。要按名称查找字段的位置,请使用indexOf() ;要查找特定位置的字段名称,请使用fieldName() 。使用field() 检索给定字段的QSqlField 对象。使用contains() 查看记录是否包含特定字段名。
当生成要在数据库中执行的查询时,只有isGenerated() 为真的字段才会包含在生成的 SQL 中。
使用append() 或insert() 可以添加记录字段,使用replace() 可以替换记录字段,使用remove() 可以删除记录字段。所有字段都可以用clear() 删除。字段的数量由count() 提供;所有字段值都可以用clearValues() 清除(为空)。
另请参阅 QSqlField 和QSqlQuery::record()。
成员函数文档
QSqlRecord::QSqlRecord()
构造一个空记录。
另请参阅 isEmpty()、append() 和insert()。
QSqlRecord::QSqlRecord(const QSqlRecord &other)
构造other 的副本。
QSqlRecord 是隐式共享的。这意味着您可以在恒定时间内复制一条记录。
[noexcept, since 6.6]
QSqlRecord::QSqlRecord(QSqlRecord &&other)
Move-constructs a new QSqlRecord fromother 。
注意: moved-from 对象other 处于部分形成状态,其中唯一有效的操作是销毁和赋一个新值。
此函数在 Qt 6.6 中引入。
[noexcept]
QSqlRecord::~QSqlRecord()
销毁对象并释放已分配的资源。
void QSqlRecord::append(const QSqlField &field)
将字段field 的副本附加到记录末尾。
另请参阅 insert()、replace() 和remove()。
void QSqlRecord::clear()
删除记录的所有字段。
另请参阅 clearValues() 和isEmpty()。
void QSqlRecord::clearValues()
清除记录中所有字段的值,并将每个字段设置为空。
另请参阅 setValue().
bool QSqlRecord::contains(QAnyStringView name) const
如果记录中有名为name 的字段,则返回true
;否则返回false
。
注意: 在 6.8 之前的 Qt 版本中,该函数使用QString ,而不是QAnyStringView 。
int QSqlRecord::count() const
返回记录中的字段数。
另请参阅 isEmpty()。
QSqlField QSqlRecord::field(int index) const
返回位置index 的字段。如果index 不在范围内,函数将返回默认构造值。
QSqlField QSqlRecord::field(QAnyStringView name) const
这是一个重载函数。
返回名为name 的字段。如果未找到名为name 的字段,函数将返回默认构造值。
注意: 在 6.8 之前的 Qt 版本中,此函数使用QString ,而不是QAnyStringView 。
QString QSqlRecord::fieldName(int index) const
返回位置index 的字段名。如果字段不存在,则返回空字符串。
另请参阅 indexOf() 。
int QSqlRecord::indexOf(QAnyStringView name) const
返回名为name 的字段在记录中的位置,如果找不到则返回-1。字段名不区分大小写。如果有多个字段匹配,则返回第一个字段。
注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QString ,而不是QAnyStringView 。
另请参阅 fieldName() 。
void QSqlRecord::insert(int pos, const QSqlField &field)
在记录的pos 位置插入字段field 。
另请参阅 append()、replace() 和remove()。
bool QSqlRecord::isEmpty() const
如果记录中没有字段,则返回true
;否则返回false
。
另请参阅 append()、insert() 和clear()。
bool QSqlRecord::isGenerated(int index) const
如果记录在index 位置有一个字段,且该字段将被生成(默认值),则返回true
;否则返回false
。
另请参阅 setGenerated() 。
bool QSqlRecord::isGenerated(QAnyStringView name) const
这是一个重载函数。
如果记录中有名为name 的字段,且该字段将被生成,则返回true
(默认值);否则返回false
。
注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QString ,而不是QAnyStringView 。
另请参阅 setGenerated() 。
bool QSqlRecord::isNull(int index) const
如果字段index 为空或位置index 上没有字段,则返回true
;否则返回false
。
另请参阅 setNull() 。
bool QSqlRecord::isNull(QAnyStringView name) const
这是一个重载函数。
如果调用name 的字段为空或没有调用name 的字段,则返回true
;否则返回false
。
注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QString ,而不是QAnyStringView 。
另请参阅 setNull() 。
QSqlRecord QSqlRecord::keyValues(const QSqlRecord &keyFields) const
返回一条记录,其中包含keyFields 所代表的字段,并根据字段名称设置为匹配值。
void QSqlRecord::remove(int pos)
删除位置pos 上的字段。如果pos 超出范围,则不会发生任何操作。
另请参阅 append()、insert() 和replace()。
void QSqlRecord::replace(int pos, const QSqlField &field)
用给定的field 替换位置pos 的字段。如果pos 超出范围,则不会发生任何操作。
另请参阅 append()、insert() 和remove()。
void QSqlRecord::setGenerated(QAnyStringView name, bool generated)
将名为name 的字段的生成标志设置为generated 。如果字段不存在,则什么也不会发生。例如,只有generated 设置为 true 的字段才会包含在QSqlQueryModel 生成的 SQL 中。
注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QString ,而不是QAnyStringView 。
另请参阅 isGenerated() 。
void QSqlRecord::setGenerated(int index, bool generated)
将字段index 的生成标志设置为generated 。
另请参阅 isGenerated() 。
void QSqlRecord::setNull(int index)
将index 字段值设置为空。如果字段不存在,则什么也不会发生。
void QSqlRecord::setNull(QAnyStringView name)
这是一个重载函数。
它将调用name 的字段值设置为空。如果字段不存在,则什么也不会发生。
注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QString ,而不是QAnyStringView 。
void QSqlRecord::setValue(int index, const QVariant &val)
将位置index 的字段值设置为val 。如果字段不存在,则不会发生任何操作。
void QSqlRecord::setValue(QAnyStringView name, const QVariant &val)
这是一个重载函数。
将调用name 的字段值设置为val 。如果字段不存在,则不会发生任何事情。
注意: 在 6.8 之前的 Qt XML 版本中,该函数使用QString ,而不是QAnyStringView 。
另请参阅 setNull() 。
[noexcept, since 6.6]
void QSqlRecord::swap(QSqlRecord &other)
将此 SQL 记录与other 互换。该操作速度非常快,而且从未出现过故障。
此函数在 Qt 6.6 中引入。
QVariant QSqlRecord::value(int index) const
返回位于记录中index 位置的字段值。如果index 越界,则返回无效的QVariant 。
另请参阅 setValue()、fieldName() 和isNull()。
QVariant QSqlRecord::value(QAnyStringView name) const
这是一个重载函数。
返回记录中名为name 的字段的值。如果字段name 不存在,则返回无效变量。
注意: 在 6.8 之前的 Qt XML 版本中,此函数使用QString ,而不是QAnyStringView 。
bool QSqlRecord::operator!=(const QSqlRecord &other) const
如果该对象与other 不相同,则返回true
;否则返回false
。
另请参见 operator==().
[noexcept, since 6.6]
QSqlRecord &QSqlRecord::operator=(QSqlRecord &&other)
移动--将other 赋值给此QSqlRecord 实例。
注意: moved-from 对象other 处于部分形成状态,在这种状态下,唯一有效的操作是销毁和赋新值。
此函数在 Qt 6.6 中引入。
QSqlRecord &QSqlRecord::operator=(const QSqlRecord &other)
设置记录等于other 。
QSqlRecord 是隐式共享的。这意味着您可以在恒定时间内复制一条记录。
bool QSqlRecord::operator==(const QSqlRecord &other) const
如果该对象与other 相同(即字段顺序相同),则返回true
;否则返回false
。
另请参阅 operator!=() 。
© 2025 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 the GNU 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.