<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/src/sql/qsqlquery.cpp:77 --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>QSqlQuery 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>QSqlQuery Class Reference<br><small>[<a href="sql.html">sql module</a>]</small></h1> <p>The QSqlQuery class provides a means of executing and manipulating SQL statements. <a href="#details">More...</a> <p><tt>#include <<a href="qsqlquery-h.html">qsqlquery.h</a>></tt> <p>Inherited by <a href="qsqlcursor.html">QSqlCursor</a>. <p><a href="qsqlquery-members.html">List of all member functions.</a> <h2>Public Members</h2> <ul> <li><div class=fn><a href="#QSqlQuery"><b>QSqlQuery</b></a> ( QSqlResult * r )</div></li> <li><div class=fn><a href="#QSqlQuery-2"><b>QSqlQuery</b></a> ( const QString & query = QString::null, QSqlDatabase * db = 0 )</div></li> <li><div class=fn><a href="#QSqlQuery-3"><b>QSqlQuery</b></a> ( const QSqlQuery & other )</div></li> <li><div class=fn>QSqlQuery & <a href="#operator-eq"><b>operator=</b></a> ( const QSqlQuery & other )</div></li> <li><div class=fn>virtual <a href="#~QSqlQuery"><b>~QSqlQuery</b></a> ()</div></li> <li><div class=fn>bool <a href="#isValid"><b>isValid</b></a> () const</div></li> <li><div class=fn>bool <a href="#isActive"><b>isActive</b></a> () const</div></li> <li><div class=fn>bool <a href="#isNull"><b>isNull</b></a> ( int field ) const</div></li> <li><div class=fn>int <a href="#at"><b>at</b></a> () const</div></li> <li><div class=fn>QString <a href="#lastQuery"><b>lastQuery</b></a> () const</div></li> <li><div class=fn>int <a href="#numRowsAffected"><b>numRowsAffected</b></a> () 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="#isSelect"><b>isSelect</b></a> () const</div></li> <li><div class=fn>int <a href="#size"><b>size</b></a> () const</div></li> <li><div class=fn>const QSqlDriver * <a href="#driver"><b>driver</b></a> () const</div></li> <li><div class=fn>const QSqlResult * <a href="#result"><b>result</b></a> () const</div></li> <li><div class=fn>virtual bool <a href="#exec"><b>exec</b></a> ( const QString & query )</div></li> <li><div class=fn>virtual QVariant <a href="#value"><b>value</b></a> ( int i ) const</div></li> <li><div class=fn>virtual bool <a href="#seek"><b>seek</b></a> ( int i, bool relative = FALSE )</div></li> <li><div class=fn>virtual bool <a href="#next"><b>next</b></a> ()</div></li> <li><div class=fn>virtual bool <a href="#prev"><b>prev</b></a> ()</div></li> <li><div class=fn>virtual bool <a href="#first"><b>first</b></a> ()</div></li> <li><div class=fn>virtual bool <a href="#last"><b>last</b></a> ()</div></li> </ul> <h2>Protected Members</h2> <ul> <li><div class=fn>virtual void <a href="#beforeSeek"><b>beforeSeek</b></a> ()</div></li> <li><div class=fn>virtual void <a href="#afterSeek"><b>afterSeek</b></a> ()</div></li> </ul> <hr><a name="details"></a><h2>Detailed Description</h2> <p> The QSqlQuery class provides a means of executing and manipulating SQL statements. <p> <p> QSqlQuery encapsulates the functionality involved in creating, navigating and retrieving data from SQL queries which are executed on a <a href="qsqldatabase.html">QSqlDatabase</a>. It can be used to execute DML (data manipulation language) statements, e.g. <tt>SELECT</tt>, <tt>INSERT</tt>, <tt>UPDATE</tt> and <tt>DELETE</tt>, and also DDL (data definition language) statements, e.g. <tt>CREATE TABLE</tt>. It can also be used to execute database-specific commands which are not standard SQL (e.g. <tt>SET DATESTYLE=ISO</tt> for PostgreSQL). <p> Successfully executed SQL statements set the query to an active state (<a href="#isActive">isActive</a>() returns TRUE) otherwise the query is set to an inactive state. In either case, when executing a new SQL statement, the query is positioned on an invalid record; an active query must be navigated to a valid record (so that <a href="#isValid">isValid</a>() returns TRUE) before values can be retrieved. <p> Navigating records is performed with the following functions: <p> <ul> <li><a href="#next">next</a>() <li><a href="#prev">prev</a>() <li><a href="#first">first</a>() <li><a href="#last">last</a>() <li><tt></tt><a href="#seek">seek</a>(int) </ul> <p> These functions allow the programmer to move forward, backward or arbitrarily through the records returned by the query. Once an active query is positioned on a valid record, data can be retrieved using <a href="#value">value</a>(). All data is transferred from the SQL backend using QVariants. <p> For example: <p> <pre> QSqlQuery query( "select name from customer" ); while ( query.<a href="#next">next</a>() ) { <a href="qstring.html">QString</a> name = query.<a href="#value">value</a>(0).toString(); doSomething( name ); } </pre> <p> To access the data returned by a query, use the value() method. Each field in the data returned by a SELECT statement is accessed by passing the index number of the desired field, starting with 0. There are no methods to access a field by name to make sure the usage of QSqlQuery is as optimal as possible (see <a href="qsqlcursor.html">QSqlCursor</a> for a more flexible interface for selecting data from a table or view in the database). <p> <p>See also <a href="qsqldatabase.html">QSqlDatabase</a>, <a href="qsqlcursor.html">QSqlCursor</a>, <a href="qvariant.html">QVariant</a> and <a href="database.html">Database Classes</a>. <hr><h2>Member Function Documentation</h2> <h3 class=fn><a name="QSqlQuery"></a>QSqlQuery::QSqlQuery ( <a href="qsqlresult.html">QSqlResult</a> * r ) </h3> Creates a QSqlQuery object which uses the <a href="qsqlresult.html">QSqlResult</a> <em>r</em> to communicate with a database. <h3 class=fn><a name="QSqlQuery-2"></a>QSqlQuery::QSqlQuery ( const <a href="qstring.html">QString</a> & query = QString::null, <a href="qsqldatabase.html">QSqlDatabase</a> * db = 0 ) </h3> Creates a QSqlQuery object using the SQL <em>query</em> and the database <em>db</em>. If <em>db</em> is 0, (the default), the application's default database is used. <p> <p>See also <a href="qsqldatabase.html">QSqlDatabase</a>. <p> <h3 class=fn><a name="QSqlQuery-3"></a>QSqlQuery::QSqlQuery ( const <a href="qsqlquery.html">QSqlQuery</a> & other ) </h3> Constructs a copy of <em>other</em>. <h3 class=fn><a name="~QSqlQuery"></a>QSqlQuery::~QSqlQuery ()<tt> [virtual]</tt> </h3> Destroys the object and frees any allocated resources. <p> <h3 class=fn>void <a name="afterSeek"></a>QSqlQuery::afterSeek ()<tt> [virtual protected]</tt> </h3> Protected virtual function called after the internal record pointer is moved to a new record. The default implementation does nothing. <h3 class=fn>int <a name="at"></a>QSqlQuery::at () const </h3> Returns the current internal position of the query. The first record is at position zero. If the position is invalid, a QSql::Location will be returned indicating the invalid position. <p> <p>See also <a href="#isValid">isValid</a>(). <p> <p>Example: <a href="sql.html#x2283">sql/overview/navigating/main.cpp</a>. <h3 class=fn>void <a name="beforeSeek"></a>QSqlQuery::beforeSeek ()<tt> [virtual protected]</tt> </h3> Protected virtual function called before the internal record pointer is moved to a new record. The default implementation does nothing. <p> <h3 class=fn>const <a href="qsqldriver.html">QSqlDriver</a> * <a name="driver"></a>QSqlQuery::driver () const </h3> Returns a pointer to the database driver associated with the query. <p> <h3 class=fn>bool <a name="exec"></a>QSqlQuery::exec ( const <a href="qstring.html">QString</a> & query )<tt> [virtual]</tt> </h3> Executes the SQL <em>query</em>. Returns TRUE if the query was successful sets the query state to active, otherwise returns FALSE and the query becomes inactive. The <em>query</em> string must use syntax appropriate for the SQL database being queried, for example, standard SQL. <p> After the query is executed, the query is positioned on an invalid record, and must be navigated to a valid record before data values can be retrieved. <p> <p>See also <a href="#isActive">isActive</a>(), <a href="#isValid">isValid</a>(), <a href="#next">next</a>(), <a href="#prev">prev</a>(), <a href="#first">first</a>(), <a href="#last">last</a>() and <a href="#seek">seek</a>(). <p> <p>Examples: <a href="sql.html#x2271">sql/overview/basicbrowsing/main.cpp</a>, <a href="sql.html#x2275">sql/overview/basicbrowsing2/main.cpp</a> and <a href="sql.html#x2280">sql/overview/basicdatamanip/main.cpp</a>. <h3 class=fn>bool <a name="first"></a>QSqlQuery::first ()<tt> [virtual]</tt> </h3> Retrieves the first record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before calling this function or it will do nothing and return FALSE. Returns TRUE if successful. If unsuccessful the query position is set to an invalid position and FALSE is returned. <p> <p>Example: <a href="sql.html#x2284">sql/overview/navigating/main.cpp</a>. <h3 class=fn>bool <a name="isActive"></a>QSqlQuery::isActive () const </h3> Returns TRUE if the query is currently active, otherwise returns FALSE. <p>Examples: <a href="sql.html#x2272">sql/overview/basicbrowsing/main.cpp</a>, <a href="sql.html#x2276">sql/overview/basicbrowsing2/main.cpp</a>, <a href="sql.html#x2281">sql/overview/basicdatamanip/main.cpp</a>, <a href="sql.html#x2285">sql/overview/navigating/main.cpp</a> and <a href="sql.html#x2289">sql/overview/retrieve1/main.cpp</a>. <h3 class=fn>bool <a name="isNull"></a>QSqlQuery::isNull ( int field ) const </h3> Returns TRUE if <em>field</em> is currently NULL, otherwise returns FALSE. The query must be active and positioned on a valid record before calling this function otherwise it returns FALSE. Note that, for some drivers, <a href="#isNull">isNull</a>() will not return accurate information until after an attempt is made to retrieve data. <p> <p>See also <a href="#isActive">isActive</a>(), <a href="#isValid">isValid</a>() and <a href="#value">value</a>(). <p> <h3 class=fn>bool <a name="isSelect"></a>QSqlQuery::isSelect () const </h3> Returns TRUE if the current query is a <tt>SELECT</tt> statement, otherwise returns FALSE. <p> <h3 class=fn>bool <a name="isValid"></a>QSqlQuery::isValid () const </h3> Returns TRUE if the query is currently positioned on a valid record, otherwise returns FALSE. <h3 class=fn>bool <a name="last"></a>QSqlQuery::last ()<tt> [virtual]</tt> </h3> Retrieves the last record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before calling this function or it will do nothing and return FALSE. Returns TRUE if successful. If unsuccessful the query position is set to an invalid position and FALSE is returned. <p> <p>Example: <a href="sql.html#x2286">sql/overview/navigating/main.cpp</a>. <h3 class=fn><a href="qsqlerror.html">QSqlError</a> <a name="lastError"></a>QSqlQuery::lastError () const </h3> Returns error information about the last error (if any) that occurred. <p> <p>See also <a href="qsqlerror.html">QSqlError</a>. <p> <h3 class=fn><a href="qstring.html">QString</a> <a name="lastQuery"></a>QSqlQuery::lastQuery () const </h3> Returns the text of the current query being used, or <a href="qstring.html#QString-null">QString::null</a> if there is no current query text. <p> <h3 class=fn>bool <a name="next"></a>QSqlQuery::next ()<tt> [virtual]</tt> </h3> Retrieves the next record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before calling this function or it will do nothing and return FALSE. <p> The following rules apply: <p> <ul> <li>If the result is currently located before the first record, e.g. immediately after a query is executed, an attempt is made to retrieve the first record. <li>If the result is currently located after the last record, there is no change and FALSE is returned. <li> If the result is located somewhere in the middle, an attempt is made to retrieve the next record. </ul> <p> If the record could not be retrieved, the result is positioned after the last record and FALSE is returned. If the record is successfully retrieved, TRUE is returned. <p> <p>See also <a href="#at">at</a>() and <a href="#isValid">isValid</a>(). <p> <p>Examples: <a href="sql.html#x2273">sql/overview/basicbrowsing/main.cpp</a>, <a href="sql.html#x2277">sql/overview/basicbrowsing2/main.cpp</a>, <a href="sql.html#x2290">sql/overview/retrieve1/main.cpp</a>, <a href="sql.html#x2375">sql/overview/subclass3/main.cpp</a>, <a href="sql.html#x2379">sql/overview/subclass4/main.cpp</a>, <a href="sql.html#x2382">sql/overview/subclass5/main.cpp</a> and <a href="sqltable-example.html#x2644">sql/sqltable/main.cpp</a>. <h3 class=fn>int <a name="numRowsAffected"></a>QSqlQuery::numRowsAffected () const </h3> Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. Note that for SELECT statements, this value will be the same as <a href="#size">size</a>(). If the query is not active (<a href="#isActive">isActive</a>() returns FALSE), -1 is returned. <p> <p>See also <a href="#size">size</a>() and <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>(). <p> <p>Examples: <a href="sql.html#x2278">sql/overview/basicbrowsing2/main.cpp</a> and <a href="sql.html#x2282">sql/overview/basicdatamanip/main.cpp</a>. <h3 class=fn><a href="qsqlquery.html">QSqlQuery</a> & <a name="operator-eq"></a>QSqlQuery::operator= ( const <a href="qsqlquery.html">QSqlQuery</a> & other ) </h3> Assigns <em>other</em> to the query. <h3 class=fn>bool <a name="prev"></a>QSqlQuery::prev ()<tt> [virtual]</tt> </h3> Retrieves the previous record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before calling this function or it will do nothing and return FALSE. <p> The following rules apply: <p> <ul> <li>If the result is currently located before the first record, there is no change and FALSE is returned. <li>If the result is currently located after the last record, an attempt is made to retrieve the last record. <li>If the result is somewhere in the middle, an attempt is made to retrieve the previous record. </ul> <p> If the record could not be retrieved, the result is positioned before the first record and FALSE is returned. If the record is successfully retrieved, TRUE is returned. <p> <p>See also <a href="#at">at</a>(). <p> <h3 class=fn>const <a href="qsqlresult.html">QSqlResult</a> * <a name="result"></a>QSqlQuery::result () const </h3> Returns a pointer to the result associated with the query. <p> <h3 class=fn>bool <a name="seek"></a>QSqlQuery::seek ( int i, bool relative = FALSE )<tt> [virtual]</tt> </h3> Retrieves the record at position (or offset) <em>i</em>, if available, and positions the query on the retrieved record. The first record is at position zero. Note that the query must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before calling this function. <p> The following rules apply: <p> If <em>relative</em> is FALSE (the default), the following rules apply: <p> <ul> <li> If <em>i</em> is negative, the result is positioned before the first record and FALSE is returned. <li> Otherwise, an attempt is made to move to the record at position <em>i</em>. If the record at position <em>i</em> could not be retrieved, the result is positioned after the last record and FALSE is returned. If the record is successfully retrieved, TRUE is returned. </ul> <p> If <em>relative</em> is TRUE, the following rules apply: <p> <ul> <li> If the result is currently positioned before the first record or on the first record, and <em>i</em> is negative, there is no change, and FALSE is returned. <li> If the result is currently located after the last record, and <em>i</em> is positive, there is no change, and FALSE is returned. <li>If the result is currently located somewhere in the middle, and the relative offset <em>i</em> moves the result below zero, the result is positioned before the first record and FALSE is returned. <li> Otherwise, an attempt is made to move to the record <em>i</em> records ahead of the current record (or <em>i</em> records behind the current record if <em>i</em> is negative). If the record at offset <em>i</em> could not be retrieved, the result is positioned after the last record if <em>i</em> >= 0, (or before the first record if <em>i</em> is negative), and FALSE is returned. If the record is successfully retrieved, TRUE is returned. </ul> <p> <p>Example: <a href="sql.html#x2287">sql/overview/navigating/main.cpp</a>. <h3 class=fn>int <a name="size"></a>QSqlQuery::size () const </h3> Returns the size of the result, (number of rows returned), or -1 if the size cannot be determined or the database does not support reporting information about query sizes. Note that for non-SELECT statements (<a href="#isSelect">isSelect</a>() returns FALSE), <a href="#size">size</a>() will return -1. If the query is not active (<a href="#isActive">isActive</a>() returns FALSE), -1 is returned. <p> To determine the number of rows affected by a non-SELECT statement, use <a href="#numRowsAffected">numRowsAffected</a>(). <p> <p>See also <a href="#isActive">isActive</a>(), <a href="#numRowsAffected">numRowsAffected</a>() and <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>(). <p> <p>Example: <a href="sql.html#x2288">sql/overview/navigating/main.cpp</a>. <h3 class=fn><a href="qvariant.html">QVariant</a> <a name="value"></a>QSqlQuery::value ( int i ) const<tt> [virtual]</tt> </h3> Returns the value of field <em>i</em> (zero based). <p> The fields are numbered from left to right using the text of the <tt>SELECT</tt> statement, e.g. in "select forename, surname from people", field 0 is forename and field 1 is surname. Using <tt>SELECT *</tt> is not recommended because the order of the fields in the query is undefined. <p> An invalid <a href="qvariant.html">QVariant</a> is returned if field <em>i</em> does not exist, if the query is inactive, or if the query is positioned on an invalid record. <p> <p>See also <a href="#prev">prev</a>(), <a href="#next">next</a>(), <a href="#first">first</a>(), <a href="#last">last</a>(), <a href="#seek">seek</a>(), <a href="#isActive">isActive</a>() and <a href="#isValid">isValid</a>(). <p> <p>Examples: <a href="sql.html#x2274">sql/overview/basicbrowsing/main.cpp</a>, <a href="sql.html#x2279">sql/overview/basicbrowsing2/main.cpp</a>, <a href="sql.html#x2291">sql/overview/retrieve1/main.cpp</a>, <a href="sql.html#x2376">sql/overview/subclass3/main.cpp</a>, <a href="sql.html#x2380">sql/overview/subclass4/main.cpp</a>, <a href="sql.html#x2383">sql/overview/subclass5/main.cpp</a> and <a href="sqltable-example.html#x2645">sql/sqltable/main.cpp</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>