File: qsqlquery.html

package info (click to toggle)
python-qt4 4.0.1-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 18,632 kB
  • ctags: 2,639
  • sloc: python: 29,409; sh: 5,646; cpp: 3,168; xml: 149; makefile: 109
file content (238 lines) | stat: -rw-r--r-- 36,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QSqlQuery Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSqlQuery Class Reference<br /><sup><sup>[<a href="qtsql.html">QtSql</a> module]</sup></sup></h1><p>The QSqlQuery class provides a means of executing and manipulating SQL statements. <a href="#details">More...</a></p>
<h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsqlquery.html#QSqlQuery">__init__</a></b> (<i>self</i>, QSqlResult&#160;<i>r</i>)</li><li><div class="fn" /><b><a href="qsqlquery.html#QSqlQuery-2">__init__</a></b> (<i>self</i>, QString&#160;<i>query</i>&#160;=&#160;QString(), QSqlDatabase&#160;<i>db</i>&#160;=&#160;QSqlDatabase())</li><li><div class="fn" /><b><a href="qsqlquery.html#QSqlQuery-3">__init__</a></b> (<i>self</i>, QSqlDatabase&#160;<i>db</i>)</li><li><div class="fn" /><b><a href="qsqlquery.html#QSqlQuery-4">__init__</a></b> (<i>self</i>, QSqlQuery&#160;<i>other</i>)</li><li><div class="fn" /><b><a href="qsqlquery.html#addBindValue">addBindValue</a></b> (<i>self</i>, QVariant&#160;<i>val</i>, QSql.ParamType&#160;<i>type</i>&#160;=&#160;QSql.In)</li><li><div class="fn" />int <b><a href="qsqlquery.html#at">at</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsqlquery.html#bindValue">bindValue</a></b> (<i>self</i>, QString&#160;<i>placeholder</i>, QVariant&#160;<i>val</i>, QSql.ParamType&#160;<i>type</i>&#160;=&#160;QSql.In)</li><li><div class="fn" /><b><a href="qsqlquery.html#bindValue-2">bindValue</a></b> (<i>self</i>, int&#160;<i>pos</i>, QVariant&#160;<i>val</i>, QSql.ParamType&#160;<i>type</i>&#160;=&#160;QSql.In)</li><li><div class="fn" />QVariant <b><a href="qsqlquery.html#boundValue">boundValue</a></b> (<i>self</i>, QString&#160;<i>placeholder</i>)</li><li><div class="fn" />QVariant <b><a href="qsqlquery.html#boundValue-2">boundValue</a></b> (<i>self</i>, int&#160;<i>pos</i>)</li><li><div class="fn" />QString-QVariant-dict <b><a href="qsqlquery.html#boundValues">boundValues</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsqlquery.html#clear">clear</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlDriver <b><a href="qsqlquery.html#driver">driver</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#exec">exec_</a></b> (<i>self</i>, QString&#160;<i>query</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#exec-2">exec_</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqlquery.html#executedQuery">executedQuery</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#first">first</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#isActive">isActive</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#isForwardOnly">isForwardOnly</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#isNull">isNull</a></b> (<i>self</i>, int&#160;<i>field</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#isSelect">isSelect</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#isValid">isValid</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#last">last</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlError <b><a href="qsqlquery.html#lastError">lastError</a></b> (<i>self</i>)</li><li><div class="fn" />QVariant <b><a href="qsqlquery.html#lastInsertId">lastInsertId</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsqlquery.html#lastQuery">lastQuery</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#next">next</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qsqlquery.html#numRowsAffected">numRowsAffected</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#prepare">prepare</a></b> (<i>self</i>, QString&#160;<i>query</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#previous">previous</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlRecord <b><a href="qsqlquery.html#record">record</a></b> (<i>self</i>)</li><li><div class="fn" />QSqlResult <b><a href="qsqlquery.html#result">result</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsqlquery.html#seek">seek</a></b> (<i>self</i>, int&#160;<i>i</i>, bool&#160;<i>relative</i>&#160;=&#160;False)</li><li><div class="fn" /><b><a href="qsqlquery.html#setForwardOnly">setForwardOnly</a></b> (<i>self</i>, bool&#160;<i>forward</i>)</li><li><div class="fn" />int <b><a href="qsqlquery.html#size">size</a></b> (<i>self</i>)</li><li><div class="fn" />QVariant <b><a href="qsqlquery.html#value">value</a></b> (<i>self</i>, int&#160;<i>i</i>)</li></ul><a name="details" /><hr /><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, 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>
<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>
<a id="qsqlquery-examples" name="qsqlquery-examples" />
<p>Navigating records is performed with the following functions:</p>
<ul>
<li><a href="qsqlquery.html#next">next</a>()</li>
<li><a href="qsqlquery.html#previous">previous</a>()</li>
<li><a href="qsqlquery.html#first">first</a>()</li>
<li><a href="qsqlquery.html#last">last</a>()</li>
<li><a href="qsqlquery.html#seek">seek</a>()</li>
</ul>
<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>
<p>For example:</p>
<pre>
        QSqlQuery query("SELECT country FROM artist");
        while (query.next()) {
            QString country = query.value(0).toString();
            doSomething(country);
        }
