~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qsqlquery.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/src/sql/kernel/qsqlquery.cpp -->
 
6
<head>
 
7
    <title>Qt 4.0: QSqlQuery Class Reference</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QSqlQuery Class Reference</h1>
 
21
<p>The QSqlQuery class provides a means of executing and manipulating SQL statements. <a href="#details">More...</a></p>
 
22
<pre>#include &lt;QSqlQuery&gt;</pre><p>Part of the <a href="qtsql.html">QtSql</a> module.</p>
 
23
<p>Inherited by <a href="q3sqlcursor.html">Q3SqlCursor</a>.</p>
 
24
<ul>
 
25
<li><a href="qsqlquery-members.html">List of all members, including inherited members</a></li>
 
26
<li><a href="qsqlquery-qt3.html">Qt 3 support members</a></li>
 
27
</ul>
 
28
<a name="public-functions"></a>
 
29
<h3>Public Functions</h3>
 
30
<ul>
 
31
<li><div class="fn"/><b><a href="qsqlquery.html#QSqlQuery">QSqlQuery</a></b> ( QSqlResult * <i>result</i> )</li>
 
32
<li><div class="fn"/><b><a href="qsqlquery.html#QSqlQuery-2">QSqlQuery</a></b> ( const QString &amp; <i>query</i> = QString(), QSqlDatabase <i>db</i> = QSqlDatabase() )</li>
 
33
<li><div class="fn"/><b><a href="qsqlquery.html#QSqlQuery-3">QSqlQuery</a></b> ( QSqlDatabase <i>db</i> )</li>
 
34
<li><div class="fn"/><b><a href="qsqlquery.html#QSqlQuery-4">QSqlQuery</a></b> ( const QSqlQuery &amp; <i>other</i> )</li>
 
35
<li><div class="fn"/><b><a href="qsqlquery.html#dtor.QSqlQuery">~QSqlQuery</a></b> ()</li>
 
36
<li><div class="fn"/>void <b><a href="qsqlquery.html#addBindValue">addBindValue</a></b> ( const QVariant &amp; <i>val</i>, QSql::ParamType <i>paramType</i> = QSql::In )</li>
 
37
<li><div class="fn"/>int <b><a href="qsqlquery.html#at">at</a></b> () const</li>
 
38
<li><div class="fn"/>void <b><a href="qsqlquery.html#bindValue">bindValue</a></b> ( const QString &amp; <i>placeholder</i>, const QVariant &amp; <i>val</i>, QSql::ParamType <i>paramType</i> = QSql::In )</li>
 
39
<li><div class="fn"/>void <b><a href="qsqlquery.html#bindValue-2">bindValue</a></b> ( int <i>pos</i>, const QVariant &amp; <i>val</i>, QSql::ParamType <i>paramType</i> = QSql::In )</li>
 
40
<li><div class="fn"/>QVariant <b><a href="qsqlquery.html#boundValue">boundValue</a></b> ( const QString &amp; <i>placeholder</i> ) const</li>
 
41
<li><div class="fn"/>QVariant <b><a href="qsqlquery.html#boundValue-2">boundValue</a></b> ( int <i>pos</i> ) const</li>
 
42
<li><div class="fn"/>QMap&lt;QString, QVariant&gt; <b><a href="qsqlquery.html#boundValues">boundValues</a></b> () const</li>
 
43
<li><div class="fn"/>void <b><a href="qsqlquery.html#clear">clear</a></b> ()</li>
 
44
<li><div class="fn"/>const QSqlDriver * <b><a href="qsqlquery.html#driver">driver</a></b> () const</li>
 
45
<li><div class="fn"/>bool <b><a href="qsqlquery.html#exec">exec</a></b> ( const QString &amp; <i>query</i> )</li>
 
46
<li><div class="fn"/>bool <b><a href="qsqlquery.html#exec-2">exec</a></b> ()</li>
 
47
<li><div class="fn"/>QString <b><a href="qsqlquery.html#executedQuery">executedQuery</a></b> () const</li>
 
48
<li><div class="fn"/>bool <b><a href="qsqlquery.html#first">first</a></b> ()</li>
 
49
<li><div class="fn"/>bool <b><a href="qsqlquery.html#isActive">isActive</a></b> () const</li>
 
50
<li><div class="fn"/>bool <b><a href="qsqlquery.html#isForwardOnly">isForwardOnly</a></b> () const</li>
 
51
<li><div class="fn"/>bool <b><a href="qsqlquery.html#isNull">isNull</a></b> ( int <i>field</i> ) const</li>
 
52
<li><div class="fn"/>bool <b><a href="qsqlquery.html#isSelect">isSelect</a></b> () const</li>
 
53
<li><div class="fn"/>bool <b><a href="qsqlquery.html#isValid">isValid</a></b> () const</li>
 
