Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QSqlDatabase Class Reference

The QSqlDatabase class represents a connection to a database. More...

    #include <QSqlDatabase>

- List of all members, including inherited members
- Compatibility members

### Public Functions

- QSqlDatabase ()
- QSqlDatabase ( const QSqlDatabase & other )
- ~QSqlDatabase ()
- void close ()
- bool commit ()
- QString connectOptions () const
- QString databaseName () const
- QSqlDriver * driver () const
- QString driverName () const
- QSqlQuery exec ( const QString & query = QString() ) const
- QString hostName () const
- bool isOpen () const
- bool isOpenError () const
- bool isValid () const
- QSqlError lastError () const
- bool open ()
- bool open ( const QString & user, const QString & password )
- QString password () const
- int port () const
- QSqlIndex primaryIndex ( const QString & tablename ) const
- QSqlRecord record ( const QString & tablename ) const
- bool rollback ()
- void setConnectOptions ( const QString & options = QString() )
- void setDatabaseName ( const QString & name )
- void setHostName ( const QString & host )
- void setPassword ( const QString & password )
- void setPort ( int p )
- void setUserName ( const QString & name )
- QStringList tables ( QSql::TableType type = QSql::Tables ) const
- bool transaction ()
- QString userName () const
- QSqlDatabase & operator= ( const QSqlDatabase & other )

### Static Public Members

- QSqlDatabase addDatabase ( const QString & type, const QString & connectionName = QLatin1String( defaultConnection ) )
- QSqlDatabase addDatabase ( QSqlDriver * driver, const QString & connectionName = QLatin1String( defaultConnection ) )
- QSqlDatabase cloneDatabase ( const QSqlDatabase & other, const QString & connectionName )
- QStringList connectionNames ()
- bool contains ( const QString & connectionName = QLatin1String( defaultConnection ) )
- QSqlDatabase database ( const QString & connectionName = QLatin1String( defaultConnection ), bool open = true )
- QStringList drivers ()
- bool isDriverAvailable ( const QString & name )
- void registerSqlDriver ( const QString & name, QSqlDriverCreatorBase * creator )
- void removeDatabase ( const QString & connectionName )

### Protected Functions

- QSqlDatabase ( const QString & type )
- QSqlDatabase ( QSqlDriver * driver )

## Detailed Description

The QSqlDatabase class represents a connection to a database.

Note that QSqlDatabase is implemented as a smart pointer to a database connection. It is reference counted, but not implicitly shared, which means that if one copy of a QSqlDatabase object is modified, it will affect all other copies.

        QSqlDatabase db = addDatabase("QPSQL7"); // No connection name => default connection
        db.setUserName("mark");
        db.setDatabaseName("bunnies"); // Database-specific
        if (db.open()) {
            // ...
        }
        // ... later
        QSqlDatabase db = database(); // No connection name => default connection
        if (db.isOpen()) {
            // ...
        }

You must use addDatabase() or database() to aquire QSqlDatabase objects and removeDatabase() to remove connections. Since QSqlDatabase is reference counted, it will output a warning if you try to remove a connection when it is still in use.

Note that transaction handling is not supported by every SQL database. You can find out whether transactions are supported using QSqlDriver::hasFeature().

The QSqlDatabase class provides an abstract interface for accessing many types of database backends. Database-specific drivers are used internally to actually access and manipulate data, (see QSqlDriver). Result set objects provide the interface for executing and manipulating SQL queries (see QSqlQuery).

Once a database object has been created you can set the connection parameters with setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(). Once the parameters have been set up you can call open() to open the connection.

Once a connection is established you can see what tables the database offers with tables(), find the primary index for a table with primaryIndex(), get meta-information about a table's fields (e.g. their names) with record(), and execute a query with exec(). If transactions are supported you can use transaction() to start a transaction, and then commit() or rollback() to complete it. If an error occurred it is given by lastError().

The normal way to get a QSqlDatabase object to open a connection is to call addDatabase(). Use database() to get a reference to an existing database connection (e.g. one created by addDatabase()). Use contains() to see if a given connection name is in the list of connections. Connections can be removed with removeDatabase().

