QSqlDriver Class Reference
The QSqlDriver class is an abstract base class for accessing specific SQL databases.
- #include <QSqlDriver>
Inherits: QObject.
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.
See also QSqlDatabase and QSqlResult.
Public Types
| Toggle details | enum QSqlDriver:: | DriverFeatureDriverFeature { Transactions , QuerySize , BLOB , Unicode , PreparedQueries , NamedPlaceholders , PositionalPlaceholders , LastInsertId , BatchOperations , SimpleLocking , LowPrecisionNumbers , EventNotifications , FinishQuery , MultipleResultSets 13 ...} { Transactions , QuerySize , BLOB , Unicode , PreparedQueries , NamedPlaceholders , PositionalPlaceholders , LastInsertId , BatchOperations , SimpleLocking , LowPrecisionNumbers , EventNotifications , FinishQuery , MultipleResultSets 13 } | |||||||||||||||||||||||||||||||||||||||||||||
This enum contains a list of features a driver might support. Use hasFeature() to query whether a feature is supported or not.
More information about supported features can be found in the Qt SQL driver documentation. See also hasFeature(). | |||||||||||||||||||||||||||||||||||||||||||||||
Look up this member in the source code. | |||||||||||||||||||||||||||||||||||||||||||||||
| Toggle details | enum QSqlDriver:: | IdentifierTypeIdentifierType { FieldName , TableName 1 ...} { FieldName , TableName 1 } | |||||||||||||||||||||||||||||||||||||||||||||
This enum contains a list of SQL identifier types.
| |||||||||||||||||||||||||||||||||||||||||||||||
Look up this member in the source code. | |||||||||||||||||||||||||||||||||||||||||||||||
| Toggle details | enum QSqlDriver:: | StatementTypeStatementType { WhereStatement , SelectStatement , UpdateStatement , InsertStatement , DeleteStatement 4 ...} { WhereStatement , SelectStatement , UpdateStatement , InsertStatement , DeleteStatement 4 } | |||||||||||||||||||||||||||||||||||||||||||||
This enum contains a list of SQL statement (or clause) types the driver can create.
See also sqlStatement(). | |||||||||||||||||||||||||||||||||||||||||||||||
Look up this member in the source code. | |||||||||||||||||||||||||||||||||||||||||||||||
Public Functions
| Toggle details | QSqlDriver | QSqlDriverQSqlDriver ( QObject *parent=0 ) ( QObject *parent=0 ) |
Constructs a new driver with the given parent. | ||
Look up this member in the source code. | ||
| Toggle details | QSqlDriver | ~QSqlDriver~QSqlDriver () () |
Destroys the object and frees any allocated resources. | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | beginTransactionbeginTransaction () () [virtual] |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | void QSqlDriver | closeclose () () [pure virtual] |
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | commitTransactioncommitTransaction () () [virtual] |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | QSqlResult * QSqlDriver | createResultcreateResult () ()const [pure virtual] |
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. | ||
Look up this member in the source code. | ||
| Toggle details | QString QSqlDriver | escapeIdentifierescapeIdentifier ( const QString &identifier , IdentifierType type ...) ( const QString &identifier , IdentifierType type )const [virtual] |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | QString QSqlDriver | formatValueformatValue ( const QSqlField &field , bool trimStrings=false ...) ( const QSqlField &field , bool trimStrings=false )const [virtual] |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | QVariant QSqlDriver | handlehandle () ()const [virtual] |
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:
This snippet returns the handle for PostgreSQL or MySQL:
See also QSqlResult::handle(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | hasFeaturehasFeature ( DriverFeature feature ) ( DriverFeature feature )const [pure virtual] |
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. | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | isIdentifierEscapedisIdentifierEscaped ( const QString &identifier , IdentifierType type ...) ( const QString &identifier , IdentifierType type )const |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | isOpenisOpen () ()const [virtual] |
Returns true if the database connection is open; otherwise returns false. | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | isOpenErrorisOpenError () ()const |
Returns true if the there was an error opening the database connection; otherwise returns false. | ||
Look up this member in the source code. | ||
| Toggle details | QSqlError QSqlDriver | lastErrorlastError () ()const |
Returns a QSqlError object which contains information about the last error that occurred on the database. See also setLastError(). | ||
Look up this member in the source code. | ||
| Toggle details | QSql::NumericalPrecisionPolicy QSqlDriver | numericalPrecisionPolicynumericalPrecisionPolicy (...) ()const |
Returns the current default precision policy for the database connection. See also QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy(), and QSqlQuery::setNumericalPrecisionPolicy(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | openopen ( const QString &db , const QString &user=QString() , const QString &password=QString() , const QString &host=QString() , int port=-1 , const QString &options=QString() ...) ( const QString &db , const QString &user=QString() , const QString &password=QString() , const QString &host=QString() , int port=-1 , const QString &options=QString() ) [pure virtual] |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | QSqlIndex QSqlDriver | primaryIndexprimaryIndex ( const QString &tableName ) ( const QString &tableName )const [virtual] |
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. | ||
Look up this member in the source code. | ||
| Toggle details | QSqlRecord QSqlDriver | recordrecord ( const QString &tableName ) ( const QString &tableName )const [virtual] |
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. | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | rollbackTransactionrollbackTransaction () () [virtual] |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | void QSqlDriver | setLastErrorsetLastError ( const QSqlError &error ) ( const QSqlError &error ) [virtual protected] |
This function is used to set the value of the last error, error, that occurred on the database. See also lastError(). | ||
Look up this member in the source code. | ||
| Toggle details | void QSqlDriver | setNumericalPrecisionPolicysetNumericalPrecisionPolicy ( QSql::NumericalPrecisionPolicy precisionPolicy ...) ( 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. See also QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy(), and QSqlQuery::numericalPrecisionPolicy(). | ||
Look up this member in the source code. | ||
| Toggle details | void QSqlDriver | setOpensetOpen ( bool open ) ( bool open ) [virtual protected] |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | void QSqlDriver | setOpenErrorsetOpenError ( bool error ) ( bool error ) [virtual protected] |
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(). | ||
Look up this member in the source code. | ||
| Toggle details | QString QSqlDriver | sqlStatementsqlStatement ( StatementType type , const QString &tableName , const QSqlRecord &rec , bool preparedStatement ...) ( StatementType type , const QString &tableName , const QSqlRecord &rec , bool preparedStatement )const [virtual] |
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. | ||
Look up this member in the source code. | ||
| Toggle details | QString QSqlDriver | stripDelimitersstripDelimiters ( const QString &identifier , IdentifierType type ...) ( const QString &identifier , IdentifierType type )const |
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. See also isIdentifierEscaped(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | subscribeToNotificationsubscribeToNotification ( const QString &name ) ( 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. 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. See also unsubscribeFromNotification(), subscribedToNotifications(), and QSqlDriver::hasFeature(). | ||
Look up this member in the source code. | ||
| Toggle details | QStringList QSqlDriver | subscribedToNotificationssubscribedToNotifications () ()const |
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. See also subscribeToNotification() and unsubscribeFromNotification(). | ||
Look up this member in the source code. | ||
| Toggle details | QStringList QSqlDriver | tablestables ( QSql::TableType tableType ) ( QSql::TableType tableType )const [virtual] |
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. | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | unsubscribeFromNotificationunsubscribeFromNotification ( const QString &name ...) ( 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() 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. See also subscribeToNotification() and subscribedToNotifications(). | ||
Look up this member in the source code. | ||
| Toggle details | QString QSqlDriver | formatValueformatValue ( const QSqlField *field ,
bool
trimStrings=false ...) ( const QSqlField *field ,
bool
trimStrings=false )const |
Use the other formatValue() overload instead. | ||
Look up this member in the source code. | ||
| Toggle details | QString QSqlDriver | nullTextnullText () ()const |
sqlStatement() is now used to generate SQL. Use tr("NULL") for example, instead. | ||
Look up this member in the source code. | ||
| Toggle details | QSqlRecord QSqlDriver | recordrecord ( const QSqlQuery &query ) ( const QSqlQuery &query )const |
Use query.record() instead. | ||
Look up this member in the source code. | ||
| Toggle details | QSqlRecord QSqlDriver | recordInforecordInfo ( const QString &tablename ) ( const QString &tablename )const |
Use record() instead. | ||
Look up this member in the source code. | ||
| Toggle details | QSqlRecord QSqlDriver | recordInforecordInfo ( const QSqlQuery &query ) ( const QSqlQuery &query )const |
Use query.record() instead. | ||
Look up this member in the source code. | ||
Signals
| Toggle details | void QSqlDriver | notificationnotification ( const QString &name ) ( const QString &name ) [signal] |
This signal is emitted when the database posts an event notification that the driver subscribes to. name identifies the event notification. See also subscribeToNotification(). | ||
Look up this member in the source code. | ||
Public Slots
| Toggle details | bool QSqlDriver | isIdentifierEscapedImplementationisIdentifierEscapedImplementation ( const QString &identifier , IdentifierType type ...) ( const QString &identifier , IdentifierType type )const[protected] [slot] |
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. See also isIdentifierEscaped(). | ||
Look up this member in the source code. | ||
| Toggle details | QString QSqlDriver | stripDelimitersImplementationstripDelimitersImplementation ( const QString &identifier , IdentifierType type ...) ( const QString &identifier , IdentifierType type )const[protected] [slot] |
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. See also stripDelimiters(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | subscribeToNotificationImplementationsubscribeToNotificationImplementation ( const QString &name ...) ( const QString &name )[protected] [slot] |
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. See also subscribeToNotification(). | ||
Look up this member in the source code. | ||
| Toggle details | QStringList QSqlDriver | subscribedToNotificationsImplementationsubscribedToNotificationsImplementation (...) ()const[protected] [slot] |
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. See also subscribedToNotifications(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QSqlDriver | unsubscribeFromNotificationImplementationunsubscribeFromNotificationImplementation ( const QString &name ...) ( const QString &name )[protected] [slot] |
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. See also unsubscribeFromNotification(). | ||
Look up this member in the source code. | ||



Votes: 2
Coverage: Qt library 4.7, 4.8, 5.0
Area 51 Engineer
27 notes
Using QSqlDriver directly for database portability
The documentation on this page states:
However, if you want to write applications that can work with any SQL database, you will have to use this class directly. The problem is that there are differences in SQL dialects between database backends. Using QSqlDriver, you can keep your code mostly free of these differences. The main trick is to not use SQL in your code directly. Instead, you can use QSqlDriver::sqlStatement to generate SQL statements for you. While this may seem a bit weird and inefficient at first, often the resulting code is not significantly longer than constructing string-based SQL statement, especially for more complex cases.
A limitation of using sqlStatement is that it does not support Data Definition Language (DDL), only Data Manipulation Language (DML). That means that only the generation of statements that manipulate the data in the database itself are supported, but the generation of statements that manipulate the structure of the data (creating new tables, renaming a column in a table, etc.) are not. Also, there is no support for things like limiting your result set or special operators that may have different names on different platforms, grouping and agregate functions, or complex WHERE statements.
However, even with these limitations, do not fall in the trap of sprinking code for these throughout your application. Instead, build an abstraction layer to generate these kinds of statements for you based on which driver is used, and keep the code for this in one place. A solution I used personally involved extending QSqlDriver in a subclass, extending sqlStatement and then subclassing this extended base class with a new set of concrete drivers.
[Revisions]