Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QSqlDatabase class is used to create SQL database connections and to provide transaction handling. More...
#include <qsqldatabase.h>
Inherits QObject.
This class is used to create connections to SQL databases. It also provides transaction handling functions for those database drivers that support transactions.
The QSqlDatabase class itself 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).
See also Database Classes.
The currently available drivers are:
Driver Type | Description |
---|---|
QODBC3 | ODBC (Open Database Connectivity) Driver |
QOCI8 | Oracle Call Interface Driver |
QPSQL7 | PostgreSQL v6.x and v7.x Driver |
QTDS7 | Sybase Adaptive Server and Microsoft SQL Server Driver |
QMYSQL3 | MySQL Driver |
QDB2 | IBM DB2, v7.1 and higher |
Note that additional 3rd party drivers can be loaded dynamically.
See also registerSqlDriver().
Creates a database connection using the driver driver, with the parent parent and the object name objname.
Warning: The framework takes ownership of the driver pointer, so it should not be deleted.
The database connection is referred to by connectionName. The newly added database connection is returned. This pointer is owned by QSqlDatabase and will be deleted on program exit or when removeDatabase() is called. 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 pointer to it.
See also database() and removeDatabase().
Examples: sql/overview/connect1/main.cpp, sql/overview/connection.cpp, and sql/sqltable/main.cpp.
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 the following way:
#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 q; q.exec( "SELECT * FROM persons" ); ...
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 that is to add something similar to the following lines 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 |
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 framework takes ownership of the driver pointer, and it should not be deleted. The returned QSqlDatabase object is owned by the framework and must not be deleted. If you want to explicitly remove the connection, use removeDatabase()
See also drivers().
See also QSqlDriver::hasFeature() and rollback().
Returns the database connect options. See the "connectOptions" property for details.
Examples: sql/overview/basicbrowsing/main.cpp and sql/overview/create_connections/main.cpp.
Returns the name of the database. See the "databaseName" property for details.
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
QStringList list = QSqlDatabase::drivers(); QStringList::Iterator it = list.begin(); while( it != list.end() ) { myProcessing( *it ); ++it; }
See also QSqlQuery and lastError().
Returns the host name where the database resides. See the "hostName" property for details.
See also drivers().
Examples: sql/overview/connection.cpp and sql/sqltable/main.cpp.
See also lastError().
Examples: sql/overview/connect1/main.cpp, sql/overview/connection.cpp, and sql/sqltable/main.cpp.
Opens the database connection using user name and password. Returns TRUE on success; otherwise returns FALSE. Error information can be retrieved using the lastError() function.
Note: For security reasons this function will not store the password internally in any Qt classes. The password is passed directly to the driver for opening a connection and then discarded.
See also lastError().
Returns the password used to connect to the database. See the "password" property for details.
Returns the port used to connect to the database. See the "port" property for details.
See also recordInfo().
Returns a QSqlRecord populated with the names of all the fields used in the SQL query. If the query is a "SELECT *" the order in which fields are returned is undefined.
See also recordInfo().
See also QSqlRecordInfo, QSqlFieldInfo, and record().
Returns a QSqlRecordInfo object with meta data for the QSqlQuery query. Note that this overloaded function may return less information than the recordInfo() function which takes the name of a table as parameter.
See also QSqlRecordInfo, QSqlFieldInfo, and record().
Example usage:
QSqlDatabase::registerSqlDriver( "MYDRIVER", new QSqlDriverCreator<MySqlDriver> ); QSqlDatabase* db = QSqlDatabase::addDatabase( "MYDRIVER" ); ...
Warning: The framework takes ownership of the creator pointer, so it should not be deleted.
Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.
Removes the database connection db from the list of database connections. The QSqlDatabase object is destroyed when it is removed from the list.
Warning: The db pointer is not valid after this function has been called. There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.
See also QSqlDriver::hasFeature(), commit(), and transaction().
Sets the database connect options to options. See the "connectOptions" property for details.
Sets the name of the database to name. See the "databaseName" property for details.
Sets the host name where the database resides to host. See the "hostName" property for details.
Sets the password used to connect to the database to password. See the "password" property for details.
Sets the port used to connect to the database to p. See the "port" property for details.
Sets the user name connected to the database to name. See the "userName" property for details.
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
QStringList list = myDatabase.tables( QSql::Tables | QSql::Views ); QStringList::Iterator it = list.begin(); while( it != list.end() ) { myProcessing( *it ); ++it; }
Returns a list of tables visibel to the user in the database. To retrieve also the views or system tables, use the function above.
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
QStringList list = myDatabase.tables(); QStringList::Iterator it = list.begin(); while( it != list.end() ) { myProcessing( *it ); ++it; }
See also QSqlDriver::hasFeature(), commit(), and rollback().
Returns the user name connected to the database. See the "userName" property for details.
This property holds the database connect options.
The format of the options string is a semicolon separated list of options or option = value pairs.
The options supported depend on the database client used:
ODBC | MySQL | PostgreSQL |
---|---|---|
|
|
|
DB2 | OCI | TDS |
|
none
|
none
|
Example of usage:
... // 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 ... }
Please refer to the client library documentation for more information about the different options. The options will be set prior to opening the database connection. Setting new options without re-opening the connection does nothing.
See also
Set this property's value with setConnectOptions() and get this property's value with connectOptions().
This property holds the name of the database.
Note that the database name is the TNS Service Name for the QOCI8 (Oracle) driver.
For the QODBC3 driver it can either be a DSN, a DSN filename (the file must have a .dsn extension), or a connection string. MS Access users can for example use the following connection string to open a .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! } ...
There is no default value.
Set this property's value with setDatabaseName() and get this property's value with databaseName().
This property holds the host name where the database resides.
There is no default value.
Set this property's value with setHostName() and get this property's value with hostName().
This property holds the password used to connect to the database.
There is no default value.
Warning: This function will store the password in a non-encrypted form internally in Qt. Use the open() call that takes a password as parameter to avoid this behaviour.
See also open().
Set this property's value with setPassword() and get this property's value with password().
This property holds the port used to connect to the database.
There is no default value.
Set this property's value with setPort() and get this property's value with port().
This property holds the user name connected to the database.
There is no default value.
Set this property's value with setUserName() and get this property's value with userName().
This file is part of the Qt toolkit. Copyright © 1995-2003 Trolltech. All Rights Reserved.
Copyright © 2003 Trolltech | Trademarks | Qt version 3.2.0b2
|