The names of the underlying SQL drivers are available from drivers(); you can check for a particular driver with isDriverAvailable(). If you have created your own custom driver you can register it with registerSqlDriver().

## Member Function Documentation

### QSqlDatabase::QSqlDatabase ()

Creates an empty, invalid QSqlDatabase object. Note that you should never create QSqlDatabase objects like this; instead you should use addDatabase(), removeDatabase() and database() to get QSqlDatabase objects.

### QSqlDatabase::QSqlDatabase ( const QSqlDatabase & other )

Creates a copy of other.

### QSqlDatabase::QSqlDatabase ( const QString & type ) `[protected]`

Creates a QSqlDatabase connection that uses the driver referred to by type. If the type is not recognized, the database connection will have no functionality.

The currently available driver types are:

Driver Type | Description
--- | ---
QDB2 | IBM DB2, v7.1 and higher
QIBASE | Borland Interbase Driver
QMYSQL3 | MySQL Driver
QOCI8 | Oracle Call Interface Driver
QODBC3 | ODBC Driver (includes Microsoft SQL Server)
QPSQL7 | PostgreSQL v6.x and v7.x Driver
QSQLITE | SQLite Driver
QTDS7 | Sybase Adaptive Server

Additional third party drivers, including your own custom drivers, can be loaded dynamically.

See also registerSqlDriver().

### QSqlDatabase::QSqlDatabase ( QSqlDriver * driver ) `[protected]`

Creates a database connection using the given driver.

### QSqlDatabase::~QSqlDatabase ()

Destroys the object, closes the connection, and frees any allocated resources.

### QSqlDatabase QSqlDatabase::addDatabase ( const QString & type, const QString & connectionName = QLatin1String( defaultConnection ) ) `[static]`

Adds a database to the list of database connections using the driver type and the connection name connectionName.

The database connection is referred to by connectionName. The newly added database connection is returned.

If connectionName is not specified, the newly added database connection becomes the default database connection for the application, and subsequent calls to database() (without a database name parameter) will return a reference to it. If connectionName is given, use database(connectionName) to retrieve a pointer to the database connection.

To make use of the connection you will need to set it up, for example by calling some or all of setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(), and then you'll need to open() the connection.

See also database() and removeDatabase().

### QSqlDatabase QSqlDatabase::addDatabase ( QSqlDriver * driver, const QString & connectionName = QLatin1String( defaultConnection ) ) `[static]`

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function is useful if you need to set up the database connection and instantiate the driver yourself. If you do this, it is recommended that you include the driver code in your own application. For example, setting up a custom PostgreSQL connection and instantiating the QPSQL7 driver can be done like this:

        #include "qtdir/src/sql/drivers/psql/qsql_psql.cpp"

(We assume that `qtdir` is the directory where Qt is installed.) This will pull in the code that is needed to use the PostgreSQL client library and to instantiate a QPSQLDriver object, assuming that you have the PostgreSQL headers somewhere in your include search path.

        PGconn* con = PQconnectdb("host=server user=bart password=simpson dbname=springfield");
        QPSQLDriver* drv =  new QPSQLDriver(con);
        QSqlDatabase db = QSqlDatabase::addDatabase(drv); // becomes the new default connection
        QSqlQuery query;
        query.exec("SELECT NAME, ID FROM STAFF");
        ...

The above code sets up a PostgreSQL connection and instantiates a QPSQLDriver object. Next, addDatabase() is called to add the connection to the known connections so that it can be used by the Qt SQL classes. When a driver is instantiated with a connection handle (or set of handles), Qt assumes that you have already opened the database connection.

Remember that you must link your application against the database client library as well. The simplest way to do this is to add lines like the ones below to your `.pro` file:

        unix:LIBS += -lpq
        win32:LIBS += libpqdll.lib

You will need to have the client library in your linker's search path.

The method described above will work for all the drivers, the only difference is the arguments the driver constructors take. Below is an overview of the drivers and their constructor arguments.