</pre>
<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>
<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>
<pre>
        QSqlQuery query("SELECT * FROM artist");
        int fieldNo = query.record().indexOf("country");
        while (query.next()) {
            QString country = query.value(fieldNo).toString();
            doSomething(country);
        }
</pre>
<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>
<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>
<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>
<a id="approaches-to-binding-values" name="approaches-to-binding-values" />
<h3>Approaches to Binding Values</h3>
<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>
<p><b>Named binding using named placeholders:</b></p>
<pre>
        QSqlQuery query;
        query.prepare("INSERT INTO person (id, forename, surname) "
                      "VALUES (:id, :forename, :surname)");
        query.bindValue(":id", 1001);
        query.bindValue(":forename", "Bart");
        query.bindValue(":surname", "Simpson");
        query.exec();
</pre>
<p><b>Positional binding using named placeholders:</b></p>
<pre>
        QSqlQuery query;
        query.prepare("INSERT INTO person (id, forename, surname) "
                      "VALUES (:id, :forename, :surname)");
        query.bindValue(0, 1001);
        query.bindValue(1, "Bart");
        query.bindValue(2, "Simpson");
        query.exec();
</pre>
<p><b>Binding values using positional placeholders (version 1):</b></p>
<pre>
        QSqlQuery query;
        query.prepare("INSERT INTO person (id, forename, surname) "
                      "VALUES (?, ?, ?)");
        query.bindValue(0, 1001);
        query.bindValue(1, "Bart");
        query.bindValue(2, "Simpson");
        query.exec();
</pre>
<p><b>Binding values using positional placeholders (version 2):</b></p>
<pre>
        QSqlQuery query;
        query.prepare("INSERT INTO person (id, forename, surname) "
                      "VALUES (?, ?, ?)");
        query.addBindValue(1001);
        query.addBindValue("Bart");
        query.addBindValue("Simpson");
        query.exec();
</pre>
<p><b>Binding values to a stored procedure:</b></p>
<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>
<pre>
        QSqlQuery query;
        query.prepare("CALL AsciiToInt(?, ?)");
        query.bindValue(0, "A");
        query.bindValue(1, 0, QSql.Out);
        query.exec();
        int i = query.boundValue(1).toInt(); // i is 65
