PySide6.QtSql.QSqlDriver¶
- class QSqlDriver¶
- The - QSqlDriverclass is an abstract base class for accessing specific SQL databases.- Details- 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 - Synopsis¶- Properties¶- Methods¶- def - __init__()
- def - connectionName()
- def - dbmsType()
- def - isOpenError()
- def - lastError()
 - Virtual methods¶
- def - cancelQuery()
- def - close()
- def - createResult()
- def - formatValue()
- def - hasFeature()
- def - isOpen()
- def - open()
- def - primaryIndex()
- def - record()
- def - setLastError()
- def - setOpen()
- def - setOpenError()
- def - sqlStatement()
- def - tables()
 - Signals¶- def - notification()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - class DriverFeature¶
- This enum contains a list of features a driver might support. Use - hasFeature()to query whether a feature is supported or not. Some features depend on the database server so they can only properly determined after the database connection is successfully opened with- open().- Constant - Description - QSqlDriver.DriverFeature.Transactions - Whether the driver supports SQL transactions. - QSqlDriver.DriverFeature.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.DriverFeature.BLOB - Whether the driver supports Binary Large Object fields. - QSqlDriver.DriverFeature.Unicode - Whether the driver supports Unicode strings if the database server does. - QSqlDriver.DriverFeature.PreparedQueries - Whether the driver supports prepared query execution. - QSqlDriver.DriverFeature.NamedPlaceholders - Whether the driver supports the use of named placeholders. - QSqlDriver.DriverFeature.PositionalPlaceholders - Whether the driver supports the use of positional placeholders. - QSqlDriver.DriverFeature.LastInsertId - Whether the driver supports returning the Id of the last touched row. - QSqlDriver.DriverFeature.BatchOperations - Whether the driver supports batched operations, see - execBatch()- QSqlDriver.DriverFeature.SimpleLocking - Whether the driver disallows a write lock on a table while other queries have a read lock on it. - QSqlDriver.DriverFeature.LowPrecisionNumbers - Whether the driver allows fetching numerical values with low precision. - QSqlDriver.DriverFeature.EventNotifications - Whether the driver supports database event notifications. - QSqlDriver.DriverFeature.FinishQuery - Whether the driver can do any low-level resource cleanup when - finish()is called.- QSqlDriver.DriverFeature.MultipleResultSets - Whether the driver can access multiple result sets returned from batched statements or stored procedures. - QSqlDriver.DriverFeature.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 
 - class StatementType¶
- This enum contains a list of SQL statement (or clause) types the driver can create. - Constant - Description - QSqlDriver.StatementType.WhereStatement - An SQL - WHEREstatement (e.g.,- WHERE f = 5).- QSqlDriver.StatementType.SelectStatement - An SQL - SELECTstatement (e.g.,- SELECT f FROM t).- QSqlDriver.StatementType.UpdateStatement - An SQL - UPDATEstatement (e.g.,- UPDATE TABLE t set f = 1).- QSqlDriver.StatementType.InsertStatement - An SQL - INSERTstatement (e.g.,- INSERT INTO t (f) values (1)).- QSqlDriver.StatementType.DeleteStatement - An SQL - DELETEstatement (e.g.,- DELETE FROM t).- See also 
 - class IdentifierType¶
- This enum contains a list of SQL identifier types. - Constant - Description - QSqlDriver.IdentifierType.FieldName - A SQL field name - QSqlDriver.IdentifierType.TableName - A SQL table name 
 - class NotificationSource¶
- This enum contains a list of SQL notification sources. - Constant - Description - QSqlDriver.NotificationSource.UnknownSource - The notification source is unknown - QSqlDriver.NotificationSource.SelfSource - The notification source is this connection - QSqlDriver.NotificationSource.OtherSource - The notification source is another connection 
 - class DbmsType¶
 - Note - Properties can be used directly when - from __feature__ import true_propertyis used or via accessor functions otherwise.- property numericalPrecisionPolicyᅟ: QSql.NumericalPrecisionPolicy¶
 - This property holds the precision policy for the database connection. - Note - Setting the precision policy doesn’t affect any currently active queries. - Access functions:
 - Constructs a new driver with the given - parent.- 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 - cancelQuery()¶