Driver | Class name | Constructor arguments | File to include
--- | --- | --- | ---
QPSQL7 | QPSQLDriver | PGconn* connection | `qsql_psql.cpp`
QMYSQL3 | QMYSQLDriver | MYSQL* connection | `qsql_mysql.cpp`
QOCI8 | QOCIDriver | OCIEnv* environment, OCIError* error, OCISvcCtx* serviceContext | `qsql_oci.cpp`
QODBC3 | QODBCDriver | SQLHANDLE environment, SQLHANDLE connection | `qsql_odbc.cpp`
QDB2 | QDB2 | SQLHANDLE environment, SQLHANDLE connection | `qsql_db2.cpp`
QTDS7 | QTDSDriver | LOGINREC* loginRecord, DBPROCESS* dbProcess, const QString& hostName | `qsql_tds.cpp`
QSQLITE | QSQLiteDriver | sqlite* connection | `qsql_sqlite.cpp`
QIBASE | QIBaseDriver | isc_db_handle connection | `qsql_ibase.cpp`

Note: The host name (or service name) is needed when constructing the QTDSDriver for creating new connections for internal queries. This is to prevent the simultaneous usage of several QSqlQuery/QSqlCursor objects from blocking each other.

Warning: The SQL framework takes ownership of the driver pointer, and it should not be deleted. If you want to explicitly remove the connection, use removeDatabase()

See also drivers().

### QSqlDatabase QSqlDatabase::cloneDatabase ( const QSqlDatabase & other, const QString & connectionName ) `[static]`

Clones the database connection other and and stores it as connectionName. All the settings from the original database, e.g. databaseName(), hostName(), etc., are copied across. Does nothing if other is an invalid database. Returns the newly created database connection. Note that the connection is not opened, to use it, it is neccessary to call open() first.

### void QSqlDatabase::close ()

Closes the database connection, freeing any resources acquired.

See also removeDatabase().

### bool QSqlDatabase::commit ()

Commits a transaction to the database if the driver supports transactions and a transaction() has been started. Returns true if the operation succeeded; otherwise returns false.

See also QSqlDriver::hasFeature() and rollback().

### QString QSqlDatabase::connectOptions () const

Returns the connection options string used for this connection; the string may be empty.

See also setConnectOptions().

### QStringList QSqlDatabase::connectionNames () `[static]`

Returns a list containing the names of all connections

### bool QSqlDatabase::contains ( const QString & connectionName = QLatin1String( defaultConnection ) ) `[static]`

Returns true if the list of database connections contains connectionName; otherwise returns false.

### QSqlDatabase QSqlDatabase::database ( const QString & connectionName = QLatin1String( defaultConnection ), bool open = true ) `[static]`

Returns the database connection called connectionName. The database connection must have been previously added with addDatabase(). If open is true (the default) and the database connection is not already open it is opened now. If no connectionName is specified the default connection is used. If connectionName does not exist in the list of databases, an invalid connection is returned.

See also isOpen().

### QString QSqlDatabase::databaseName () const

Returns the connection's database name; it may be empty.

See also setDatabaseName().

### QSqlDriver * QSqlDatabase::driver () const

Returns the database driver used to access the database connection.

See also addDatabase() and drivers().

### QString QSqlDatabase::driverName () const

Returns the connection's driver name.

See also addDatabase() and driver().

### QStringList QSqlDatabase::drivers () `[static]`

Returns a list of all the available database drivers.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

        QStringList list = QSqlDatabase::drivers();
        QStringList::Iterator i = list.begin();
        while(i != list.end()) {
            myProcessing(*i);
            ++i;
        }

### QSqlQuery QSqlDatabase::exec ( const QString & query = QString() ) const

Executes a SQL statement (e.g. an `INSERT`, `UPDATE` or `DELETE` statement), on the database, and returns a QSqlQuery object. Use lastError() to retrieve error information. If query is empty, an empty, invalid query is returned and lastError() is not affected.

See also QSqlQuery and lastError().

### QString QSqlDatabase::hostName () const

Returns the connection's host name; it may be empty.

See also setHostName().

### bool QSqlDatabase::isDriverAvailable ( const QString & name ) `[static]`

Returns true if a driver called name is available; otherwise returns false.

See also drivers().

### bool QSqlDatabase::isOpen () const

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

### bool QSqlDatabase::isOpenError () const