</pre>
<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>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSqlQuery" />QSqlQuery.__init__ (<i>self</i>, <a href="qsqlresult.html">QSqlResult</a>&#160;<i>r</i>)</h3><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>
<h3 class="fn"><a name="QSqlQuery-2" />QSqlQuery.__init__ (<i>self</i>, <a href="qstring.html">QString</a>&#160;<i>query</i>&#160;=&#160;QString(), <a href="qsqldatabase.html">QSqlDatabase</a>&#160;<i>db</i>&#160;=&#160;QSqlDatabase())</h3><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>
<p>See also <a href="qsqldatabase.html">QSqlDatabase</a>.</p>
<h3 class="fn"><a name="QSqlQuery-3" />QSqlQuery.__init__ (<i>self</i>, <a href="qsqldatabase.html">QSqlDatabase</a>&#160;<i>db</i>)</h3><p>Constructs a <a href="qsqlquery.html">QSqlQuery</a> object using the database <i>db</i>.</p>
<p>See also <a href="qsqldatabase.html">QSqlDatabase</a>.</p>
<h3 class="fn"><a name="QSqlQuery-4" />QSqlQuery.__init__ (<i>self</i>, <a href="qsqlquery.html">QSqlQuery</a>&#160;<i>other</i>)</h3><p>Constructs a copy of <i>other</i>.</p>
<h3 class="fn"><a name="addBindValue" />QSqlQuery.addBindValue (<i>self</i>, <a href="qvariant.html">QVariant</a>&#160;<i>val</i>, <a href="qsql-paramtype.html">QSql.ParamType</a>&#160;<i>type</i>&#160;=&#160;QSql.In)</h3><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 <a href="qsql.html#ParamTypeFlag-enum">QSql.Out</a> or <a href="qsql.html#ParamTypeFlag-enum">QSql.InOut</a>, the placeholder will be overwritten with data from the database after the <a href="qsqlquery.html#exec">exec_</a>() call.</p>
<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>
<h3 class="fn"><a name="at" />int QSqlQuery.at (<i>self</i>)</h3><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>
<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>
<h3 class="fn"><a name="bindValue" />QSqlQuery.bindValue (<i>self</i>, <a href="qstring.html">QString</a>&#160;<i>placeholder</i>, <a href="qvariant.html">QVariant</a>&#160;<i>val</i>, <a href="qsql-paramtype.html">QSql.ParamType</a>&#160;<i>type</i>&#160;=&#160;QSql.In)</h3><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 <a href="qsql.html#ParamTypeFlag-enum">QSql.Out</a> or <a href="qsql.html#ParamTypeFlag-enum">QSql.InOut</a>, the placeholder will be overwritten with data from the database after the <a href="qsqlquery.html#exec">exec_</a>() call.</p>
<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>
<h3 class="fn"><a name="bindValue-2" />QSqlQuery.bindValue (<i>self</i>, int&#160;<i>pos</i>, <a href="qvariant.html">QVariant</a>&#160;<i>val</i>, <a href="qsql-paramtype.html">QSql.ParamType</a>&#160;<i>type</i>&#160;=&#160;QSql.In)</h3><p>This is an overloaded member function, provided for convenience.</p>
<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 <a href="qsql.html#ParamTypeFlag-enum">QSql.Out</a> or <a href="qsql.html#ParamTypeFlag-enum">QSql.InOut</a>, the placeholder will be overwritten with data from the database after the <a href="qsqlquery.html#exec">exec_</a>() call.</p>
<h3 class="fn"><a name="boundValue" /><a href="qvariant.html">QVariant</a> QSqlQuery.boundValue (<i>self</i>, <a href="qstring.html">QString</a>&#160;<i>placeholder</i>)</h3><p>Returns the value for the <i>placeholder</i>.</p>
<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>
<h3 class="fn"><a name="boundValue-2" /><a href="qvariant.html">QVariant</a> QSqlQuery.boundValue (<i>self</i>, int&#160;<i>pos</i>)</h3><p>This is an overloaded member function, provided for convenience.</p>
<p>Returns the value for the placeholder at position <i>pos</i>.</p>
<h3 class="fn"><a name="boundValues" />QString-QVariant-dict QSqlQuery.boundValues (<i>self</i>)</h3><p>Returns a map of the bound values.</p>
<p>With named binding, the bound values can be examined in the following ways:</p>
<pre>
        QMapIterator&lt;QString, QVariant&gt; i(query.boundValues());
        while (i.hasNext()) {
            i.next();
            cout &lt;&lt; i.key().toAscii().data() &lt;&lt; ": "
                 &lt;&lt; i.value().toString().toAscii().data() &lt;&lt; endl;
        }
</pre>
<p>With positional binding, the code becomes:</p>
<pre>
        QList&lt;QVariant&gt; list = query.boundValues().values();
        for (int i = 0; i &lt; list.size(); ++i)
            cout &lt;&lt; i &lt;&lt; ": " &lt;&lt; list.at(i).toString().toAscii().data() &lt;&lt; endl;
</pre>
<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>
<h3 class="fn"><a name="clear" />QSqlQuery.clear (<i>self</i>)</h3><p>Clears the result set and releases any resources held by the query. You should rarely if ever need to call this function.</p>
<h3 class="fn"><a name="driver" /><a href="qsqldriver.html">QSqlDriver</a> QSqlQuery.driver (<i>self</i>)</h3><p>Returns the database driver associated with the query.</p>
<h3 class="fn"><a name="exec" />bool QSqlQuery.exec_ (<i>self</i>, <a href="qstring.html">QString</a>&#160;<i>query</i>)</h3><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>
<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>
<p>Note that the last error for this query is reset when exec() is called.</p>
<p>Example:</p>
<pre>
        QSqlQuery query;
        query.prepare("INSERT INTO person (id, forename, surname) "
                      "VALUES (:id, :forename, :surname)");
        query.bindValue(":id", 1001);
        query.bindValue(":forename", "Bart");
        query.bindValue(":surname", "Simpson");
        query.exec();
</pre>
<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>
<h3 class="fn"><a name="exec-2" />bool QSqlQuery.exec_ (<i>self</i>)</h3><p>This is an overloaded member function, provided for convenience.</p>
<p>Executes a previously prepared SQL query. Returns true if the query executed successfully; otherwise returns false.</p>
<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>
<h3 class="fn"><a name="executedQuery" /><a href="qstring.html">QString</a> QSqlQuery.executedQuery (<i>self</i>)</h3><p>Returns the last query that was executed.</p>
<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>
<p>See also <a href="qsqlquery.html#lastQuery">lastQuery</a>().</p>
<h3 class="fn"><a name="first" />bool QSqlQuery.first (<i>self</i>)</h3><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>
<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>
<h3 class="fn"><a name="isActive" />bool QSqlQuery.isActive (<i>self</i>)</h3><p>Returns true if the query is currently active; otherwise returns false.</p>
<h3 class="fn"><a name="isForwardOnly" />bool QSqlQuery.isForwardOnly (<i>self</i>)</h3><p>Returns true if you can only scroll forward through a result set; otherwise returns false.</p>
<p>See also <a href="qsqlquery.html#setForwardOnly">setForwardOnly</a>() and <a href="qsqlquery.html#next">next</a>().</p>
<h3 class="fn"><a name="isNull" />bool QSqlQuery.isNull (<i>self</i>, int&#160;<i>field</i>)</h3><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>
<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>
<h3 class="fn"><a name="isSelect" />bool QSqlQuery.isSelect (<i>self</i>)</h3><p>Returns true if the current query is a <tt>SELECT</tt> statement; otherwise returns false.</p>
<h3 class="fn"><a name="isValid" />bool QSqlQuery.isValid (<i>self</i>)</h3><p>Returns true if the query is currently positioned on a valid record; otherwise returns false.</p>
<h3 class="fn"><a name="last" />bool QSqlQuery.last (<i>self</i>)</h3><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>
<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>
<h3 class="fn"><a name="lastError" /><a href="qsqlerror.html">QSqlError</a> QSqlQuery.lastError (<i>self</i>)</h3><p>Returns error information about the last error (if any) that occurred with this query.</p>
<p>See also <a href="qsqlerror.html">QSqlError</a> and <a href="qsqldatabase.html#lastError">QSqlDatabase.lastError</a>().</p>
<h3 class="fn"><a name="lastInsertId" /><a href="qvariant.html">QVariant</a> QSqlQuery.lastInsertId (<i>self</i>)</h3><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>
<p>See also <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>().</p>
<h3 class="fn"><a name="lastQuery" /><a href="qstring.html">QString</a> QSqlQuery.lastQuery (<i>self</i>)</h3><p>Returns the text of the current query being used, or an empty string if there is no current query text.</p>
<p>See also <a href="qsqlquery.html#executedQuery">executedQuery</a>().</p>
<h3 class="fn"><a name="next" />bool QSqlQuery.next (<i>self</i>)</h3><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>
<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>
<li>If the result is currently located after the last record, there is no change and false is returned.</li>
<li>If the result is located somewhere in the middle, an attempt is made to retrieve the next record.</li>
</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="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>
<h3 class="fn"><a name="numRowsAffected" />int QSqlQuery.numRowsAffected (<i>self</i>)</h3><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>
<p>See also <a href="qsqlquery.html#size">size</a>() and <a href="qsqldriver.html#hasFeature">QSqlDriver.hasFeature</a>().</p>
<h3 class="fn"><a name="prepare" />bool QSqlQuery.prepare (<i>self</i>, <a href="qstring.html">QString</a>&#160;<i>query</i>)</h3><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>
<p>Portability note: Some databases choose to delay preparing a query until it is executed the first time. In this case, preparing a syntactically wrong query succeeds, but every consecutive <a href="qsqlquery.html#exec">exec_</a>() will fail.</p>
<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>
<h3 class="fn"><a name="previous" />bool QSqlQuery.previous (<i>self</i>)</h3><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>
<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>
<li>If the result is currently located after the last record, an attempt is made to retrieve the last record.</li>
<li>If the result is somewhere in the middle, an attempt is made to retrieve the previous record.</li>
</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="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>
<h3 class="fn"><a name="record" /><a href="qsqlrecord.html">QSqlRecord</a> QSqlQuery.record (<i>self</i>)</h3><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>
<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>
<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>
<pre>
    QSqlQuery q("select * from employees");
    QSqlRecord rec = q.record();

    qDebug() &lt;&lt; "Number of columns: " &lt;&lt; rec.count();

    int nameCol = rec.indexOf("name"); // index of the field "name"
    while (q.next())
        qDebug() &lt;&lt; q.value(nameCol).toString(); // output all names
</pre>
<p>See also <a href="qsqlquery.html#value">value</a>().</p>
<h3 class="fn"><a name="result" /><a href="qsqlresult.html">QSqlResult</a> QSqlQuery.result (<i>self</i>)</h3><p>Returns the result associated with the query.</p>
<h3 class="fn"><a name="seek" />bool QSqlQuery.seek (<i>self</i>, int&#160;<i>i</i>, bool&#160;<i>relative</i>&#160;=&#160;False)</h3><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>
<p>If <i>relative</i> is false (the default), the following rules apply:</p>
<ul>
<li>If <i>index</i> is negative, the result is positioned before the first record and false is returned.</li>
<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>
</ul>
<p>If <i>relative</i> 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 <i>index</i> is negative, there is no change, and false is returned.</li>
<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>
<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>
<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>
</ul>
<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>
<h3 class="fn"><a name="setForwardOnly" />QSqlQuery.setForwardOnly (<i>self</i>, bool&#160;<i>forward</i>)</h3><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>
<p>Forward only mode is off by default.</p>
<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>
<h3 class="fn"><a name="size" />int QSqlQuery.size (<i>self</i>)</h3><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>
<p>To determine the number of rows affected by a non-<tt>SELECT</tt> statement, use <a href="qsqlquery.html#numRowsAffected">numRowsAffected</a>().</p>
<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>
<h3 class="fn"><a name="value" /><a href="qvariant.html">QVariant</a> QSqlQuery.value (<i>self</i>, int&#160;<i>i</i>)</h3><p>Returns the value of field <i>index</i> in the current record.</p>
<p>The fields are numbered from left to right using the text of the <tt>SELECT</tt> statement, e.g. in</p>
<pre>
    SELECT forename, surname FROM people;
</pre>
<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>
<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>
<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>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td width="25%">PyQt&#160;4.0.1 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.trolltech.com">Trolltech&#160;AS</a> 2006</td><td align="right" width="25%">Qt&#160;4.1.4</td></tr></table></div></address></body></html>