- Return type:
- bool 
 
 - abstract close()¶
 - Derived classes must reimplement this pure virtual function in order to close the database connection. Return true on success, false on failure. - 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 - connectionName()¶
- Return type:
- str 
 
 - Returns the database connection name the driver was created by with - addDatabase()- abstract 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.- 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 on- type.- The default implementation does nothing. - See also - formatValue(field[, trimStrings=false])¶
- Parameters:
- field – - QSqlField
- trimStrings – 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). If- trimStringsis 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 - fieldis bytearray data, 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()- abstract hasFeature(f)¶
- Parameters:
- f – - DriverFeature
- Return type:
- bool 
 
 - Returns - trueif the driver supports feature- feature; otherwise returns- false.- Note that some databases need to be - open()before this can be determined.- See also - 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 on- type.- Reimplement this function if you want to provide your own implementation in your - QSqlDriversubclass,- See also - isOpen()¶
- Return type:
- bool 
 
 - Returns - trueif the database connection is open; otherwise returns false.- isOpenError()¶
- Return type:
- bool 
 
 - Returns - trueif the there was an error opening the database connection; otherwise returns- false.- Returns a - QSqlErrorobject which contains information about the last error that occurred on the database.- See also - maximumIdentifierLength(type)¶
- Parameters:
- type – - IdentifierType
- Return type:
- int 
 
 - Returns the maximum length for the identifier - typeaccording to the database settings. Returns INT_MAX by default if the is no maximum for the database.- notification(name, source, payload)¶
- Parameters:
- name – str 
- source – - NotificationSource
- payload – object 
 
 
 - This signal is emitted when the database posts an event notification that the driver subscribes to. - nameidentifies the event notification,- sourceindicates the signal source,- payloadholds the extra data optionally delivered with the notification.- See also - numericalPrecisionPolicy()¶
- Return type:
 
 - Returns the numericalPrecisionPolicy. - See also - Getter of property - numericalPrecisionPolicyᅟ.- abstract 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 name- user, password- password, host- host, port- portand connection options- options.- The function must return true on success and false on failure. - See also - Returns the primary index for table - tableName. Returns an empty- QSqlIndexif the table doesn’t have a primary index. The default implementation returns an empty index.- record(tableName)¶
- Parameters:
- tableName – str 
- Return type:
 
 - Returns a - QSqlRecordpopulated 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.- 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 - This function is used to set the value of the last error, - error, that occurred on the database.- See also - setNumericalPrecisionPolicy(precisionPolicy)¶
- Parameters:
- precisionPolicy – - NumericalPrecisionPolicy
 
 - Sets - numericalPrecisionPolicyto- precisionPolicy.- See also - Setter of property - numericalPrecisionPolicyᅟ.- 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 of- open().- See also - 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 of- open(). Note that if- erroris true the open state of the database is set to closed (i.e.,- isOpen()returns- false).- See also - sqlStatement(type, tableName, rec, preparedStatement)¶
- Parameters:
- type – - StatementType
- tableName – str 
- rec – - QSqlRecord
- preparedStatement – bool 
 
- Return type:
- str 
 
 - Returns a SQL statement of type - typefor the table- tableNamewith the values from- rec. If- preparedStatementis 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.- 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 on- type. If- identifierdoes 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 - 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 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 - nameis posted by the database the- notification()signal is emitted.- Reimplement this function if you want to provide event notification support in your own - QSqlDriversubclass,- 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,- 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 as- Tablesfor backward compatibility.- 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 by- nameis posted by the database.- Reimplement this function if you want to provide event notification support in your own - QSqlDriversubclass,