Home · All Classes · Modules

QSqlDriver Class Reference
[QtSql module]

The QSqlDriver class is an abstract base class for accessing specific SQL databases. More...

Inherits QObject.

Types

Methods

Qt Signals


Detailed Description

The QSqlDriver class is an abstract base class for accessing specific SQL databases.

This class should not be used directly. Use QSqlDatabase instead.

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 for more information.


Type Documentation

QSqlDriver.DriverFeature

This enum contains a list of features a driver might support. Use hasFeature() to query whether a feature is supported or not.

Constant Value Description
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() is called.
QSqlDriver.MultipleResultSets 13 Whether the driver can access multiple result sets returned from batched statements or stored procedures.

More information about supported features can be found in the Qt SQL driver documentation.

See also hasFeature().

QSqlDriver.IdentifierType

This enum contains a list of SQL identifier types.

Constant Value Description
QSqlDriver.FieldName 0 A SQL field name
QSqlDriver.TableName 1 A SQL table name

QSqlDriver.StatementType

This enum contains a list of SQL statement (or clause) types the driver can create.

Constant Value Description
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).

See also sqlStatement().


Method Documentation

QSqlDriver.__init__ (self, QObject parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a new driver with the given parent.

bool QSqlDriver.beginTransaction (self)

This function is called to begin a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See also commitTransaction() and rollbackTransaction().

QSqlDriver.close (self)

This method is abstract and should be reimplemented in any sub-class.

Derived classes must reimplement this pure virtual function in order to close the database connection. Return true on success, false on failure.

See also open() and setOpen().

bool QSqlDriver.commitTransaction (self)

This function is called to commit a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See also beginTransaction() and rollbackTransaction().

QSqlResult QSqlDriver.createResult (self)

This method is abstract and should be reimplemented in any sub-class.

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 (self, QString identifier, IdentifierType type)

Returns the identifier escaped according to the database rules. identifier can either be a table name or field name, dependent on type.

The default implementation does nothing.

See also isIdentifierEscaped().

QString QSqlDriver.formatValue (self, QSqlField field, bool trimStrings = False)

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:

See also QVariant.toString().

QVariant QSqlDriver.handle (self)

Returns the low-level database handle wrapped in a QVariant or an invalid variant if there is no handle.

Warning: Use this with uttermost care and only if you know what you're doing.

Warning: The handle returned here can become a stale pointer if the connection is modified (for example, if you close the connection).

Warning: 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 = ...;
 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 != 0) { // check that it is not NULL
         ...
     }
 }

This snippet returns the handle for PostgreSQL or MySQL:

 if (qstrcmp(v.typeName(), "PGconn*") == 0) {
     PGconn *handle = *static_cast<PGconn **>(v.data());
     if (handle != 0) ...
 }

 if (qstrcmp(v.typeName(), "MYSQL*") == 0) {
     MYSQL *handle = *static_cast<MYSQL **>(v.data());
     if (handle != 0) ...
 }

See also QSqlResult.handle().

bool QSqlDriver.hasFeature (self, DriverFeature f)

This method is abstract and should be reimplemented in any sub-class.

Returns true if the driver supports feature feature; otherwise returns false.

Note that some databases need to be open() before this can be determined.

See also DriverFeature.

bool QSqlDriver.isIdentifierEscaped (self, QString identifier, IdentifierType type)

Returns whether identifier is escaped according to the database rules. identifier can either be a table name or field name, dependent on type.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide your own implementation in your QSqlDriver subclass, reimplement the isIdentifierEscapedImplementation() slot in your subclass instead. The isIdentifierEscapedFunction() will dynamically detect the slot and call it.

See also stripDelimiters() and escapeIdentifier().

bool QSqlDriver.isIdentifierEscapedImplementation (self, QString identifier, IdentifierType type)

This method is also a Qt slot with the C++ signature bool isIdentifierEscapedImplementation(const QString&, ::QSqlDriver::IdentifierType) const.

This slot returns whether identifier is escaped according to the database rules. identifier can either be a table name or field name, dependent on type.

Because of binary compatibility constraints, isIdentifierEscaped() function (introduced in Qt 4.5) is not virtual. Instead, isIdentifierEscaped() will dynamically detect and call this slot. The default implementation assumes the escape/delimiter character is a double quote. Reimplement this slot in your own QSqlDriver if your database engine uses a different delimiter character.

This function was introduced in Qt 4.6.

See also isIdentifierEscaped().

bool QSqlDriver.isOpen (self)

Returns true if the database connection is open; otherwise returns false.

bool QSqlDriver.isOpenError (self)

Returns true if the there was an error opening the database connection; otherwise returns false.

QSqlError QSqlDriver.lastError (self)

Returns a QSqlError object which contains information about the last error that occurred on the database.

See also setLastError().

QSql.NumericalPrecisionPolicy QSqlDriver.numericalPrecisionPolicy (self)

Returns the current default precision policy for the database connection.

This function was introduced in Qt 4.6.

See also QSql.NumericalPrecisionPolicy, setNumericalPrecisionPolicy(), QSqlQuery.numericalPrecisionPolicy(), and QSqlQuery.setNumericalPrecisionPolicy().

bool QSqlDriver.open (self, QString db, QString user = '', QString password = '', QString host = '', int port = -1, QString options = '')

This method is abstract and should be reimplemented in any sub-class.

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.

See also setOpen().

QSqlIndex QSqlDriver.primaryIndex (self, QString tableName)

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 (self, QString tableName)

Returns a 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 (self)

This function is called to rollback a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See also beginTransaction() and commitTransaction().

QSqlDriver.setLastError (self, QSqlError e)

This function is used to set the value of the last error, error, that occurred on the database.

See also lastError().