54
<li><div class="fn"/>bool <b><a href="qsqlquery.html#last">last</a></b> ()</li>
 
55
<li><div class="fn"/>QSqlError <b><a href="qsqlquery.html#lastError">lastError</a></b> () const</li>
 
56
<li><div class="fn"/>QVariant <b><a href="qsqlquery.html#lastInsertId">lastInsertId</a></b> () const</li>
 
57
<li><div class="fn"/>QString <b><a href="qsqlquery.html#lastQuery">lastQuery</a></b> () const</li>
 
58
<li><div class="fn"/>bool <b><a href="qsqlquery.html#next">next</a></b> ()</li>
 
59
<li><div class="fn"/>int <b><a href="qsqlquery.html#numRowsAffected">numRowsAffected</a></b> () const</li>
 
60
<li><div class="fn"/>bool <b><a href="qsqlquery.html#prepare">prepare</a></b> ( const QString &amp; <i>query</i> )</li>
 
61
<li><div class="fn"/>bool <b><a href="qsqlquery.html#previous">previous</a></b> ()</li>
 
62
<li><div class="fn"/>QSqlRecord <b><a href="qsqlquery.html#record">record</a></b> () const</li>
 
63
<li><div class="fn"/>const QSqlResult * <b><a href="qsqlquery.html#result">result</a></b> () const</li>
 
64
<li><div class="fn"/>bool <b><a href="qsqlquery.html#seek">seek</a></b> ( int <i>index</i>, bool <i>relative</i> = false )</li>
 
65
<li><div class="fn"/>void <b><a href="qsqlquery.html#setForwardOnly">setForwardOnly</a></b> ( bool <i>forward</i> )</li>
 
66
<li><div class="fn"/>int <b><a href="qsqlquery.html#size">size</a></b> () const</li>
 
67
<li><div class="fn"/>QVariant <b><a href="qsqlquery.html#value">value</a></b> ( int <i>index</i> ) const</li>
 
68
<li><div class="fn"/>QSqlQuery &amp; <b><a href="qsqlquery.html#operator-eq">operator=</a></b> ( const QSqlQuery &amp; <i>other</i> )</li>
 
69
</ul>
 
70
<a name="details"></a>
 
71
<hr />
 
72
<h2>Detailed Description</h2>
 
73
<p>The QSqlQuery class provides a means of executing and manipulating SQL statements.</p>
 
74
<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, such as <tt>SELECT</tt>, <tt>INSERT</tt>, <tt>UPDATE</tt> and <tt>DELETE</tt>, as well as DDL (data definition language) statements, such as <tt>CREATE</tt> <tt>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>
 
75
<p>Successfully executed SQL statements set the query's state to active; <a href="qsqlquery.html#isActive">isActive</a>() then returns true. Otherwise the query's state is set to inactive. 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="qsqlquery.html#isValid">isValid</a>() returns true) before values can be retrieved.</p>
 
76
<a name="qsqlquery-examples"></a><p>Navigating records is performed with the following functions:</p>
 
77
<ul>
 
78
<li><a href="qsqlquery.html#next">next</a>()</li>
 
79
<li><a href="qsqlquery.html#previous">previous</a>()</li>
 
80
<li><a href="qsqlquery.html#first">first</a>()</li>
 
81
<li><a href="qsqlquery.html#last">last</a>()</li>
 
82
<li><a href="qsqlquery.html#seek">seek</a>()</li>
 
83
</ul>
 
84
<p>These functions allow the programmer to move forward, backward or arbitrarily through the records returned by the query. If you only need to move forward through the results, e.g. using <a href="qsqlquery.html#next">next</a>() or using <a href="qsqlquery.html#seek">seek</a>() with a positive offset, you can use <a href="qsqlquery.html#setForwardOnly">setForwardOnly</a>() and save a significant amount of memory overhead. Once an active query is positioned on a valid record, data can be retrieved using <a href="qsqlquery.html#value">value</a>(). All data is transferred from the SQL backend using QVariants.</p>
 
85
<p>For example:</p>
 
86
<pre>&nbsp;       QSqlQuery query(&quot;SELECT country FROM artist&quot;);
 
87
        while (query.next()) {
 
88
            QString country = query.value(0).toString();
 
89
            doSomething(country);
 
90
        }</pre>
 
91
<p>To access the data returned by a query, use value(int). Each field in the data returned by a <tt>SELECT</tt> statement is accessed by passing the field's position in the statement, starting from 0. This makes using <tt>SELECT *</tt> queries inadvisable because the order of the fields returned is indeterminate.</p>
 
92
<p>For the sake of efficiency, there are no functions to access a field by name (unless you use prepared queries with names, as explained below). To convert a field name into an index, use <a href="qsqlquery.html#record">record</a>().<a href="qsqlrecord.html#indexOf">indexOf()</a>, for example:</p>
 
