The QSqlDatabase class is used to create SQL database connections and to provide transaction handling.
.PP
Note that transaction handling is not supported by every SQL database. You can find out whether transactions are supported using QSqlDriver::hasFeature().
.PP
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).
Creates a QSqlDatabase connection called \fIname\fR that uses the driver referred to by \fItype\fR, with the parent \fIparent\fR and the object name \fIobjname\fR. If the \fItype\fR is not recognized, the database connection will have no functionality.
.PP
The currently available drivers are:
.PP
<center>.nf
.TS
l - l. Driver Type Description QODBC3 ODBC Driver (includes Microsoft SQL Server) QOCI8 Oracle Call Interface Driver QPSQL7 PostgreSQL v6.x and v7.x Driver QTDS7 Sybase Adaptive Server QMYSQL3 MySQL Driver QDB2 IBM DB2, v7.1 and higher QSQLITE SQLite Driver QIBASE
.TE
.fi
</center>
.PP
Additional third party drivers, including your own custom drivers, can be loaded dynamically.
Adds a database to the list of database connections using the driver \fItype\fR and the connection name \fIconnectionName\fR.
.PP
The database connection is referred to by \fIconnectionName\fR. 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.
.PP
If \fIconnectionName\fR 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. If \fIconnectionName\fR is given, use database(connectionName) to retrieve a pointer to the database connection.
.PP
\fBWarning:\fR If you add a database with the same name as an existing database, the new database will replace the old one. This will happen automatically if you call this function more than once without specifying \fIconnectionName\fR.
.PP
See also database() and removeDatabase().
.PP
Examples:
.)l sql/overview/connect1/main.cpp, sql/overview/connection.cpp, and sql/sqltable/main.cpp.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
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:
(We assume that \fCqtdir\fR is the directory where TQt 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.
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 TQt SQL classes. When a driver is instantiated with a connection handle (or set of handles), TQt 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 those below to your \fC.pro\fR file:
.PP
.nf
.br
unix:LIBS += -lpq
.br
win32:LIBS += libpqdll.lib
.br
.fi
.PP
You will need to have the client library in your linker's search path.
.PP
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.
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.
.PP
\fBWarning:\fR The SQL framework takes ownership of the \fIdriver\fR 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()
Returns the database connection called \fIconnectionName\fR. The database connection must have been previously added with addDatabase(). If \fIopen\fR is TRUE (the default) and the database connection is not already open it is opened now. If no \fIconnectionName\fR is specified the default connection is used. If \fIconnectionName\fR does not exist in the list of databases, 0 is returned. The pointer returned is owned by QSqlDatabase and should \fInot\fR be deleted.
\fBWarning:\fR There are restrictions on the use of database connections in threaded applications. Please see the Thread Support in TQt document for more information about threading and SQL databases.
Executes a SQL statement (e.g. an \fCINSERT\fR, \fCUPDATE\fR or \fCDELETE\fR statement) on the database, and returns a QSqlQuery object. Use lastError() to retrieve error information. If \fIquery\fR is TQString::null, an empty, invalid query is returned and lastError() is not affected.
Returns TRUE if a driver called \fIname\fR is available; otherwise returns FALSE.
.PP
See also drivers().
.SH "bool QSqlDatabase::isOpen () const"
Returns TRUE if the database connection is currently open; otherwise returns FALSE.
.SH "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.
.SH "QSqlError QSqlDatabase::lastError () const"
Returns information about the last error that occurred on the database. See QSqlError for more information.
.PP
Examples:
.)l sql/overview/connection.cpp and sql/sqltable/main.cpp.
.SH "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.
.PP
See also lastError().
.PP
Examples:
.)l sql/overview/connect1/main.cpp, sql/overview/connection.cpp, and sql/sqltable/main.cpp.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Opens the database connection using the given \fIuser\fR name and \fIpassword\fR. Returns TRUE on success; otherwise returns FALSE. Error information can be retrieved using the lastError() function.
.PP
This function does not store the password it is given. Instead, the password is passed directly to the driver for opening a connection and is then discarded.
Returns a QSqlRecord populated with the names of all the fields in the table (or view) called \fItablename\fR. The order in which the fields appear in the record is undefined. If no such table (or view) exists, an empty record is returned.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns a QSqlRecord populated with the names of all the fields used in the SQL \fIquery\fR. If the query is a "SELECT *" the order in which fields appear in the record is undefined.
Returns a QSqlRecordInfo populated with meta data about the table or view \fItablename\fR. If no such table (or view) exists, an empty record is returned.
.PP
See also QSqlRecordInfo, QSqlFieldInfo, and record().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns a QSqlRecordInfo object with meta data for the QSqlQuery \fIquery\fR. Note that this overloaded function may return less information than the recordInfo() function which takes the name of a table as parameter.
.PP
See also QSqlRecordInfo, QSqlFieldInfo, and record().
This function registers a new SQL driver called \fIname\fR, within the SQL framework. This is useful if you have a custom SQL driver and don't want to compile it as a plugin.
.PP
Example usage:
.PP
.nf
.br
QSqlDatabase::registerSqlDriver( "MYDRIVER", new QSqlDriverCreator<MyDatabaseDriver> );
.br
QSqlDatabase* db = QSqlDatabase::addDatabase( "MYDRIVER" );
.br
...
.br
.fi
.PP
\fBWarning:\fR The framework takes ownership of the \fIcreator\fR pointer, so it should not be deleted.
Removes the database connection \fIconnectionName\fR from the list of database connections.
.PP
\fBWarning:\fR There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.
.SH "void QSqlDatabase::removeDatabase ( QSqlDatabase * db )\fC [static]\fR"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Removes the database connection \fIdb\fR from the list of database connections. The QSqlDatabase object is destroyed when it is removed from the list.
.PP
\fBWarning:\fR The \fIdb\fR 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.
.SH "bool QSqlDatabase::rollback ()"
Rolls a transaction back on the database if the driver supports transactions. Returns TRUE if the operation succeeded; otherwise returns FALSE.
.PP
See also QSqlDriver::hasFeature(), commit(), and transaction().
Sets the user name connected to the database to \fIname\fR. See the "userName" property for details.
.SH "QStringList QSqlDatabase::tables ( QSql::TableType type ) const"
Returns a list of the database's tables, system tables and views, as specified by the parameter \fItype\fR.
.PP
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
.PP
.nf
.br
QStringList list = myDatabase.tables( QSql::Tables | QSql::Views );
.br
QStringList::Iterator it = list.begin();
.br
while( it != list.end() ) {
.br
myProcessing( *it );
.br
++it;
.br
}
.br
.fi
.PP
Example: sql/sqltable/main.cpp.
.SH "QStringList QSqlDatabase::tables () const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns a list of the database's tables that are visible to the user. To include views or system tables, use the version of this function that takes a table \fCtype\fR parameter.
.PP
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
.PP
.nf
.br
QStringList list = myDatabase.tables();
.br
QStringList::Iterator it = list.begin();
.br
while( it != list.end() ) {
.br
myProcessing( *it );
.br
++it;
.br
}
.br
.fi
.SH "bool QSqlDatabase::transaction ()"
Begins a transaction on the database if the driver supports transactions. Returns TRUE if the operation succeeded; otherwise returns FALSE.
.PP
See also QSqlDriver::hasFeature(), commit(), and rollback().
The format of the options string is a semi-colon separated list of option names or option = value pairs. The options depend on the database client used:
db->setConnectOptions( "SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON" ); // set ODBC options
.br
if ( !db->open() ) {
.br
db->setConnectOptions(); // don't try to set this option
.br
...
.br
}
.br
.fi
.PP
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.
.PP
See also
.PP
Set this property's value with setConnectOptions() and get this property's value with connectOptions().
Note that the database name is the TNS Service Name for the QOCI8 (Oracle) driver.
.PP
For the QODBC3 driver it can either be a DSN, a DSN filename (the file must have a \fC.dsn\fR extension), or a connection string. MS Access users can for example use the following connection string to open a \fC.mdb\fR file directly, instead of having to create a DSN entry in the ODBC manager:
This property holds the password used to connect to the database.
.PP
There is no default value.
.PP
\fBWarning:\fR This function stores the password in plain text within Qt. Use the open() call that takes a password as parameter to avoid this behaviour.
.PP
See also open().
.PP
Set this property's value with setPassword() and get this property's value with password().
.SH "int port"
This property holds the port used to connect to the database.
.PP
There is no default value.
.PP
Set this property's value with setPort() and get this property's value with port().