Note that transaction handling is not supported by every SQL database. You can find out whether transactions are supported using TQSqlDriver::hasFeature().
The TQSqlDatabase 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 TQSqlDriver). Result set objects provide the interface for executing and manipulating SQL queries (see TQSqlQuery).
Creates a TQSqlDatabase 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.
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.
The database connection is referred to by \fIconnectionName\fR. The newly added database connection is returned. This pointer is owned by TQSqlDatabase and will be deleted on program exit or when removeDatabase() is called.
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 TQSqlQuery/TQSqlCursor objects from blocking each other.
\fBWarning:\fR The SQL framework takes ownership of the \fIdriver\fR pointer, and it should not be deleted. The returned TQSqlDatabase 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 TQSqlDatabase 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 TQSqlQuery 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 there was an error opening the database connection; otherwise returns FALSE. Error information can be retrieved using the lastError() function.
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 TQSqlRecord 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.
Returns a TQSqlRecord 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 TQSqlRecordInfo populated with meta data about the table or view \fItablename\fR. If no such table (or view) exists, an empty record is returned.
Returns a TQSqlRecordInfo object with meta data for the TQSqlQuery \fIquery\fR. Note that this overloaded function may return less information than the recordInfo() function which takes the name of a table as parameter.
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.
Removes the database connection \fIdb\fR from the list of database connections. The TQSqlDatabase object is destroyed when it is removed from the list.
\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.
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.
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().