93
<pre>&nbsp;       QSqlQuery query(&quot;SELECT * FROM artist&quot;);
 
94
        int fieldNo = query.record().indexOf(&quot;country&quot;);
 
95
        while (query.next()) {
 
96
            QString country = query.value(fieldNo).toString();
 
97
            doSomething(country);
 
98
        }</pre>
 
99
<p>QSqlQuery supports prepared query execution and the binding of parameter values to placeholders. Some databases don't support these features, so for those, Qt emulates the required functionality. For example, the Oracle and ODBC drivers have proper prepared query support, and Qt makes use of it; but for databases that don't have this support, Qt implements the feature itself, e.g. by replacing placeholders with actual values when a query is executed. Use <a href="qsqlquery.html#numRowsAffected">numRowsAffected</a>() to find out how many rows were affected by a non-<tt>SELECT</tt> query, and <a href="qsqlquery.html#size">size</a>() to find how many were retrieved by a <tt>SELECT</tt>.</p>
 
100
<p>Oracle databases identify placeholders by using a colon-name syntax, e.g <tt>:name</tt>. ODBC simply uses <tt>?</tt> characters. Qt supports both syntaxes, with the restriction that you can't mix them in the same query.</p>
 
101
<p>You can retrieve the values of all the fields in a single variable (a map) using <a href="qsqlquery.html#boundValues">boundValues</a>().</p>
 
102
<p>Below we present the same example using each of the four different binding approaches, as well as one example of binding values to a stored procedure.</p>
 
103
<p><b>Named binding using named placeholders:</b></p>
 
104
<pre>&nbsp;       QSqlQuery query;
 
105
        query.prepare(&quot;INSERT INTO person (id, forename, surname) &quot;
 
106
                      &quot;VALUES (:id, :forename, :surname)&quot;);
 
107
        query.bindValue(&quot;:id&quot;, 1001);
 
108
        query.bindValue(&quot;:forename&quot;, &quot;Bart&quot;);
 
109
        query.bindValue(&quot;:surname&quot;, &quot;Simpson&quot;);
 
110
        query.exec();</pre>
 
111
<p><b>Positional binding using named placeholders:</b></p>
 
112
<pre>&nbsp;       QSqlQuery query;
 
113
        query.prepare(&quot;INSERT INTO person (id, forename, surname) &quot;
 
114
                      &quot;VALUES (:id, :forename, :surname)&quot;);
 
115
        query.bindValue(0, 1001);
 
116
        query.bindValue(1, &quot;Bart&quot;);
 
117
        query.bindValue(2, &quot;Simpson&quot;);
 
118
        query.exec();</pre>
 
119
<p><b>Binding values using positional placeholders (version 1):</b></p>
 
120
<pre>&nbsp;       QSqlQuery query;
 
121
        query.prepare(&quot;INSERT INTO person (id, forename, surname) &quot;
 
122
                      &quot;VALUES (?, ?, ?)&quot;);
 
123
        query.bindValue(0, 1001);
 
124
        query.bindValue(1, &quot;Bart&quot;);
 
125
        query.bindValue(2, &quot;Simpson&quot;);
 
126
        query.exec();</pre>
 
127
<p><b>Binding values using positional placeholders (version 2):</b></p>
 
128
<pre>&nbsp;       QSqlQuery query;
 
129
        query.prepare(&quot;INSERT INTO person (id, forename, surname) &quot;
 
130
                      &quot;VALUES (?, ?, ?)&quot;);
 
131
        query.addBindValue(1001);
 
132
        query.addBindValue(&quot;Bart&quot;);
 
133
        query.addBindValue(&quot;Simpson&quot;);
 
134
        query.exec();</pre>
 
135
<p><b>Binding values to a stored procedure:</b></p>
 
136
<p>This code calls a stored procedure called <tt>AsciiToInt()</tt>, passing it a character through its in parameter, and taking its result in the out parameter.</p>
 
137
<pre>&nbsp;       QSqlQuery query;
 
138
        query.prepare(&quot;CALL AsciiToInt(?, ?)&quot;);
 
139
        query.bindValue(0, &quot;A&quot;);
 
140
        query.bindValue(1, 0, QSql::Out);
 
141
        query.exec();
 
142
        int i = query.boundValue(1).toInt(); // i is 65</pre>
 
143
<p>See also <a href="qsqldatabase.html">QSqlDatabase</a>, <a href="qsqlquerymodel.html">QSqlQueryModel</a>, <a href="qsqltablemodel.html">QSqlTableModel</a>, and <a href="qvariant.html">QVariant</a>.</p>
 
144
<hr />
 
145
<h2>Member Function Documentation</h2>
 
146
<h3 class="fn"><a name="QSqlQuery"></a>QSqlQuery::QSqlQuery ( <a href="qsqlresult.html">QSqlResult</a> * <i>result</i> )</h3>
 