Returns true if there was an error opening the database connection; otherwise returns false. Error information can be retrieved using the lastError() function.

### bool QSqlDatabase::isValid () const

Returns true if the QSqlDatabase has a valid driver.

        QSqlDatabase db;
        qDebug() << db.isValid(); // false, db is not valid
        db = QSqlDatabase::database("sales");
        qDebug() << db.isValid(); // true if "sales" connection exists
        QSqlDatabase::removeDatabase("sales");
        qDebug() << db.isValid(); // false since "sales" does not exist anymore

### QSqlError QSqlDatabase::lastError () const

Returns information about the last error that occurred on the database. See QSqlError for more information.

### bool QSqlDatabase::open ()

Opens the database connection using the current connection values. Returns true on success; otherwise returns false. Error information can be retrieved using the lastError() function.

See also lastError(), setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions().

### bool QSqlDatabase::open ( const QString & user, const QString & password )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Opens the database connection using the given user name and password. Returns true on success; otherwise returns false. Error information can be retrieved using the lastError() function.

This function does not store the password it is given. Instead, the password is passed directly to the driver for opening the connection and it is then discarded.

See also lastError().

### QString QSqlDatabase::password () const

Returns the connection's password. If the password was not set with setPassword(), and if the password was given in the open() call, or if no password was used, an empty string is returned.

### int QSqlDatabase::port () const

Returns the connection's port number. The value is undefined if the port number has not been set, for example using setPort().

### QSqlIndex QSqlDatabase::primaryIndex ( const QString & tablename ) const

Returns the primary index for table tablename. If no primary index exists an empty QSqlIndex is returned.

### QSqlRecord QSqlDatabase::record ( const QString & tablename ) const

Returns a QSqlRecord populated with the names of all the fields in the table (or view) called tablename. The order in which the fields appear in the record is undefined. If no such table (or view) exists, an empty record is returned.

### void QSqlDatabase::registerSqlDriver ( const QString & name, QSqlDriverCreatorBase * creator ) `[static]`

This function registers a new SQL driver called name, within the SQL framework. This is useful if you have a custom SQL driver and don't want to compile it as a plugin.

Example:

        QSqlDatabase::registerSqlDriver("MYDRIVER", new QSqlDriverCreator<MyDatabaseDriver>);
        QSqlDatabase db = QSqlDatabase::addDatabase("MYDRIVER");
        ...

Warning: The framework takes ownership of the creator pointer, so it should not be deleted.

### void QSqlDatabase::removeDatabase ( const QString & connectionName ) `[static]`

Removes the database connection connectionName from the list of database connections.

Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.

Since QSqlDatabase is reference counted, a warning will be issued if the connection is still in use.

        // WRONG
        QSqlDatabase db = QSqlDatabase::database("sales");
        QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
        QSqlDatabase::removeDatabase("sales"); // will output a warning
        // "db" is now a dangling invalid database connection,
        // "query" contains an invalid result set

The correct way to do it:

        {
            QSqlDatabase db = QSqlDatabase::database("sales");
            QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
        }
        // Both "db" and "query" are destroyed because they are out of scope
        QSqlDatabase::removeDatabase("sales"); // correct

### bool QSqlDatabase::rollback ()

Rolls a transaction back on the database if the driver supports transactions and a transaction() has been started. Returns true if the operation succeeded; otherwise returns false.

See also QSqlDriver::hasFeature() and commit().

### void QSqlDatabase::setConnectOptions ( const QString & options = QString() )

Sets database-specific options. This must be done before the connection is opened or it has no effect; (or you can close() the connection, call this function and open() the connection again).

The format of the options string is a semicolon separated list of option names or option=value pairs. The options depend on the database client used:

ODBC | MySQL | PostgreSQL
--- | --- | ---

- SQL_ATTR_ACCESS_MODE
- SQL_ATTR_LOGIN_TIMEOUT
- SQL_ATTR_CONNECTION_TIMEOUT
- SQL_ATTR_CURRENT_CATALOG
- SQL_ATTR_METADATA_ID
- SQL_ATTR_PACKET_SIZE
- SQL_ATTR_TRACEFILE
- SQL_ATTR_TRACE
- SQL_ATTR_CONNECTION_POOLING

