QSqlError Class
QSqlError 类提供了 SQL 数据库错误信息。更多内容...
| 头文件: | #include <QSqlError> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Sql) target_link_libraries(mytarget PRIVATE Qt6::Sql) |
| qmake: | QT += sql |
- 所有成员的列表,包括继承的成员
- QSqlError 属于数据库类。
公共类型
| enum | ErrorType { NoError, ConnectionError, StatementError, TransactionError, UnknownError } |
公共函数
| QSqlError(const QString &driverText = QString(), const QString &databaseText = QString(), QSqlError::ErrorType type = NoError, const QString &nativeErrorCode = QString()) | |
| QSqlError(const QSqlError &other) | |
| QSqlError(QSqlError &&other) | |
| ~QSqlError() | |
| QString | databaseText() const |
| QString | driverText() const |
| bool | isValid() const |
| QString | nativeErrorCode() const |
| void | swap(QSqlError &other) |
| QString | text() const |
| QSqlError::ErrorType | type() const |
| bool | operator!=(const QSqlError &other) const |
| QSqlError & | operator=(QSqlError &&other) |
| QSqlError & | operator=(const QSqlError &other) |
| bool | operator==(const QSqlError &other) const |
详细说明
QSqlError 对象可以提供与数据库相关的错误数据,包括driverText() 和databaseText() 消息(或将两者拼接为text()),以及nativeErrorCode() 和type()。
另请参阅 QSqlDatabase::lastError() 和QSqlQuery::lastError()。
成员类型文档
enum QSqlError::ErrorType
此枚举类型描述了错误发生的上下文,例如连接错误、语句错误等。
| 常量 | 值 | 描述 |
|---|---|---|
QSqlError::NoError | 0 | 未发生错误。 |
QSqlError::ConnectionError | 1 | 连接错误。 |
QSqlError::StatementError | 2 | SQL 语句语法错误。 |
QSqlError::TransactionError | 3 | 事务失败错误。 |
QSqlError::UnknownError | 4 | 未知错误。 |
成员函数文档
QSqlError::QSqlError(const QString &driverText = QString(), const QString &databaseText = QString(), QSqlError::ErrorType type = NoError, const QString &nativeErrorCode = QString())
构建一个包含驱动程序错误文本driverText 、数据库特定错误文本databaseText 、类型type 以及本机错误代码nativeErrorCode 的错误对象。
QSqlError::QSqlError(const QSqlError &other)
创建other 的副本。
[constexpr noexcept default] QSqlError::QSqlError(QSqlError &&other)
通过“move”操作构造一个 QSqlError 实例,使其指向与other 原本指向的同一对象。
注意: 被移动的对象 other 将处于一种部分构成的状态,在此状态下,唯一有效的操作是销毁和赋予新值。
[noexcept] QSqlError::~QSqlError()
销毁该对象并释放所有已分配的资源。
QString QSqlError::databaseText() const
返回数据库报告的错误文本。该文本可能包含特定于该数据库的描述;也可能为空。
另请参阅 driverText() 和text()。
QString QSqlError::driverText() const
返回驱动程序报告的错误文本。该文本可能包含特定于数据库的描述,也可能为空。
另请参阅 databaseText() 和text()。
bool QSqlError::isValid() const
如果设置了错误,则返回true ;否则返回false。
示例:
QSqlQueryModel model;
model.setQuery("select * from myTable");
if(model.lastError().isValid())
qDebug() << model.lastError();另请参阅 type()。
QString QSqlError::nativeErrorCode() const
返回数据库特有的(本机)错误代码;如果无法确定,则返回空字符串。
注意:某些 驱动程序(如 DB2 或 ODBC)可能会返回多个错误代码。此时,; 将用作错误代码之间的分隔符。
[noexcept] void QSqlError::swap(QSqlError &other)
使用other 替换此错误。此操作非常快,且绝不会失败。
QString QSqlError::text() const
这是一个便捷函数,用于将databaseText()和driverText()拼接成一个字符串。
另请参阅 driverText() 和databaseText()。
QSqlError::ErrorType QSqlError::type() const
返回错误类型;如果无法确定类型,则返回 -1。
bool QSqlError::operator!=(const QSqlError &other) const
将other 错误中的type()和nativeErrorCode()与当前错误进行比较,若不一致则返回true 。
[noexcept] QSqlError &QSqlError::operator=(QSqlError &&other)
将other 通过移动赋值操作赋值给此QSqlError 实例。
注意: 被移动的源对象 other 将处于部分初始化状态,在此状态下,唯一有效的操作是销毁或赋予新值。
QSqlError &QSqlError::operator=(const QSqlError &other)
将other 错误的值赋给此错误。
bool QSqlError::operator==(const QSqlError &other) const
将other 错误中的type()和nativeErrorCode()与该错误进行比较,并返回true ,如果两者相等。
© 2026 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.