147
<p>Constructs a <a href="qsqlquery.html">QSqlQuery</a> object which uses the <a href="qsqlresult.html">QSqlResult</a> <i>result</i> to communicate with a database.</p>
 
148
<h3 class="fn"><a name="QSqlQuery-2"></a>QSqlQuery::QSqlQuery ( const <a href="qstring.html">QString</a> &amp; <i>query</i> = QString(), <a href="qsqldatabase.html">QSqlDatabase</a> <i>db</i> = QSqlDatabase() )</h3>
 
149
<p>Constructs a <a href="qsqlquery.html">QSqlQuery</a> object using the SQL <i>query</i> and the database <i>db</i>. If <i>db</i> is not specified, the application's default database is used. If <i>query</i> is not an empty string, it will be executed.</p>
 
150
<p>See also <a href="qsqldatabase.html">QSqlDatabase</a>.</p>
 
151
<h3 class="fn"><a name="QSqlQuery-3"></a>QSqlQuery::QSqlQuery ( <a href="qsqldatabase.html">QSqlDatabase</a> <i>db</i> )</h3>
 
152
<p>Constructs a <a href="qsqlquery.html">QSqlQuery</a> object using the database <i>db</i>.</p>
 
153
<p>See also <a href="qsqldatabase.html">QSqlDatabase</a>.</p>
 
154
<h3 class="fn"><a name="QSqlQuery-4"></a>QSqlQuery::QSqlQuery ( const QSqlQuery &amp; <i>other</i> )</h3>
 
155
<p>Constructs a copy of <i>other</i>.</p>
 
156
<h3 class="fn"><a name="dtor.QSqlQuery"></a>QSqlQuery::~QSqlQuery ()</h3>
 
157
<p>Destroys the object and frees any allocated resources.</p>
 
158
<h3 class="fn"><a name="addBindValue"></a>void QSqlQuery::addBindValue ( const <a href="qvariant.html">QVariant</a> &amp; <i>val</i>, <a href="qsql.html#ParamTypeFlag-enum">QSql::ParamType</a> <i>paramType</i> = QSql::In )</h3>
 
159
<p>Adds the value <i>val</i> to the list of values when using positional value binding. The order of the addBindValue() calls determines which placeholder a value will be bound to in the prepared query. If <i>paramType</i> is <tt>QSql::Out</tt> or <tt>QSql::InOut</tt>, the placeholder will be overwritten with data from the database after the <a href="qsqlquery.html#exec">exec</a>() call.</p>
 
160
<p>See also <a href="qsqlquery.html#bindValue">bindValue</a>(), <a href="qsqlquery.html#prepare">prepare</a>(), <a href="qsqlquery.html#exec">exec</a>(), <a href="qsqlquery.html#boundValue">boundValue</a>(), and <a href="qsqlquery.html#boundValues">boundValues</a>().</p>
 
161
<h3 class="fn"><a name="at"></a>int QSqlQuery::at () const</h3>
 
162
<p>Returns the current internal position of the query. The first record is at position zero. If the position is invalid, the function returns <a href="qsql.html#Location-enum">QSql::BeforeFirstRow</a> or <a href="qsql.html#Location-enum">QSql::AfterLastRow</a>, which are special negative values.</p>
 
163
<p>See also <a href="qsqlquery.html#previous">previous</a>(), <a href="qsqlquery.html#next">next</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#isActive">isActive</a>(), and <a href="qsqlquery.html#isValid">isValid</a>().</p>
 
164
<h3 class="fn"><a name="bindValue"></a>void QSqlQuery::bindValue ( const <a href="qstring.html">QString</a> &amp; <i>placeholder</i>, const <a href="qvariant.html">QVariant</a> &amp; <i>val</i>, <a href="qsql.html#ParamTypeFlag-enum">QSql::ParamType</a> <i>paramType</i> = QSql::In )</h3>
 
165
<p>Set the placeholder <i>placeholder</i> to be bound to value <i>val</i> in the prepared statement. Note that the placeholder mark (e.g <tt>:</tt>) must be included when specifying the placeholder name. If <i>paramType</i> is <tt>QSql::Out</tt> or <tt>QSql::InOut</tt>, the placeholder will be overwritten with data from the database after the <a href="qsqlquery.html#exec">exec</a>() call.</p>
 
166
<p>See also <a href="qsqlquery.html#addBindValue">addBindValue</a>(), <a href="qsqlquery.html#prepare">prepare</a>(), <a href="qsqlquery.html#exec">exec</a>(), <a href="qsqlquery.html#boundValue">boundValue</a>(), and <a href="qsqlquery.html#boundValues">boundValues</a>().</p>
 
