The QSqlDriver class is an abstract base class for accessing specific SQL databases. 更多...
头: | #include <QSqlDriver> |
qmake: | QT += sql |
继承: | QObject |
enum | DriverFeature { Transactions, QuerySize, BLOB, Unicode, PreparedQueries, …, CancelQuery } |
enum | IdentifierType { FieldName, TableName } |
enum | NotificationSource { UnknownSource, SelfSource, OtherSource } |
enum | StatementType { WhereStatement, SelectStatement, UpdateStatement, InsertStatement, DeleteStatement } |
QSqlDriver (QObject * parent = nullptr) | |
virtual | ~QSqlDriver () |
virtual bool | beginTransaction () |
virtual void | close () = 0 |
virtual bool | commitTransaction () |
virtual QSqlResult * | createResult () const = 0 |
virtual QString | escapeIdentifier (const QString & 标识符 , QSqlDriver::IdentifierType type ) const |
virtual QString | formatValue (const QSqlField & field , bool trimStrings = false) const |
virtual QVariant | handle () const |
virtual bool | hasFeature (QSqlDriver::DriverFeature feature ) const = 0 |
virtual bool | isIdentifierEscaped (const QString & 标识符 , QSqlDriver::IdentifierType type ) const |
virtual bool | isOpen () const |
bool | isOpenError () const |
QSqlError | lastError () const |
QSql::NumericalPrecisionPolicy | numericalPrecisionPolicy () const |
virtual bool | open (const QString & db , const QString & user = QString(), const QString & password = QString(), const QString & host = QString(), int port = -1, const QString & options = QString()) = 0 |
virtual QSqlIndex | primaryIndex (const QString & tableName ) const |
virtual QSqlRecord | record (const QString & tableName ) const |
virtual bool | rollbackTransaction () |
void | setNumericalPrecisionPolicy (QSql::NumericalPrecisionPolicy precisionPolicy ) |
virtual QString | sqlStatement (QSqlDriver::StatementType type , const QString & tableName , const QSqlRecord & rec , bool preparedStatement ) const |
virtual QString | stripDelimiters (const QString & 标识符 , QSqlDriver::IdentifierType type ) const |
virtual bool | subscribeToNotification (const QString & name ) |
virtual QStringList | subscribedToNotifications () const |
virtual QStringList | tables (QSql::TableType tableType ) const |
virtual bool | unsubscribeFromNotification (const QString & name ) |
void | notification (const QString & name , QSqlDriver::NotificationSource source , const QVariant & payload ) |
virtual void | setLastError (const QSqlError & error ) |
virtual void | setOpen (bool open ) |
virtual void | setOpenError (bool error ) |
This class should not be used directly. Use QSqlDatabase 代替。
If you want to create your own SQL drivers, you can subclass this class and reimplement its pure virtual functions and those virtual functions that you need. See How to Write Your Own Database Driver 了解更多信息。
另请参阅 QSqlDatabase and QSqlResult .
This enum contains a list of features a driver might support. Use hasFeature () to query whether a feature is supported or not.
常量 | 值 | 描述 |
---|---|---|
QSqlDriver::Transactions
|
0
|
Whether the driver supports SQL transactions. |
QSqlDriver::QuerySize
|
1
|
Whether the database is capable of reporting the size of a query. Note that some databases do not support returning the size (i.e. number of rows returned) of a query, in which case QSqlQuery::size () will return -1. |
QSqlDriver::BLOB
|
2
|
Whether the driver supports Binary Large Object fields. |
QSqlDriver::Unicode
|
3
|
Whether the driver supports Unicode strings if the database server does. |
QSqlDriver::PreparedQueries
|
4
|
Whether the driver supports prepared query execution. |
QSqlDriver::NamedPlaceholders
|
5
|
Whether the driver supports the use of named placeholders. |
QSqlDriver::PositionalPlaceholders
|
6
|
Whether the driver supports the use of positional placeholders. |
QSqlDriver::LastInsertId
|
7
|
Whether the driver supports returning the Id of the last touched row. |
QSqlDriver::BatchOperations
|
8
|
Whether the driver supports batched operations, see QSqlQuery::execBatch () |
QSqlDriver::SimpleLocking
|
9
|
Whether the driver disallows a write lock on a table while other queries have a read lock on it. |
QSqlDriver::LowPrecisionNumbers
|
10
|
Whether the driver allows fetching numerical values with low precision. |
QSqlDriver::EventNotifications
|
11
|
Whether the driver supports database event notifications. |
QSqlDriver::FinishQuery
|
12
|
Whether the driver can do any low-level resource cleanup when QSqlQuery::finish () 被调用。 |
QSqlDriver::MultipleResultSets
|
13
|
Whether the driver can access multiple result sets returned from batched statements or stored procedures. |
QSqlDriver::CancelQuery
|
14
|
Whether the driver allows cancelling a running query. |
More information about supported features can be found in the Qt SQL driver 文档编制。
另请参阅 hasFeature ().
This enum contains a list of SQL identifier types.
常量 | 值 | 描述 |
---|---|---|
QSqlDriver::FieldName
|
0
|
A SQL field name |
QSqlDriver::TableName
|
1
|
A SQL table name |
This enum contains a list of SQL notification sources.
常量 | 值 | 描述 |
---|---|---|
QSqlDriver::UnknownSource
|
0
|
The notification source is unknown |
QSqlDriver::SelfSource
|
1
|
The notification source is this connection |
QSqlDriver::OtherSource
|
2
|
The notification source is another connection |
This enum contains a list of SQL statement (or clause) types the driver can create.
常量 | 值 | 描述 |
---|---|---|
QSqlDriver::WhereStatement
|
0
|
An SQL
WHERE
statement (e.g.,
WHERE f = 5
).
|
QSqlDriver::SelectStatement
|
1
|
An SQL
SELECT
statement (e.g.,
SELECT f FROM t
).
|
QSqlDriver::UpdateStatement
|
2
|
An SQL
UPDATE
statement (e.g.,
UPDATE TABLE t set f = 1
).
|
QSqlDriver::InsertStatement
|
3
|
An SQL
INSERT
statement (e.g.,
INSERT INTO t (f) values (1)
).
|
QSqlDriver::DeleteStatement
|
4
|
An SQL
DELETE
statement (e.g.,
DELETE FROM t
).
|
另请参阅 sqlStatement ().
Constructs a new driver with the given parent .
[signal]
void
QSqlDriver::
notification
(const
QString
&
name
,
QSqlDriver::NotificationSource
source
, const
QVariant
&
payload
)
This signal is emitted when the database posts an event notification that the driver subscribes to. name identifies the event notification, source indicates the signal source, payload holds the extra data optionally delivered with the notification.
注意: 信号 notification 在此类中被重载。通过使用函数指针句法连接到此信号,Qt 提供用于获得如此范例展示的函数指针的方便帮助程序:
connect(sqlDriver, QOverload<const QString &, QSqlDriver::NotificationSource, const QVariant &>::of(&QSqlDriver::notification), [=](const QString &name, QSqlDriver::NotificationSource source, const QVariant &payload){ /* ... */ });
该函数在 Qt 5.0 引入。
另请参阅 subscribeToNotification ().
[虚拟]
QSqlDriver::
~QSqlDriver
()
销毁对象并释放任何分配资源。
[虚拟]
bool
QSqlDriver::
beginTransaction
()
This function is called to begin a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns
false
.
另请参阅 commitTransaction () 和 rollbackTransaction ().
[pure virtual]
void
QSqlDriver::
close
()
Derived classes must reimplement this pure virtual function in order to close the database connection. Return true on success, false on failure.
[虚拟]
bool
QSqlDriver::
commitTransaction
()
This function is called to commit a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns
false
.
另请参阅 beginTransaction () 和 rollbackTransaction ().
[pure virtual]
QSqlResult
*QSqlDriver::
createResult
() const
Creates an empty SQL result on the database. Derived classes must reimplement this function and return a QSqlResult object appropriate for their database to the caller.
[虚拟]
QString
QSqlDriver::
escapeIdentifier
(const
QString
&
标识符
,
QSqlDriver::IdentifierType
type
) const
返回 标识符 escaped according to the database rules. 标识符 can either be a table name or field name, dependent on type .
默认实现什么都不做。
另请参阅 isIdentifierEscaped ().
[虚拟]
QString
QSqlDriver::
formatValue
(const
QSqlField
&
field
,
bool
trimStrings
= false) const
Returns a string representation of the field value for the database. This is used, for example, when constructing INSERT and UPDATE statements.
The default implementation returns the value formatted as a string according to the following rules:
另请参阅 QVariant::toString ().
[虚拟]
QVariant
QSqlDriver::
handle
() const
Returns the low-level database handle wrapped in a QVariant or an invalid variant if there is no handle.
警告: Use this with uttermost care and only if you know what you're doing.
警告: The handle returned here can become a stale pointer if the connection is modified (for example, if you close the connection).
警告: The handle can be NULL if the connection is not open yet.
The handle returned here is database-dependent, you should query the type name of the variant before accessing it.
This example retrieves the handle for a connection to sqlite:
QSqlDatabase db = QSqlDatabase::database(); QVariant v = db.driver()->handle(); if (v.isValid() && (qstrcmp(v.typeName(), "sqlite3*") == 0)) { // v.data() returns a pointer to the handle sqlite3 *handle = *static_cast<sqlite3 **>(v.data()); if (handle) { // ... } }
This snippet returns the handle for PostgreSQL or MySQL:
if (qstrcmp(v.typeName(), "PGconn*") == 0) { PGconn *handle = *static_cast<PGconn **>(v.data()); if (handle) { // ... } } if (qstrcmp(v.typeName(), "MYSQL*") == 0) { MYSQL *handle = *static_cast<MYSQL **>(v.data()); if (handle) { // ... } }
另请参阅 QSqlResult::handle ().
[pure virtual]
bool
QSqlDriver::
hasFeature
(
QSqlDriver::DriverFeature
feature
) const
返回
true
if the driver supports feature
feature
;否则返回
false
.
Note that some databases need to be open () before this can be determined.
另请参阅 DriverFeature .
[虚拟]
bool
QSqlDriver::
isIdentifierEscaped
(const
QString
&
标识符
,
QSqlDriver::IdentifierType
type
) const
返回是否 标识符 is escaped according to the database rules. 标识符 can either be a table name or field name, dependent on type .
Reimplement this function if you want to provide your own implementation in your QSqlDriver 子类,
另请参阅 stripDelimiters () 和 escapeIdentifier ().
[虚拟]
bool
QSqlDriver::
isOpen
() const
返回
true
if the database connection is open; otherwise returns false.
返回
true
if the there was an error opening the database connection; otherwise returns
false
.
返回 QSqlError object which contains information about the last error that occurred on the database.
另请参阅 setLastError ().
Returns the current default precision policy for the database connection.
该函数在 Qt 4.6 引入。
另请参阅 QSql::NumericalPrecisionPolicy , setNumericalPrecisionPolicy (), QSqlQuery::numericalPrecisionPolicy (),和 QSqlQuery::setNumericalPrecisionPolicy ().
[pure virtual]
bool
QSqlDriver::
open
(const
QString
&
db
, const
QString
&
user
= QString(), const
QString
&
password
= QString(), const
QString
&
host
= QString(),
int
port
= -1, const
QString
&
options
= QString())
Derived classes must reimplement this pure virtual function to open a database connection on database db , using user name user , password password , host host , port port and connection options options .
The function must return true on success and false on failure.
另请参阅 setOpen ().
[虚拟]
QSqlIndex
QSqlDriver::
primaryIndex
(const
QString
&
tableName
) const
Returns the primary index for table tableName . Returns an empty QSqlIndex if the table doesn't have a primary index. The default implementation returns an empty index.
[虚拟]
QSqlRecord
QSqlDriver::
record
(const
QString
&
tableName
) const
返回 QSqlRecord populated with the names of the fields in table tableName . If no such table exists, an empty record is returned. The default implementation returns an empty record.
[虚拟]
bool
QSqlDriver::
rollbackTransaction
()
This function is called to rollback a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns
false
.
另请参阅 beginTransaction () 和 commitTransaction ().
[virtual protected]
void
QSqlDriver::
setLastError
(const
QSqlError
&
error
)
This function is used to set the value of the last error, error , that occurred on the database.
另请参阅 lastError ().
Sets the default numerical precision policy used by queries created by this driver to precisionPolicy .
Note: Setting the default precision policy to precisionPolicy doesn't affect any currently active queries.
该函数在 Qt 4.6 引入。
另请参阅 QSql::NumericalPrecisionPolicy , numericalPrecisionPolicy (), QSqlQuery::setNumericalPrecisionPolicy (),和 QSqlQuery::numericalPrecisionPolicy ().
[virtual protected]
void
QSqlDriver::
setOpen
(
bool
open
)
This function sets the open state of the database to open . Derived classes can use this function to report the status of open ().
另请参阅 open () 和 setOpenError ().
[virtual protected]
void
QSqlDriver::
setOpenError
(
bool
error
)
This function sets the open error state of the database to
error
. Derived classes can use this function to report the status of
open
(). Note that if
error
is true the open state of the database is set to closed (i.e.,
isOpen
() 返回
false
).
另请参阅 isOpenError (), open (),和 setOpen ().
[虚拟]
QString
QSqlDriver::
sqlStatement
(
QSqlDriver::StatementType
type
, const
QString
&
tableName
, const
QSqlRecord
&
rec
,
bool
preparedStatement
) const
Returns a SQL statement of type type for the table tableName with the values from rec 。若 preparedStatement is true, the string will contain placeholders instead of values.
The generated flag in each field of rec determines whether the field is included in the generated statement.
This method can be used to manipulate tables without having to worry about database-dependent SQL dialects. For non-prepared statements, the values will be properly escaped.
In the WHERE statement, each non-null field of rec specifies a filter condition of equality to the field value, or if prepared, a placeholder. However, prepared or not, a null field specifies the condition IS NULL and never introduces a placeholder. The application must not attempt to bind data for the null field during execution. The field must be set to some non-null value if a placeholder is desired. Furthermore, since non-null fields specify equality conditions and SQL NULL is not equal to anything, even itself, it is generally not useful to bind a null to a placeholder.
[虚拟]
QString
QSqlDriver::
stripDelimiters
(const
QString
&
标识符
,
QSqlDriver::IdentifierType
type
) const
返回 标识符 with the leading and trailing delimiters removed, 标识符 can either be a table name or field name, dependent on type 。若 标识符 does not have leading and trailing delimiter characters, 标识符 is returned without modification.
Reimplement this function if you want to provide your own implementation in your QSqlDriver 子类,
该函数在 Qt 4.5 引入。
另请参阅 isIdentifierEscaped ().
[虚拟]
bool
QSqlDriver::
subscribeToNotification
(const
QString
&
name
)
This function is called to subscribe to event notifications from the database. name identifies the event notification.
If successful, return true, otherwise return false.
The database must be open when this function is called. When the database is closed by calling close () all subscribed event notifications are automatically unsubscribed. Note that calling open () on an already open database may implicitly cause close () to be called, which will cause the driver to unsubscribe from all event notifications.
When an event notification identified by name is posted by the database the notification() signal is emitted.
Reimplement this function if you want to provide event notification support in your own QSqlDriver 子类,
该函数在 Qt 4.4 引入。
另请参阅 unsubscribeFromNotification (), subscribedToNotifications (),和 QSqlDriver::hasFeature ().
[虚拟]
QStringList
QSqlDriver::
subscribedToNotifications
() const
Returns a list of the names of the event notifications that are currently subscribed to.
Reimplement this function if you want to provide event notification support in your own QSqlDriver 子类,
该函数在 Qt 4.4 引入。
另请参阅 subscribeToNotification () 和 unsubscribeFromNotification ().
[虚拟]
QStringList
QSqlDriver::
tables
(
QSql::TableType
tableType
) const
Returns a list of the names of the tables in the database. The default implementation returns an empty list.
The tableType argument describes what types of tables should be returned. Due to binary compatibility, the string contains the value of the enum QSql::TableTypes as text. An empty string should be treated as QSql::Tables for backward compatibility.
[虚拟]
bool
QSqlDriver::
unsubscribeFromNotification
(const
QString
&
name
)
This function is called to unsubscribe from event notifications from the database. name identifies the event notification.
If successful, return true, otherwise return false.
The database must be open when this function is called. All subscribed event notifications are automatically unsubscribed from when the close () 函数被调用。
After calling this function the notification() signal will no longer be emitted when an event notification identified by name is posted by the database.
Reimplement this function if you want to provide event notification support in your own QSqlDriver 子类,
该函数在 Qt 4.4 引入。
另请参阅 subscribeToNotification () 和 subscribedToNotifications ().