- CLIENT_COMPRESS
- CLIENT_FOUND_ROWS
- CLIENT_IGNORE_SPACE
- CLIENT_SSL
- CLIENT_ODBC
- CLIENT_NO_SCHEMA
- CLIENT_INTERACTIVE

- connect_timeout
- options
- tty
- requiressl
- service

DB2 | OCI | TDS

- SQL_ATTR_ACCESS_MODE
- SQL_ATTR_LOGIN_TIMEOUT

none

none

Examples:

        ...
        // MySQL connection
        db.setConnectOptions("CLIENT_SSL;CLIENT_IGNORE_SPACE"); // use an SSL connection to the server
        if (!db.open()) {
            db.setConnectOptions(); // clears the connect option string
            ...
        }
        ...
        // PostgreSQL connection
        db.setConnectOptions("requiressl=1"); // enable PostgreSQL SSL connections
        if (!db.open()) {
            db.setConnectOptions(); // clear options
            ...
        }
        ...
        // ODBC connection
        db.setConnectOptions("SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON"); // set ODBC options
        if (!db.open()) {
            db.setConnectOptions(); // don't try to set this option
            ...
        }

Refer to the client library documentation for more information about the different options.

See also connectOptions().

### void QSqlDatabase::setDatabaseName ( const QString & name )

Sets the connection's name to name. This must be done before the connection is opened or it has no effect; (or you can close() the connection, call this function and open() the connection again). The name is database-specific.

For the QOCI8 (Oracle) driver the database name is the TNS Service Name.

For the QODBC3 driver the name can either be a DSN, a DSN filename (in which case the file must have a `.dsn` extension), or a connection string.

For example, Microsoft Access users can use the following connection string to open an `.mdb` file directly, instead of having to create a DSN entry in the ODBC manager:

        ...
        db = QSqlDatabase::addDatabase("QODBC3");
        db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
        if (db.open()) {
            // success!
        }
        ...

("FIL" is the required spelling in Microsoft's API.)

There is no default value.

See also databaseName(), setUserName(), setPassword(), setHostName(), setPort(), setConnectOptions(), and open().

### void QSqlDatabase::setHostName ( const QString & host )

Sets the connection's host name to host. This must be done before the connection is opened or it has no effect; (or you can close() the connection, call this function and open() the connection again).

There is no default value.

See also hostName(), setUserName(), setPassword(), setDatabaseName(), setPort(), setConnectOptions(), and open().

### void QSqlDatabase::setPassword ( const QString & password )

Sets the connection's password to password. This must be done before the connection is opened or it has no effect; (or you can close() the connection, call this function and open() the connection again).

There is no default value.

Warning: This function stores the password in plain text within Qt. Use the open() call that takes a password as parameter to avoid this behavior.

See also password(), setUserName(), setDatabaseName(), setHostName(), setPort(), setConnectOptions(), and open().

### void QSqlDatabase::setPort ( int p )

Sets the connection's port number to p. This must be done before the connection is opened or it has no effect; (or you can close() the connection, call this function and open() the connection again).

There is no default value.

setDatabaseName() setConnectOptions() open()

See also port(), setUserName(), setPassword(), and setHostName().

### void QSqlDatabase::setUserName ( const QString & name )

Sets the connection's user name to name. This must be done before the connection is opened or it has no effect; (or you can close() the connection, call this function and open() the connection again).

There is no default value.

setPort() setConnectOptions() open()

See also userName(), setDatabaseName(), setPassword(), and setHostName().

### QStringList QSqlDatabase::tables ( QSql::TableType type = QSql::Tables ) const

Returns a list of the database's tables, system tables and views, as specified by the parameter type.

### bool QSqlDatabase::transaction ()

Begins a transaction on the database if the driver supports transactions. Returns true if the operation succeeded; otherwise returns false.

See also QSqlDriver::hasFeature(), commit(), and rollback().

### QString QSqlDatabase::userName () const

Returns the connection's user name; it may be empty.

See also setUserName().

### QSqlDatabase & QSqlDatabase::operator= ( const QSqlDatabase & other )

Assigns other to this object.