167
<h3 class="fn"><a name="bindValue-2"></a>void QSqlQuery::bindValue ( int <i>pos</i>, const <a href="qvariant.html">QVariant</a> &amp; <i>val</i>, <a href="qsql.html#ParamTypeFlag-enum">QSql::ParamType</a> <i>paramType</i> = QSql::In )</h3>
 
168
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
169
<p>Set the placeholder in position <i>pos</i> to be bound to value <i>val</i> in the prepared statement. Field numbering starts at 0. If <i>paramType</i> is <tt>QSql::Out</tt> or <tt>QSql::InOut</tt>, the placeholder will be overwritten with data from the database after the <a href="qsqlquery.html#exec">exec</a>() call.</p>
 
170
<h3 class="fn"><a name="boundValue"></a><a href="qvariant.html">QVariant</a> QSqlQuery::boundValue ( const <a href="qstring.html">QString</a> &amp; <i>placeholder</i> ) const</h3>
 
171
<p>Returns the value for the <i>placeholder</i>.</p>
 
172
<p>See also <a href="qsqlquery.html#boundValues">boundValues</a>(), <a href="qsqlquery.html#bindValue">bindValue</a>(), and <a href="qsqlquery.html#addBindValue">addBindValue</a>().</p>
 
173
<h3 class="fn"><a name="boundValue-2"></a><a href="qvariant.html">QVariant</a> QSqlQuery::boundValue ( int <i>pos</i> ) const</h3>
 
174
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
175
<p>Returns the value for the placeholder at position <i>pos</i>.</p>
 
176
<h3 class="fn"><a name="boundValues"></a><a href="qmap.html">QMap</a>&lt;<a href="qstring.html">QString</a>, <a href="qvariant.html">QVariant</a>&gt; QSqlQuery::boundValues () const</h3>
 
177
<p>Returns a map of the bound values.</p>
 
178
<p>With named binding, the bound values can be examined in the following ways:</p>
 
179
<pre>&nbsp;       QMapIterator&lt;QString, QVariant&gt; i(query.boundValues());
 
180
        while (i.hasNext()) {
 
181
            i.next();
 
182
            cout &lt;&lt; i.key().toAscii().data() &lt;&lt; &quot;: &quot;
 
183
                 &lt;&lt; i.value().toString().toAscii().data() &lt;&lt; endl;
 
184
        }</pre>
 
185
<p>With positional binding, the code becomes:</p>
 
186
<pre>&nbsp;       QList&lt;QVariant&gt; list = query.boundValues().values();
 
187
        for (int i = 0; i &lt; list.size(); ++i)
 
188
            cout &lt;&lt; i &lt;&lt; &quot;: &quot; &lt;&lt; list.at(i).toString().toAscii().data() &lt;&lt; endl;</pre>
 
189
<p>See also <a href="qsqlquery.html#boundValue">boundValue</a>(), <a href="qsqlquery.html#bindValue">bindValue</a>(), and <a href="qsqlquery.html#addBindValue">addBindValue</a>().</p>
 
190
<h3 class="fn"><a name="clear"></a>void QSqlQuery::clear ()</h3>
 
191
<p>Clears the result set and releases any resources held by the query. You should rarely if ever need to call this function.</p>
 
192
<h3 class="fn"><a name="driver"></a>const <a href="qsqldriver.html">QSqlDriver</a> * QSqlQuery::driver () const</h3>
 
193
<p>Returns the database driver associated with the query.</p>
 
194
<h3 class="fn"><a name="exec"></a>bool QSqlQuery::exec ( const <a href="qstring.html">QString</a> &amp; <i>query</i> )</h3>
 
195
<p>Executes the SQL in <i>query</i>. Returns true and sets the query state to active if the query was successful; otherwise returns false. The <i>query</i> string must use syntax appropriate for the SQL database being queried (for example, standard SQL).</p>
 
196
<p>After the query is executed, the query is positioned on an <i>invalid</i> record and must be navigated to a valid record before data values can be retrieved (for example, using <a href="qsqlquery.html#next">next</a>()).</p>
 
197
<p>Note that the last error for this query is reset when exec() is called.</p>
 
198
<p>Example:</p>
 
199
<pre>&nbsp;       QSqlQuery query(&quot;SELECT country FROM artist&quot;);
 
200
        while (query.next()) {
 
201
            QString country = query.value(0).toString();
 
202
            doSomething(country);
 
203
        }</pre>
 
204
<p>See also <a href="qsqlquery.html#isActive">isActive</a>(), <a href="qsqlquery.html#isValid">isValid</a>(), <a href="qsqlquery.html#next">next</a>(), <a href="qsqlquery.html#previous">previous</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), and <a href="qsqlquery.html#seek">seek</a>().</p>
 
205
<h3 class="fn"><a name="exec-2"></a>bool QSqlQuery::exec ()</h3>
 
