<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/src/sql/qsqldatabase.cpp:276 --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>QSqlDatabase Class</title> <style type="text/css"><!-- h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } body { background: #ffffff; color: black; } --></style> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr bgcolor="#E5E5E5"> <td valign=center> <a href="index.html"> <font color="#004faf">Home</font></a> | <a href="classes.html"> <font color="#004faf">All Classes</font></a> | <a href="mainclasses.html"> <font color="#004faf">Main Classes</font></a> | <a href="annotated.html"> <font color="#004faf">Annotated</font></a> | <a href="groups.html"> <font color="#004faf">Grouped Classes</font></a> | <a href="functions.html"> <font color="#004faf">Functions</font></a> </td> <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QSqlDatabase Class Reference<br><small>[<a href="sql.html">sql module</a>]</small></h1> <p>The QSqlDatabase class is used to create SQL database connections and provide transaction handling. <a href="#details">More...</a> <p><tt>#include <<a href="qsqldatabase-h.html">qsqldatabase.h</a>></tt> <p>Inherits <a href="qobject.html">QObject</a>. <p><a href="qsqldatabase-members.html">List of all member functions.</a> <h2>Public Members</h2> <ul> <li><div class=fn><a href="#~QSqlDatabase"><b>~QSqlDatabase</b></a> ()</div></li> <li><div class=fn>bool <a href="#open"><b>open</b></a> ()</div></li> <li><div class=fn>bool <a href="#open-2"><b>open</b></a> ( const QString & user, const QString & password )</div></li> <li><div class=fn>void <a href="#close"><b>close</b></a> ()</div></li> <li><div class=fn>bool <a href="#isOpen"><b>isOpen</b></a> () const</div></li> <li><div class=fn>bool <a href="#isOpenError"><b>isOpenError</b></a> () const</div></li> <li><div class=fn>QStringList <a href="#tables"><b>tables</b></a> () const</div></li> <li><div class=fn>QSqlIndex <a href="#primaryIndex"><b>primaryIndex</b></a> ( const QString & tablename ) const</div></li> <li><div class=fn>QSqlRecord <a href="#record"><b>record</b></a> ( const QString & tablename ) const</div></li> <li><div class=fn>QSqlRecord <a href="#record-2"><b>record</b></a> ( const QSqlQuery & query ) const</div></li> <li><div class=fn>QSqlRecordInfo <a href="#recordInfo"><b>recordInfo</b></a> ( const QString & tablename ) const</div></li> <li><div class=fn>QSqlRecordInfo <a href="#recordInfo-2"><b>recordInfo</b></a> ( const QSqlQuery & query ) const</div></li> <li><div class=fn>QSqlQuery <a href="#exec"><b>exec</b></a> ( const QString & query = QString::null ) const</div></li> <li><div class=fn>QSqlError <a href="#lastError"><b>lastError</b></a> () const</div></li> <li><div class=fn>bool <a href="#transaction"><b>transaction</b></a> ()</div></li> <li><div class=fn>bool <a href="#commit"><b>commit</b></a> ()</div></li> <li><div class=fn>bool <a href="#rollback"><b>rollback</b></a> ()</div></li> <li><div class=fn>virtual void <a href="#setDatabaseName"><b>setDatabaseName</b></a> ( const QString & name )</div></li> <li><div class=fn>virtual void <a href="#setUserName"><b>setUserName</b></a> ( const QString & name )</div></li> <li><div class=fn>virtual void <a href="#setPassword"><b>setPassword</b></a> ( const QString & password )</div></li> <li><div class=fn>virtual void <a href="#setHostName"><b>setHostName</b></a> ( const QString & host )</div></li> <li><div class=fn>virtual void <a href="#setPort"><b>setPort</b></a> ( int p )</div></li> <li><div class=fn>QString <a href="#databaseName"><b>databaseName</b></a> () const</div></li> <li><div class=fn>QString <a href="#userName"><b>userName</b></a> () const</div></li> <li><div class=fn>QString <a href="#password"><b>password</b></a> () const</div></li> <li><div class=fn>QString <a href="#hostName"><b>hostName</b></a> () const</div></li> <li><div class=fn>QString <a href="#driverName"><b>driverName</b></a> () const</div></li> <li><div class=fn>int <a href="#port"><b>port</b></a> () const</div></li> <li><div class=fn>QSqlDriver * <a href="#driver"><b>driver</b></a> () const</div></li> </ul> <h2>Static Public Members</h2> <ul> <li><div class=fn>QSqlDatabase * <a href="#addDatabase"><b>addDatabase</b></a> ( const QString & type, const QString & connectionName = defaultConnection )</div></li> <li><div class=fn>QSqlDatabase * <a href="#database"><b>database</b></a> ( const QString & connectionName = defaultConnection, bool open = TRUE )</div></li> <li><div class=fn>void <a href="#removeDatabase"><b>removeDatabase</b></a> ( const QString & connectionName )</div></li> <li><div class=fn>bool <a href="#contains"><b>contains</b></a> ( const QString & connectionName = defaultConnection )</div></li> <li><div class=fn>QStringList <a href="#drivers"><b>drivers</b></a> ()</div></li> </ul> <h2>Properties</h2> <ul> <li><div class=fn>QString <a href="#databaseName-prop"><b>databaseName</b></a> - the name of the database or the TNS Service Name for the QOCI8 (Oracle) driver</div></li> <li><div class=fn>QString <a href="#hostName-prop"><b>hostName</b></a> - the host name where the database resides</div></li> <li><div class=fn>QString <a href="#password-prop"><b>password</b></a> - the password used to connect to the database</div></li> <li><div class=fn>int <a href="#port-prop"><b>port</b></a> - the port used to connect to the database</div></li> <li><div class=fn>QString <a href="#userName-prop"><b>userName</b></a> - the user name connected to the database</div></li> </ul> <h2>Protected Members</h2> <ul> <li><div class=fn><a href="#QSqlDatabase"><b>QSqlDatabase</b></a> ( const QString & driver, const QString & name, QObject * parent = 0, const char * objname = 0 )</div></li> </ul> <hr><a name="details"></a><h2>Detailed Description</h2> <p> The QSqlDatabase class is used to create SQL database connections and provide transaction handling. <p> <p> This class is used to create connections to SQL databases. It also provides transaction handling functions for those database drivers that support transactions. <p> The QSqlDatabase class itself provides an abstract interface for accessing many types of database backend. Database-specific drivers are used internally to actually access and manipulate data, (see <a href="qsqldriver.html">QSqlDriver</a>). Result set objects provide the interface for executing and manipulating SQL queries (see <a href="qsqlquery.html">QSqlQuery</a>). <p> <p>See also <a href="database.html">Database Classes</a>. <hr><h2>Member Function Documentation</h2> <h3 class=fn><a name="QSqlDatabase"></a>QSqlDatabase::QSqlDatabase ( const <a href="qstring.html">QString</a> & driver, const <a href="qstring.html">QString</a> & name, <a href="qobject.html">QObject</a> * parent = 0, const char * objname = 0 )<tt> [protected]</tt> </h3> Creates a QSqlDatabase connection named <em>name</em> that uses the driver referred to by <em>driver</em>, with the parent <em>parent</em> and the object name <em>objname</em>. If the <em>driver</em> is not recognized, the database connection will have no functionality. <p> The currently available drivers are: <p> <ul> <li> <a href="sql-driver.html#QODBC3">QODBC3</a> - ODBC (Open Database Connectivity) Driver <li> <a href="sql-driver.html#QOCI8">QOCI8</a> - Oracle Call Interface Driver <li> <a href="sql-driver.html#QPSQL7">QPSQL7</a> - PostgreSQL v6.x and v7.x Driver <li> <a href="sql-driver.html#QTDS7">QTDS7</a> - Sybase Adaptive Server and Microsoft SQL Server Driver <li> <a href="sql-driver.html#QMYSQL3">QMYSQL3</a> - MySQL Driver </ul> <p> Note that additional 3<sup>rd</sup> party drivers can be loaded dynamically. <p> <h3 class=fn><a name="~QSqlDatabase"></a>QSqlDatabase::~QSqlDatabase () </h3> Destroys the object and frees any allocated resources. <p> <h3 class=fn><a href="qsqldatabase.html">QSqlDatabase</a> * <a name="addDatabase"></a>QSqlDatabase::addDatabase ( const <a href="qstring.html">QString</a> & type, const <a href="qstring.html">QString</a> & connectionName = defaultConnection )<tt> [static]</tt> </h3> Adds a database to the list of database connections using the driver <em>type</em> and the connection name <em>connectionName</em>. <p> The database connection is referred to by <em>connectionName</em>. A pointer to the newly added database connection is returned. This pointer is owned by QSqlDatabase and will be deleted on program exit or when <a href="#removeDatabase">removeDatabase</a>() is called. If <em>connectionName</em> is not specified, the newly added database connection becomes the default database connection for the application, and subsequent calls to <a href="#database">database</a>() (without a database name parameter) will return a pointer to it. <p> <p>See also <a href="#database">database</a>() and <a href="#removeDatabase">removeDatabase</a>(). <p> <p>Examples: <a href="sql.html#x2256">sql/overview/connect1/main.cpp</a>, <a href="sql.html#x2262">sql/overview/create_connections/main.cpp</a> and <a href="sqltable-example.html#x2637">sql/sqltable/main.cpp</a>. <h3 class=fn>void <a name="close"></a>QSqlDatabase::close () </h3> Closes the database connection, freeing any resources acquired. <p> <h3 class=fn>bool <a name="commit"></a>QSqlDatabase::commit () </h3> Commits a transaction to the database if the driver supports transactions. Returns TRUE if the operation succeeded, FALSE otherwise. <p> <p>See also <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>() and <a href="#rollback">rollback</a>(). <h3 class=fn>bool <a name="contains"></a>QSqlDatabase::contains ( const <a href="qstring.html">QString</a> & connectionName = defaultConnection )<tt> [static]</tt> </h3> Returns TRUE if the list of database connections contains <em>connectionName</em>, otherwise returns FALSE. <p> <h3 class=fn><a href="qsqldatabase.html">QSqlDatabase</a> * <a name="database"></a>QSqlDatabase::database ( const <a href="qstring.html">QString</a> & connectionName = defaultConnection, bool open = TRUE )<tt> [static]</tt> </h3> Returns a pointer to the database connection named <em>connectionName</em>. The database connection must have been previously added with <a href="#database">database</a>(). If <em>open</em> is TRUE (the default) and the database connection is not already open it is opened now. If no <em>connectionName</em> is specified the default connection is used. If <em>connectionName</em> does not exist in the list of databases, 0 is returned. The pointer returned is owned by QSqlDatabase and should <em>not</em> be deleted. <p> <p>Examples: <a href="sql.html#x2270">sql/overview/basicbrowsing/main.cpp</a> and <a href="sql.html#x2263">sql/overview/create_connections/main.cpp</a>. <h3 class=fn><a href="qstring.html">QString</a> <a name="databaseName"></a>QSqlDatabase::databaseName () const </h3><p>Returns the name of the database or the TNS Service Name for the QOCI8 (Oracle) driver. See the <a href="qsqldatabase.html#databaseName-prop">"databaseName"</a> property for details. <h3 class=fn><a href="qsqldriver.html">QSqlDriver</a> * <a name="driver"></a>QSqlDatabase::driver () const </h3> Returns a pointer to the database driver used to access the database connection. <p> <h3 class=fn><a href="qstring.html">QString</a> <a name="driverName"></a>QSqlDatabase::driverName () const </h3> Returns the name of the driver used by the database connection. <p> <h3 class=fn><a href="qstringlist.html">QStringList</a> <a name="drivers"></a>QSqlDatabase::drivers ()<tt> [static]</tt> </h3> Returns a list of all available database drivers. <p> Note that if you want to iterate over the list, you should iterate over a copy, e.g. <pre> <a href="qstringlist.html">QStringList</a> list = myDatabase.drivers(); QStringList::Iterator it = list.<a href="qvaluelist.html#begin">begin</a>(); while( it != list.<a href="qvaluelist.html#end">end</a>() ) { myProcessing( *it ); ++it; } </pre> <h3 class=fn><a href="qsqlquery.html">QSqlQuery</a> <a name="exec"></a>QSqlDatabase::exec ( const <a href="qstring.html">QString</a> & query = QString::null ) const </h3> Executes an SQL statement (e.g. an INSERT, UPDATE or DELETE statement) on the database, and returns a <a href="qsqlquery.html">QSqlQuery</a> object. Use <a href="#lastError">lastError</a>() to retrieve error information. If <em>query</em> is <a href="qstring.html#QString-null">QString::null</a>, an empty, invalid query is returned and lastError() is not affected. <p> <p>See also <a href="qsqlquery.html">QSqlQuery</a> and <a href="#lastError">lastError</a>(). <h3 class=fn><a href="qstring.html">QString</a> <a name="hostName"></a>QSqlDatabase::hostName () const </h3><p>Returns the host name where the database resides. See the <a href="qsqldatabase.html#hostName-prop">"hostName"</a> property for details. <h3 class=fn>bool <a name="isOpen"></a>QSqlDatabase::isOpen () const </h3> Returns TRUE if the database connection is currently open, otherwise returns FALSE. <p> <h3 class=fn>bool <a name="isOpenError"></a>QSqlDatabase::isOpenError () const </h3> Returns TRUE if there was an error opening the database connection, otherwise returns FALSE. Error information can be retrieved using the <a href="#lastError">lastError</a>() function. <p> <h3 class=fn><a href="qsqlerror.html">QSqlError</a> <a name="lastError"></a>QSqlDatabase::lastError () const </h3> Returns information about the last error that occurred on the database. See <a href="qsqlerror.html">QSqlError</a> for more information. <p> <p>Examples: <a href="sql.html#x2264">sql/overview/create_connections/main.cpp</a> and <a href="sqltable-example.html#x2638">sql/sqltable/main.cpp</a>. <h3 class=fn>bool <a name="open"></a>QSqlDatabase::open () </h3> Opens the database connection using the current connection values. Returns TRUE on success, and FALSE if there was an error. Error information can be retrieved using the <a href="#lastError">lastError</a>() function. <p> <p>See also <a href="#lastError">lastError</a>(). <p>Examples: <a href="sql.html#x2257">sql/overview/connect1/main.cpp</a>, <a href="sql.html#x2265">sql/overview/create_connections/main.cpp</a> and <a href="sqltable-example.html#x2639">sql/sqltable/main.cpp</a>. <h3 class=fn>bool <a name="open-2"></a>QSqlDatabase::open ( const <a href="qstring.html">QString</a> & user, const <a href="qstring.html">QString</a> & password ) </h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function. <p> Opens the database connection using <em>user</em> name and <em>password</em>. Returns TRUE on success, and FALSE if there was an error. Error information can be retrieved using the <a href="#lastError">lastError</a>() function. <p> <p>See also <a href="#lastError">lastError</a>(). <h3 class=fn><a href="qstring.html">QString</a> <a name="password"></a>QSqlDatabase::password () const </h3><p>Returns the password used to connect to the database. See the <a href="qsqldatabase.html#password-prop">"password"</a> property for details. <h3 class=fn>int <a name="port"></a>QSqlDatabase::port () const </h3><p>Returns the port used to connect to the database. See the <a href="qsqldatabase.html#port-prop">"port"</a> property for details. <h3 class=fn><a href="qsqlindex.html">QSqlIndex</a> <a name="primaryIndex"></a>QSqlDatabase::primaryIndex ( const <a href="qstring.html">QString</a> & tablename ) const </h3> Returns the primary index for table <em>tablename</em>. If no primary index exists an empty <a href="qsqlindex.html">QSqlIndex</a> will be returned. <p> <h3 class=fn><a href="qsqlrecord.html">QSqlRecord</a> <a name="record"></a>QSqlDatabase::record ( const <a href="qstring.html">QString</a> & tablename ) const </h3> Returns a <a href="qsqlrecord.html">QSqlRecord</a> populated with the names of all the fields in the table (or view) named <em>tablename</em>. The order in which the fields are returned is undefined. If no such table (or view) exists, an empty record is returned. <p> <p>See also <a href="#recordInfo">recordInfo</a>(). <h3 class=fn><a href="qsqlrecord.html">QSqlRecord</a> <a name="record-2"></a>QSqlDatabase::record ( const <a href="qsqlquery.html">QSqlQuery</a> & query ) const </h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function. <p> Returns a <a href="qsqlrecord.html">QSqlRecord</a> populated with the names of all the fields used in the SQL <em>query</em>. If the query is a "SELECT *" the order in which fields are returned is undefined. <p> <p>See also <a href="#recordInfo">recordInfo</a>(). <h3 class=fn><a href="qsqlrecordinfo.html">QSqlRecordInfo</a> <a name="recordInfo"></a>QSqlDatabase::recordInfo ( const <a href="qstring.html">QString</a> & tablename ) const </h3> Returns a <a href="qsqlrecordinfo.html">QSqlRecordInfo</a> populated with meta-data about the table (or view) <em>tablename</em>. If no such table (or view) exists, an empty record is returned. <p> <p>See also <a href="qsqlrecordinfo.html">QSqlRecordInfo</a>, <a href="qsqlfieldinfo.html">QSqlFieldInfo</a> and <a href="#record">record</a>(). <h3 class=fn><a href="qsqlrecordinfo.html">QSqlRecordInfo</a> <a name="recordInfo-2"></a>QSqlDatabase::recordInfo ( const <a href="qsqlquery.html">QSqlQuery</a> & query ) const </h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function. <p> Returns a <a href="qsqlrecordinfo.html">QSqlRecordInfo</a> object with meta data for the <a href="qsqlquery.html">QSqlQuery</a> <em>query</em>. Note that this overloaded function may return not as much information as the recordInfo function which takes the name of a table as parameter. <p> <p>See also <a href="qsqlrecordinfo.html">QSqlRecordInfo</a>, <a href="qsqlfieldinfo.html">QSqlFieldInfo</a> and <a href="#record">record</a>(). <h3 class=fn>void <a name="removeDatabase"></a>QSqlDatabase::removeDatabase ( const <a href="qstring.html">QString</a> & connectionName )<tt> [static]</tt> </h3> Removes the database connection <em>connectionName</em> from the list of database connections. Note that there should be no open queries on the database connection when this function is called, otherwise a resource leak will occur. <p> <h3 class=fn>bool <a name="rollback"></a>QSqlDatabase::rollback () </h3> Rolls a transaction back on the database if the driver supports transactions. Returns TRUE if the operation succeeded, FALSE otherwise. <p> <p>See also <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>(), <a href="#commit">commit</a>() and <a href="#transaction">transaction</a>(). <h3 class=fn>void <a name="setDatabaseName"></a>QSqlDatabase::setDatabaseName ( const <a href="qstring.html">QString</a> & name )<tt> [virtual]</tt> </h3><p>Sets the name of the database or the TNS Service Name for the QOCI8 (Oracle) driver to <em>name</em>. See the <a href="qsqldatabase.html#databaseName-prop">"databaseName"</a> property for details. <h3 class=fn>void <a name="setHostName"></a>QSqlDatabase::setHostName ( const <a href="qstring.html">QString</a> & host )<tt> [virtual]</tt> </h3><p>Sets the host name where the database resides to <em>host</em>. See the <a href="qsqldatabase.html#hostName-prop">"hostName"</a> property for details. <h3 class=fn>void <a name="setPassword"></a>QSqlDatabase::setPassword ( const <a href="qstring.html">QString</a> & password )<tt> [virtual]</tt> </h3><p>Sets the password used to connect to the database to <em>password</em>. See the <a href="qsqldatabase.html#password-prop">"password"</a> property for details. <h3 class=fn>void <a name="setPort"></a>QSqlDatabase::setPort ( int p )<tt> [virtual]</tt> </h3><p>Sets the port used to connect to the database to <em>p</em>. See the <a href="qsqldatabase.html#port-prop">"port"</a> property for details. <h3 class=fn>void <a name="setUserName"></a>QSqlDatabase::setUserName ( const <a href="qstring.html">QString</a> & name )<tt> [virtual]</tt> </h3><p>Sets the user name connected to the database to <em>name</em>. See the <a href="qsqldatabase.html#userName-prop">"userName"</a> property for details. <h3 class=fn><a href="qstringlist.html">QStringList</a> <a name="tables"></a>QSqlDatabase::tables () const </h3> Returns a list of tables in the database. <p> Note that if you want to iterate over the list, you should iterate over a copy, e.g. <pre> <a href="qstringlist.html">QStringList</a> list = myDatabase.tables(); QStringList::Iterator it = list.<a href="qvaluelist.html#begin">begin</a>(); while( it != list.<a href="qvaluelist.html#end">end</a>() ) { myProcessing( *it ); ++it; } </pre> <h3 class=fn>bool <a name="transaction"></a>QSqlDatabase::transaction () </h3> Begins a transaction on the database if the driver supports transactions. Returns TRUE if the operation succeeded, FALSE otherwise. <p> <p>See also <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>(), <a href="#commit">commit</a>() and <a href="#rollback">rollback</a>(). <h3 class=fn><a href="qstring.html">QString</a> <a name="userName"></a>QSqlDatabase::userName () const </h3><p>Returns the user name connected to the database. See the <a href="qsqldatabase.html#userName-prop">"userName"</a> property for details. <hr><h2>Property Documentation</h2> <h3 class=fn><a href="qstring.html">QString</a> <a name="databaseName-prop"></a>databaseName</h3> <p>This property holds the name of the database or the TNS Service Name for the QOCI8 (Oracle) driver. <p>There is no default value. <p>Set this property's value with <a href="#setDatabaseName">setDatabaseName</a>() and get this property's value with <a href="#databaseName">databaseName</a>(). <h3 class=fn><a href="qstring.html">QString</a> <a name="hostName-prop"></a>hostName</h3> <p>This property holds the host name where the database resides. <p>There is no default value. <p> <p>Set this property's value with <a href="#setHostName">setHostName</a>() and get this property's value with <a href="#hostName">hostName</a>(). <h3 class=fn><a href="qstring.html">QString</a> <a name="password-prop"></a>password</h3> <p>This property holds the password used to connect to the database. <p>There is no default value. <p> <p>Set this property's value with <a href="#setPassword">setPassword</a>() and get this property's value with <a href="#password">password</a>(). <h3 class=fn>int <a name="port-prop"></a>port</h3> <p>This property holds the port used to connect to the database. <p>There is no default value. <p> <p>Set this property's value with <a href="#setPort">setPort</a>() and get this property's value with <a href="#port">port</a>(). <h3 class=fn><a href="qstring.html">QString</a> <a name="userName-prop"></a>userName</h3> <p>This property holds the user name connected to the database. <p>There is no default value. <p> <p>Set this property's value with <a href="#setUserName">setUserName</a>() and get this property's value with <a href="#userName">userName</a>(). <!-- eof --> <hr><p> This file is part of the <a href="index.html">Qt toolkit</a>. Copyright © 1995-2001 <a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center> <table width=100% cellspacing=0 border=0><tr> <td>Copyright © 2001 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a> <td align=right><div align=right>Qt version 3.0.2</div> </table></div></address></body> </html>