QSqlDriver.setNumericalPrecisionPolicy (self, QSql.NumericalPrecisionPolicy precisionPolicy)

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.

This function was introduced in Qt 4.6.

See also QSql.NumericalPrecisionPolicy, numericalPrecisionPolicy(), QSqlQuery.setNumericalPrecisionPolicy(), and QSqlQuery.numericalPrecisionPolicy().

QSqlDriver.setOpen (self, bool o)

This function sets the open state of the database to open. Derived classes can use this function to report the status of open().

See also open() and setOpenError().

QSqlDriver.setOpenError (self, bool e)

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() returns false).

See also isOpenError(), open(), and setOpen().

QString QSqlDriver.sqlStatement (self, StatementType type, QString tableName, QSqlRecord rec, bool preparedStatement)

Returns a SQL statement of type type for the table tableName with the values from rec. If preparedStatement is true, the string will contain placeholders instead of values.

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.

QString QSqlDriver.stripDelimiters (self, QString identifier, IdentifierType type)

Returns the identifier with the leading and trailing delimiters removed, identifier can either be a table name or field name, dependent on type. If identifier does not have leading and trailing delimiter characters, identifier is returned without modification.

Warning: Because of binary compatibility constraints, this function is not virtual, If you want to provide your own implementation in your QSqlDriver subclass, reimplement the stripDelimitersImplementation() slot in your subclass instead. The stripDelimiters() function will dynamically detect the slot and call it.

This function was introduced in Qt 4.5.

See also isIdentifierEscaped().

QString QSqlDriver.stripDelimitersImplementation (self, QString identifier, IdentifierType type)

This method is also a Qt slot with the C++ signature QString stripDelimitersImplementation(const QString&, ::QSqlDriver::IdentifierType) const.

This slot returns identifier with the leading and trailing delimiters removed, identifier can either be a tablename or field name, dependent on type. If identifier does not have leading and trailing delimiter characters, identifier is returned without modification.

Because of binary compatibility constraints, the stripDelimiters() function (introduced in Qt 4.5) is not virtual. Instead, stripDelimiters() will dynamically detect and call this slot. It generally unnecessary to reimplement this slot.

This function was introduced in Qt 4.6.

See also stripDelimiters().

QStringList QSqlDriver.subscribedToNotifications (self)

Returns a list of the names of the event notifications that are currently subscribed to.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide event notification support in your own QSqlDriver subclass, reimplement the subscribedToNotificationsImplementation() slot in your subclass instead. The subscribedToNotifications() function will dynamically detect the slot and call it.

This function was introduced in Qt 4.4.

See also subscribeToNotification() and unsubscribeFromNotification().

QStringList QSqlDriver.subscribedToNotificationsImplementation (self)

This method is also a Qt slot with the C++ signature QStringList subscribedToNotificationsImplementation() const.

Returns a list of the names of the event notifications that are currently subscribed to.

Reimplement this slot to provide your own QSqlDriver subclass with event notification support; because of binary compatibility constraints, the subscribedToNotifications() function (introduced in Qt 4.4) is not virtual. Instead, subscribedToNotifications() will dynamically detect and call this slot. The default implementation simply returns an empty QStringList.

This function was introduced in Qt 4.4.

See also subscribedToNotifications().

bool QSqlDriver.subscribeToNotification (self, 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.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide event notification support in your own QSqlDriver subclass, reimplement the subscribeToNotificationImplementation() slot in your subclass instead. The subscribeToNotification() function will dynamically detect the slot and call it.

This function was introduced in Qt 4.4.

See also unsubscribeFromNotification(), subscribedToNotifications(), and QSqlDriver.hasFeature().

bool QSqlDriver.subscribeToNotificationImplementation (self, QString name)

This method is also a Qt slot with the C++ signature bool subscribeToNotificationImplementation(const QString&).

This slot 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 slot 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 slot to provide your own QSqlDriver subclass with event notification support; because of binary compatibility constraints, the subscribeToNotification() function (introduced in Qt 4.4) is not virtual. Instead, subscribeToNotification() will dynamically detect and call this slot. The default implementation does nothing and returns false.

This function was introduced in Qt 4.4.

See also subscribeToNotification().

QStringList QSqlDriver.tables (self, QSql.TableType tableType)

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 (self, 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() function is called.

After calling this function the notification() signal will no longer be emitted when an event notification identified by name is posted by the database.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide event notification support in your own QSqlDriver subclass, reimplement the unsubscribeFromNotificationImplementation() slot in your subclass instead. The unsubscribeFromNotification() function will dynamically detect the slot and call it.

This function was introduced in Qt 4.4.

See also subscribeToNotification() and subscribedToNotifications().

bool QSqlDriver.unsubscribeFromNotificationImplementation (self, QString name)

This method is also a Qt slot with the C++ signature bool unsubscribeFromNotificationImplementation(const QString&).

This slot 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 slot is called. All subscribed event notifications are automatically unsubscribed from when the close() function is called.

After calling this slot the notification() signal will no longer be emitted when an event notification identified by name is posted by the database.

Reimplement this slot to provide your own QSqlDriver subclass with event notification support; because of binary compatibility constraints, the unsubscribeFromNotification() function (introduced in Qt 4.4) is not virtual. Instead, unsubscribeFromNotification() will dynamically detect and call this slot. The default implementation does nothing and returns false.

This function was introduced in Qt 4.4.

See also unsubscribeFromNotification().


Qt Signal Documentation

void notification (const QString&)

This is the default overload of this signal.

This signal is emitted when the database posts an event notification that the driver subscribes to. name identifies the event notification.

This function was introduced in Qt 4.4.

See also subscribeToNotification().


PyQt 4.12.1 for X11Copyright © Riverbank Computing Ltd and The Qt Company 2015Qt 4.8.7