206
<p>This is an overloaded member function, provided for convenience. It behaves essentially like the above function.</p>
 
207
<p>Executes a previously prepared SQL query. Returns true if the query executed successfully; otherwise returns false.</p>
 
208
<p>See also <a href="qsqlquery.html#prepare">prepare</a>(), <a href="qsqlquery.html#bindValue">bindValue</a>(), <a href="qsqlquery.html#addBindValue">addBindValue</a>(), <a href="qsqlquery.html#boundValue">boundValue</a>(), and <a href="qsqlquery.html#boundValues">boundValues</a>().</p>
 
209
<h3 class="fn"><a name="executedQuery"></a><a href="qstring.html">QString</a> QSqlQuery::executedQuery () const</h3>
 
210
<p>Returns the last query that was executed.</p>
 
211
<p>In most cases this function returns the same string as <a href="qsqlquery.html#lastQuery">lastQuery</a>(). If a prepared query with placeholders is executed on a DBMS that does not support it, the preparation of this query is emulated. The placeholders in the original query are replaced with their bound values to form a new query. This function returns the modified query. It is mostly useful for debugging purposes.</p>
 
212
<p>See also <a href="qsqlquery.html#lastQuery">lastQuery</a>().</p>
 
213
<h3 class="fn"><a name="first"></a>bool QSqlQuery::first ()</h3>
 
214
<p>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="qsqlquery.html#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>
 
215
<p>See also <a href="qsqlquery.html#next">next</a>(), <a href="qsqlquery.html#previous">previous</a>(), <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#at">at</a>(), <a href="qsqlquery.html#isActive">isActive</a>(), and <a href="qsqlquery.html#isValid">isValid</a>().</p>
 
216
<h3 class="fn"><a name="isActive"></a>bool QSqlQuery::isActive () const</h3>
 
217
<p>Returns true if the query is currently active; otherwise returns false.</p>
 
218
<h3 class="fn"><a name="isForwardOnly"></a>bool QSqlQuery::isForwardOnly () const</h3>
 
219
<p>Returns true if you can only scroll forward through a result set; otherwise returns false.</p>
 
220
<p>See also <a href="qsqlquery.html#setForwardOnly">setForwardOnly</a>() and <a href="qsqlquery.html#next">next</a>().</p>
 
221
<h3 class="fn"><a name="isNull"></a>bool QSqlQuery::isNull ( int <i>field</i> ) const</h3>
 
222
<p>Returns true if the query is active and positioned on a valid record and the <i>field</i> is NULL; otherwise returns false. Note that for some drivers, isNull() will not return accurate information until after an attempt is made to retrieve data.</p>
 
223
<p>See also <a href="qsqlquery.html#isActive">isActive</a>(), <a href="qsqlquery.html#isValid">isValid</a>(), and <a href="qsqlquery.html#value">value</a>().</p>
 
224
<h3 class="fn"><a name="isSelect"></a>bool QSqlQuery::isSelect () const</h3>
 
225
<p>Returns true if the current query is a <tt>SELECT</tt> statement; otherwise returns false.</p>
 
226
<h3 class="fn"><a name="isValid"></a>bool QSqlQuery::isValid () const</h3>
 
227
<p>Returns true if the query is currently positioned on a valid record; otherwise returns false.</p>
 
228
<h3 class="fn"><a name="last"></a>bool QSqlQuery::last ()</h3>
 
229
<p>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="qsqlquery.html#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>
 
230
<p>See also <a href="qsqlquery.html#next">next</a>(), <a href="qsqlquery.html#previous">previous</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#at">at</a>(), <a href="qsqlquery.html#isActive">isActive</a>(), and <a href="qsqlquery.html#isValid">isValid</a>().</p>
 
231
<h3 class="fn"><a name="lastError"></a><a href="qsqlerror.html">QSqlError</a> QSqlQuery::lastError () const</h3>
 
232
<p>Returns error information about the last error (if any) that occurred.</p>
 
233
<p>See also <a href="qsqlerror.html">QSqlError</a>.</p>
 
234
<h3 class="fn"><a name="lastInsertId"></a><a href="qvariant.html">QVariant</a> QSqlQuery::lastInsertId () const</h3>
 
235
<p>Returns the object ID of the most recent inserted row if the database supports it. An invalid <a href="qvariant.html">QVariant</a> will be returned if the query did not insert any value or if the database does not report the id back. If more than one row was touched by the insert, the behavior is undefined.</p>
 
236
<p>See also <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>().</p>
 
237
<h3 class="fn"><a name="lastQuery"></a><a href="qstring.html">QString</a> QSqlQuery::lastQuery () const</h3>
 
238
<p>Returns the text of the current query being used, or an empty string if there is no current query text.</p>
 
239
<p>See also <a href="qsqlquery.html#executedQuery">executedQuery</a>().</p>
 
