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

Synopsis¶
Functions¶
def
dbmsType()def
isOpenError()def
lastError()def
numericalPrecisionPolicy()def
setNumericalPrecisionPolicy(precisionPolicy)
Virtual functions¶
def
beginTransaction()def
cancelQuery()def
close()def
commitTransaction()def
createResult()def
escapeIdentifier(identifier, type)def
formatValue(field[, trimStrings=false])def
hasFeature(f)def
isIdentifierEscaped(identifier, type)def
isOpen()def
open(db[, user=””[, password=””[, host=””[, port=-1[, connOpts=””]]]]])def
primaryIndex(tableName)def
record(tableName)def
rollbackTransaction()def
setLastError(e)def
setOpen(o)def
setOpenError(e)def
sqlStatement(type, tableName, rec, preparedStatement)def
stripDelimiters(identifier, type)def
subscribeToNotification(name)def
subscribedToNotifications()def
tables(tableType)def
unsubscribeFromNotification(name)
Signals¶
def
notification(name)def
notification(name, source, payload)
Detailed Description¶
This class should not be used directly. Use
QSqlDatabaseinstead.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.
See also
- class PySide2.QtSql.QSqlDriver([parent=None])¶
- param parent:
Constructs a new driver with the given
parent.
- PySide2.QtSql.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
Description
QSqlDriver.Transactions
Whether the driver supports SQL transactions.
QSqlDriver.QuerySize
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
size()will return -1.QSqlDriver.BLOB
Whether the driver supports Binary Large Object fields.
QSqlDriver.Unicode
Whether the driver supports Unicode strings if the database server does.
QSqlDriver.PreparedQueries
Whether the driver supports prepared query execution.
QSqlDriver.NamedPlaceholders
Whether the driver supports the use of named placeholders.
QSqlDriver.PositionalPlaceholders
Whether the driver supports the use of positional placeholders.
QSqlDriver.LastInsertId
Whether the driver supports returning the Id of the last touched row.
QSqlDriver.BatchOperations
Whether the driver supports batched operations, see
execBatch()QSqlDriver.SimpleLocking
Whether the driver disallows a write lock on a table while other queries have a read lock on it.
QSqlDriver.LowPrecisionNumbers
Whether the driver allows fetching numerical values with low precision.
QSqlDriver.EventNotifications
Whether the driver supports database event notifications.
QSqlDriver.FinishQuery
Whether the driver can do any low-level resource cleanup when
finish()is called.QSqlDriver.MultipleResultSets
Whether the driver can access multiple result sets returned from batched statements or stored procedures.
QSqlDriver.CancelQuery
Whether the driver allows cancelling a running query.
More information about supported features can be found in the Qt SQL driver documentation.
See also
- PySide2.QtSql.QSqlDriver.StatementType¶
This enum contains a list of SQL statement (or clause) types the driver can create.
Constant
Description
QSqlDriver.WhereStatement
An SQL
WHEREstatement (e.g.,WHERE f = 5).QSqlDriver.SelectStatement
An SQL
SELECTstatement (e.g.,SELECT f FROM t).QSqlDriver.UpdateStatement
An SQL
UPDATEstatement (e.g.,UPDATE TABLE t set f = 1).QSqlDriver.InsertStatement
An SQL
INSERTstatement (e.g.,INSERT INTO t (f) values (1)).QSqlDriver.DeleteStatement
An SQL
DELETEstatement (e.g.,DELETE FROM t).See also
- PySide2.QtSql.QSqlDriver.IdentifierType¶
This enum contains a list of SQL identifier types.
Constant
Description
QSqlDriver.FieldName
A SQL field name
QSqlDriver.TableName
A SQL table name
- PySide2.QtSql.QSqlDriver.NotificationSource¶
This enum contains a list of SQL notification sources.
Constant
Description
QSqlDriver.UnknownSource
The notification source is unknown
QSqlDriver.SelfSource
The notification source is this connection
QSqlDriver.OtherSource
The notification source is another connection
- PySide2.QtSql.QSqlDriver.DbmsType¶
This enum contains DBMS types.
Constant
Description
QSqlDriver.UnknownDbms
QSqlDriver.MSSqlServer
QSqlDriver.MySqlServer
QSqlDriver.PostgreSQL
QSqlDriver.Oracle
QSqlDriver.Sybase
QSqlDriver.SQLite
QSqlDriver.Interbase
QSqlDriver.DB2
- PySide2.QtSql.QSqlDriver.beginTransaction()¶
- Return type:
bool
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
- PySide2.QtSql.QSqlDriver.cancelQuery()¶
- Return type:
bool
Tries to cancel the running query, if the underlying driver has the capability to cancel queries. Returns
trueon success, otherwise false.This function can be called from a different thread.
If you use this function as a slot, you need to use a
DirectConnectionfrom a different thread.Reimplement this function to support canceling running queries in your own
QSqlDriversubclass. It must be implemented in a thread-safe manner.See also
- PySide2.QtSql.QSqlDriver.close()¶
Derived classes must reimplement this pure virtual function in order to close the database connection. Return true on success, false on failure.
- PySide2.QtSql.QSqlDriver.commitTransaction()¶
- Return type:
bool
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
- PySide2.QtSql.QSqlDriver.createResult()¶
- Return type:
Creates an empty SQL result on the database. Derived classes must reimplement this function and return a
QSqlResultobject appropriate for their database to the caller.
- PySide2.QtSql.QSqlDriver.dbmsType()¶
- Return type:
Returns the current DBMS type for the database connection.
- PySide2.QtSql.QSqlDriver.escapeIdentifier(identifier, type)¶
- Parameters:
identifier – str
type –
IdentifierType
- Return type:
str
Returns the
identifierescaped according to the database rules.identifiercan either be a table name or field name, dependent ontype.The default implementation does nothing.
See also
- PySide2.QtSql.QSqlDriver.formatValue(field[, trimStrings=false])¶
- Parameters:
field –
PySide2.QtSql.QSqlFieldtrimStrings – bool
- Return type:
str
Returns a string representation of the
fieldvalue 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:
If
fieldis character data, the value is returned enclosed in single quotation marks, which is appropriate for many SQL databases. Any embedded single-quote characters are escaped (replaced with two single-quote characters). IftrimStringsis true (the default is false), all trailing whitespace is trimmed from the field.If
fieldis date/time data, the value is formatted in ISO format and enclosed in single quotation marks. If the date/time data is invalid, “NULL” is returned.If
fieldisbytearraydata, and the driver can edit binary fields, the value is formatted as a hexadecimal string.For any other field type, toString() is called on its value and the result of this is returned.
See also
toString()
- PySide2.QtSql.QSqlDriver.hasFeature(f)¶
- Parameters:
f –
DriverFeature- Return type:
bool
Returns
trueif the driver supports featurefeature; otherwise returnsfalse.Note that some databases need to be
open()before this can be determined.See also
DriverFeature
- PySide2.QtSql.QSqlDriver.isIdentifierEscaped(identifier, type)¶
- Parameters:
identifier – str
type –
IdentifierType
- Return type:
bool
Returns whether
identifieris escaped according to the database rules.identifiercan either be a table name or field name, dependent ontype.Reimplement this function if you want to provide your own implementation in your
QSqlDriversubclass,See also
- PySide2.QtSql.QSqlDriver.isOpen()¶
- Return type:
bool
Returns
trueif the database connection is open; otherwise returns false.
- PySide2.QtSql.QSqlDriver.isOpenError()¶
- Return type:
bool
Returns
trueif the there was an error opening the database connection; otherwise returnsfalse.
- PySide2.QtSql.QSqlDriver.lastError()¶
- Return type:
Returns a
QSqlErrorobject which contains information about the last error that occurred on the database.See also
- PySide2.QtSql.QSqlDriver.notification(name)¶
- Parameters:
name – str
Note
This function is deprecated.
- PySide2.QtSql.QSqlDriver.notification(name, source, payload)
- Parameters:
name – str
source –
NotificationSourcepayload – object
- PySide2.QtSql.QSqlDriver.numericalPrecisionPolicy()¶
- Return type:
Returns the current default precision policy for the database connection.
See also
NumericalPrecisionPolicysetNumericalPrecisionPolicy()numericalPrecisionPolicy()setNumericalPrecisionPolicy()
- PySide2.QtSql.QSqlDriver.open(db[, user=""[, password=""[, host=""[, port=-1[, connOpts=""]]]]])¶
- Parameters:
db – str
user – str
password – str
host – str
port – int
connOpts – str
- Return type:
bool
Derived classes must reimplement this pure virtual function to open a database connection on database
db, using user nameuser, passwordpassword, hosthost, portportand connection optionsoptions.The function must return true on success and false on failure.
See also
- PySide2.QtSql.QSqlDriver.primaryIndex(tableName)¶
- Parameters:
tableName – str
- Return type:
Returns the primary index for table
tableName. Returns an emptyQSqlIndexif the table doesn’t have a primary index. The default implementation returns an empty index.
- PySide2.QtSql.QSqlDriver.record(tableName)¶
- Parameters:
tableName – str
- Return type:
Returns a
QSqlRecordpopulated with the names of the fields in tabletableName. If no such table exists, an empty record is returned. The default implementation returns an empty record.
- PySide2.QtSql.QSqlDriver.rollbackTransaction()¶
- Return type:
bool
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
- PySide2.QtSql.QSqlDriver.setLastError(e)¶
- Parameters:
This function is used to set the value of the last error,
error, that occurred on the database.See also
- PySide2.QtSql.QSqlDriver.setNumericalPrecisionPolicy(precisionPolicy)¶
- Parameters:
precisionPolicy –
NumericalPrecisionPolicy
Sets the default numerical precision policy used by queries created by this driver to
precisionPolicy.Note: Setting the default precision policy to
precisionPolicydoesn’t affect any currently active queries.See also
NumericalPrecisionPolicynumericalPrecisionPolicy()setNumericalPrecisionPolicy()numericalPrecisionPolicy()
- PySide2.QtSql.QSqlDriver.setOpen(o)¶
- Parameters:
o – bool
This function sets the open state of the database to
open. Derived classes can use this function to report the status ofopen().See also
- PySide2.QtSql.QSqlDriver.setOpenError(e)¶
- Parameters:
e – bool
This function sets the open error state of the database to
error. Derived classes can use this function to report the status ofopen(). Note that iferroris true the open state of the database is set to closed (i.e.,isOpen()returnsfalse).See also
- PySide2.QtSql.QSqlDriver.sqlStatement(type, tableName, rec, preparedStatement)¶
- Parameters:
type –
StatementTypetableName – str
rec –
PySide2.QtSql.QSqlRecordpreparedStatement – bool
- Return type:
str
Returns a SQL statement of type
typefor the tabletableNamewith the values fromrec. IfpreparedStatementis true, the string will contain placeholders instead of values.The generated flag in each field of
recdetermines 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
recspecifies 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.
- PySide2.QtSql.QSqlDriver.stripDelimiters(identifier, type)¶
- Parameters:
identifier – str
type –
IdentifierType
- Return type:
str
Returns the
identifierwith the leading and trailing delimiters removed,identifiercan either be a table name or field name, dependent ontype. Ifidentifierdoes not have leading and trailing delimiter characters,identifieris returned without modification.Reimplement this function if you want to provide your own implementation in your
QSqlDriversubclass,See also
- PySide2.QtSql.QSqlDriver.subscribeToNotification(name)¶
- Parameters:
name – str
- Return type:
bool
This function is called to subscribe to event notifications from the database.
nameidentifies 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 callingopen()on an already open database may implicitly causeclose()to be called, which will cause the driver to unsubscribe from all event notifications.When an event notification identified by
nameis posted by the database thenotification()signal is emitted.Reimplement this function if you want to provide event notification support in your own
QSqlDriversubclass,
- PySide2.QtSql.QSqlDriver.subscribedToNotifications()¶
- Return type:
list of strings
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
QSqlDriversubclass,
- PySide2.QtSql.QSqlDriver.tables(tableType)¶
- Parameters:
tableType –
TableType- Return type:
list of strings
Returns a list of the names of the tables in the database. The default implementation returns an empty list.
The
tableTypeargument 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 asTablesfor backward compatibility.
- PySide2.QtSql.QSqlDriver.unsubscribeFromNotification(name)¶
- Parameters:
name – str
- Return type:
bool
This function is called to unsubscribe from event notifications from the database.
nameidentifies 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 bynameis posted by the database.Reimplement this function if you want to provide event notification support in your own
QSqlDriversubclass,
© 2022 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.