240
<h3 class="fn"><a name="next"></a>bool QSqlQuery::next ()</h3>
 
241
<p>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="qsqlquery.html#isSelect">isSelect</a>() must return true before calling this function or it will do nothing and return false.</p>
 
242
<p>The following rules apply:</p>
 
243
<ul>
 
244
<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>
 
245
<li>If the result is currently located after the last record, there is no change and false is returned.</li>
 
246
<li>If the result is located somewhere in the middle, an attempt is made to retrieve the next record.</li>
 
247
</ul>
 
248
<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>
 
249
<p>See also <a href="qsqlquery.html#previous">previous</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#at">at</a>(), <a href="qsqlquery.html#isActive">isActive</a>(), and <a href="qsqlquery.html#isValid">isValid</a>().</p>
 
250
<h3 class="fn"><a name="numRowsAffected"></a>int QSqlQuery::numRowsAffected () const</h3>
 
251
<p>Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. Note that for <tt>SELECT</tt> statements, the value is undefined; use <a href="qsqlquery.html#size">size</a>() instead. If the query is not active (<a href="qsqlquery.html#isActive">isActive</a>() returns false), -1 is returned.</p>
 
252
<p>See also <a href="qsqlquery.html#size">size</a>() and <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>().</p>
 
253
<h3 class="fn"><a name="prepare"></a>bool QSqlQuery::prepare ( const <a href="qstring.html">QString</a> &amp; <i>query</i> )</h3>
 
254
<p>Prepares the SQL query <i>query</i> for execution. The query may contain placeholders for binding values. Both Oracle style colon-name (e.g., <tt>:surname</tt>), and ODBC style (<tt>?</tt>) placeholders are supported; but they cannot be mixed in the same query. See the <a href="qsqlquery.html#qsqlquery-examples">Detailed Description</a> for examples.</p>
 
255
<p>See also <a href="qsqlquery.html#exec">exec</a>(), <a href="qsqlquery.html#bindValue">bindValue</a>(), and <a href="qsqlquery.html#addBindValue">addBindValue</a>().</p>
 
256
<h3 class="fn"><a name="previous"></a>bool QSqlQuery::previous ()</h3>
 
257
<p>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="qsqlquery.html#isSelect">isSelect</a>() must return true before calling this function or it will do nothing and return false.</p>
 
258
<p>The following rules apply:</p>
 
259
<ul>
 
260
<li>If the result is currently located before the first record, there is no change and false is returned.</li>
 
261
<li>If the result is currently located after the last record, an attempt is made to retrieve the last record.</li>
 
262
<li>If the result is somewhere in the middle, an attempt is made to retrieve the previous record.</li>
 
263
</ul>
 
264
<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>
 
265
<p>See also <a href="qsqlquery.html#next">next</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#at">at</a>(), <a href="qsqlquery.html#isActive">isActive</a>(), and <a href="qsqlquery.html#isValid">isValid</a>().</p>
 
266
<h3 class="fn"><a name="record"></a><a href="qsqlrecord.html">QSqlRecord</a> QSqlQuery::record () const</h3>
 
267
<p>Returns a <a href="qsqlrecord.html">QSqlRecord</a> containing the field information for the current query. If the query points to a valid row (<a href="qsqlquery.html#isValid">isValid</a>() returns true), the record is populated with the row's values. An empty record is returned when there is no active query (<a href="qsqlquery.html#isActive">isActive</a>() returns false).</p>
 
268
<p>To retrieve values from a query, <a href="qsqlquery.html#value">value</a>() should be used since its index-based lookup is faster.</p>
 
269
<p>In the following example, a <tt>SELECT * FROM</tt> query is executed. Since the order of the columns is not defined, <a href="qsqlrecord.html#indexOf">QSqlRecord::indexOf</a>() is used to obtain the index of a column.</p>
 
270
<pre>&nbsp;   QSqlQuery q(&quot;select * from employees&quot;);
 
271
    QSqlRecord rec = q.record();
 
272
 
 
273
    qDebug() &lt;&lt; &quot;Number of columns: &quot; &lt;&lt; rec.count();
 
274
 
 
275
    int nameCol = rec.indexOf(&quot;name&quot;); // index of the field &quot;name&quot;
 
276
    while (q.next())
 
277
        qDebug() &lt;&lt; q.value(nameCol).toString(); // output all names</pre>
 
278
<p>See also <a href="qsqlquery.html#value">value</a>().</p>
 
279
<h3 class="fn"><a name="result"></a>const <a href="qsqlresult.html">QSqlResult</a> * QSqlQuery::result () const</h3>
 
280
<p>Returns the result associated with the query.</p>
 
281
<h3 class="fn"><a name="seek"></a>bool QSqlQuery::seek ( int <i>index</i>, bool <i>relative</i> = false )</h3>
 
282
<p>Retrieves the record at position <i>index</i>, if available, and positions the query on the retrieved record. The first record is at position 0. Note that the query must be in an active state and <a href="qsqlquery.html#isSelect">isSelect</a>() must return true before calling this function.</p>
 
283
<p>If <i>relative</i> is false (the default), the following rules apply:</p>
 
284
<ul>
 
285
<li>If <i>index</i> is negative, the result is positioned before the first record and false is returned.</li>
 
286
<li>Otherwise, an attempt is made to move to the record at position <i>index</i>. If the record at position <i>index</i> 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.</li>
 
287
</ul>
 
288
<p>If <i>relative</i> is true, the following rules apply:</p>
 
289
<ul>
 
290
<li>If the result is currently positioned before the first record or on the first record, and <i>index</i> is negative, there is no change, and false is returned.</li>
 
291
<li>If the result is currently located after the last record, and <i>index</i> is positive, there is no change, and false is returned.</li>
 
292
<li>If the result is currently located somewhere in the middle, and the relative offset <i>index</i> moves the result below zero, the result is positioned before the first record and false is returned.</li>
 
293
<li>Otherwise, an attempt is made to move to the record <i>index</i> records ahead of the current record (or <i>index</i> records behind the current record if <i>index</i> is negative). If the record at offset <i>index</i> could not be retrieved, the result is positioned after the last record if <i>index</i> &gt;= 0, (or before the first record if <i>index</i> is negative), and false is returned. If the record is successfully retrieved, true is returned.</li>
 
294
</ul>
 
295
<p>See also <a href="qsqlquery.html#next">next</a>(), <a href="qsqlquery.html#previous">previous</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#at">at</a>(), <a href="qsqlquery.html#isActive">isActive</a>(), and <a href="qsqlquery.html#isValid">isValid</a>().</p>
 
296
<h3 class="fn"><a name="setForwardOnly"></a>void QSqlQuery::setForwardOnly ( bool <i>forward</i> )</h3>
 
297
<p>Sets forward only mode to <i>forward</i>. If <i>forward</i> is true, only <a href="qsqlquery.html#next">next</a>() and <a href="qsqlquery.html#seek">seek</a>() with positive values, are allowed for navigating the results. Forward only mode needs far less memory since results do not need to be cached.</p>
 
298
<p>Forward only mode is off by default.</p>
 
299
<p>See also <a href="qsqlquery.html#isForwardOnly">isForwardOnly</a>(), <a href="qsqlquery.html#next">next</a>(), and <a href="qsqlquery.html#seek">seek</a>().</p>
 
300
<h3 class="fn"><a name="size"></a>int QSqlQuery::size () const</h3>
 
301
<p>Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-<tt>SELECT</tt> statements (<a href="qsqlquery.html#isSelect">isSelect</a>() returns false), size() will return -1. If the query is not active (<a href="qsqlquery.html#isActive">isActive</a>() returns false), -1 is returned.</p>
 
302
<p>To determine the number of rows affected by a non-<tt>SELECT</tt> statement, use <a href="qsqlquery.html#numRowsAffected">numRowsAffected</a>().</p>
 
303
<p>See also <a href="qsqlquery.html#isActive">isActive</a>(), <a href="qsqlquery.html#numRowsAffected">numRowsAffected</a>(), and <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>().</p>
 
304
<h3 class="fn"><a name="value"></a><a href="qvariant.html">QVariant</a> QSqlQuery::value ( int <i>index</i> ) const</h3>
 
305
<p>Returns the value of field <i>index</i> in the current record.</p>
 
306
<p>The fields are numbered from left to right using the text of the <tt>SELECT</tt> statement, e.g. in</p>
 
307
<pre>&nbsp;   SELECT forename, surname FROM people;</pre>
 
308
<p>field 0 is <tt>forename</tt> and field 1 is <tt>surname</tt>. Using <tt>SELECT *</tt> is not recommended because the order of the fields in the query is undefined.</p>
 
309
<p>An invalid <a href="qvariant.html">QVariant</a> is returned if field <i>index</i> does not exist, if the query is inactive, or if the query is positioned on an invalid record.</p>
 
310
<p>See also <a href="qsqlquery.html#previous">previous</a>(), <a href="qsqlquery.html#next">next</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#isActive">isActive</a>(), and <a href="qsqlquery.html#isValid">isValid</a>().</p>
 
311
<h3 class="fn"><a name="operator-eq"></a>QSqlQuery &amp; QSqlQuery::operator= ( const QSqlQuery &amp; <i>other</i> )</h3>
 
312
<p>Assigns <i>other</i> to this object.</p>
 
313
<p /><address><hr /><div align="center">
 
314
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
315
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
316
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
317
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
318
</tr></table></div></address></body>
 
319
</html>