1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/sql/qsqlquery.cpp:79 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>QSqlQuery Class</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <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>
|
---|
33 |
|
---|
34 | <p>The QSqlQuery class provides a means of executing and
|
---|
35 | manipulating SQL statements.
|
---|
36 | <a href="#details">More...</a>
|
---|
37 | <p><tt>#include <<a href="qsqlquery-h.html">qsqlquery.h</a>></tt>
|
---|
38 | <p>Inherited by <a href="qsqlcursor.html">QSqlCursor</a>.
|
---|
39 | <p><a href="qsqlquery-members.html">List of all member functions.</a>
|
---|
40 | <h2>Public Members</h2>
|
---|
41 | <ul>
|
---|
42 | <li class=fn><a href="#QSqlQuery"><b>QSqlQuery</b></a> ( QSqlResult * r )</li>
|
---|
43 | <li class=fn><a href="#QSqlQuery-2"><b>QSqlQuery</b></a> ( const QString & query = QString::null, QSqlDatabase * db = 0 )</li>
|
---|
44 | <li class=fn>explicit <a href="#QSqlQuery-3"><b>QSqlQuery</b></a> ( QSqlDatabase * db )</li>
|
---|
45 | <li class=fn><a href="#QSqlQuery-4"><b>QSqlQuery</b></a> ( const QSqlQuery & other )</li>
|
---|
46 | <li class=fn>QSqlQuery & <a href="#operator-eq"><b>operator=</b></a> ( const QSqlQuery & other )</li>
|
---|
47 | <li class=fn>virtual <a href="#~QSqlQuery"><b>~QSqlQuery</b></a> ()</li>
|
---|
48 | <li class=fn>bool <a href="#isValid"><b>isValid</b></a> () const</li>
|
---|
49 | <li class=fn>bool <a href="#isActive"><b>isActive</b></a> () const</li>
|
---|
50 | <li class=fn>bool <a href="#isNull"><b>isNull</b></a> ( int field ) const</li>
|
---|
51 | <li class=fn>int <a href="#at"><b>at</b></a> () const</li>
|
---|
52 | <li class=fn>QString <a href="#lastQuery"><b>lastQuery</b></a> () const</li>
|
---|
53 | <li class=fn>int <a href="#numRowsAffected"><b>numRowsAffected</b></a> () const</li>
|
---|
54 | <li class=fn>QSqlError <a href="#lastError"><b>lastError</b></a> () const</li>
|
---|
55 | <li class=fn>bool <a href="#isSelect"><b>isSelect</b></a> () const</li>
|
---|
56 | <li class=fn>int <a href="#size"><b>size</b></a> () const</li>
|
---|
57 | <li class=fn>const QSqlDriver * <a href="#driver"><b>driver</b></a> () const</li>
|
---|
58 | <li class=fn>const QSqlResult * <a href="#result"><b>result</b></a> () const</li>
|
---|
59 | <li class=fn>bool <a href="#isForwardOnly"><b>isForwardOnly</b></a> () const</li>
|
---|
60 | <li class=fn>void <a href="#setForwardOnly"><b>setForwardOnly</b></a> ( bool forward )</li>
|
---|
61 | <li class=fn>virtual bool <a href="#exec"><b>exec</b></a> ( const QString & query )</li>
|
---|
62 | <li class=fn>virtual QVariant <a href="#value"><b>value</b></a> ( int i ) const</li>
|
---|
63 | <li class=fn>virtual bool <a href="#seek"><b>seek</b></a> ( int i, bool relative = FALSE )</li>
|
---|
64 | <li class=fn>virtual bool <a href="#next"><b>next</b></a> ()</li>
|
---|
65 | <li class=fn>virtual bool <a href="#prev"><b>prev</b></a> ()</li>
|
---|
66 | <li class=fn>virtual bool <a href="#first"><b>first</b></a> ()</li>
|
---|
67 | <li class=fn>virtual bool <a href="#last"><b>last</b></a> ()</li>
|
---|
68 | <li class=fn>bool <a href="#exec-2"><b>exec</b></a> ()</li>
|
---|
69 | <li class=fn>bool <a href="#prepare"><b>prepare</b></a> ( const QString & query )</li>
|
---|
70 | <li class=fn>void <a href="#bindValue-2"><b>bindValue</b></a> ( const QString & placeholder, const QVariant & val )</li>
|
---|
71 | <li class=fn>void <a href="#bindValue-3"><b>bindValue</b></a> ( int pos, const QVariant & val )</li>
|
---|
72 | <li class=fn>void <a href="#addBindValue-2"><b>addBindValue</b></a> ( const QVariant & val )</li>
|
---|
73 | <li class=fn>void <a href="#bindValue"><b>bindValue</b></a> ( const QString & placeholder, const QVariant & val, QSql::ParameterType type )</li>
|
---|
74 | <li class=fn>void <a href="#bindValue-4"><b>bindValue</b></a> ( int pos, const QVariant & val, QSql::ParameterType type )</li>
|
---|
75 | <li class=fn>void <a href="#addBindValue"><b>addBindValue</b></a> ( const QVariant & val, QSql::ParameterType type )</li>
|
---|
76 | <li class=fn>QVariant <a href="#boundValue"><b>boundValue</b></a> ( const QString & placeholder ) const</li>
|
---|
77 | <li class=fn>QVariant <a href="#boundValue-2"><b>boundValue</b></a> ( int pos ) const</li>
|
---|
78 | <li class=fn>QMap<QString, QVariant> <a href="#boundValues"><b>boundValues</b></a> () const</li>
|
---|
79 | <li class=fn>QString <a href="#executedQuery"><b>executedQuery</b></a> () const</li>
|
---|
80 | </ul>
|
---|
81 | <h2>Protected Members</h2>
|
---|
82 | <ul>
|
---|
83 | <li class=fn>virtual void <a href="#beforeSeek"><b>beforeSeek</b></a> ()</li>
|
---|
84 | <li class=fn>virtual void <a href="#afterSeek"><b>afterSeek</b></a> ()</li>
|
---|
85 | </ul>
|
---|
86 | <hr><a name="details"></a><h2>Detailed Description</h2>
|
---|
87 |
|
---|
88 |
|
---|
89 | The QSqlQuery class provides a means of executing and
|
---|
90 | manipulating SQL statements.
|
---|
91 | <p>
|
---|
92 |
|
---|
93 |
|
---|
94 | <p> QSqlQuery encapsulates the functionality involved in creating,
|
---|
95 | navigating and retrieving data from SQL queries which are executed
|
---|
96 | on a <a href="qsqldatabase.html">QSqlDatabase</a>. It can be used to execute DML (data
|
---|
97 | manipulation language) statements, e.g. <tt>SELECT</tt>, <tt>INSERT</tt>, <tt>UPDATE</tt> and <tt>DELETE</tt>, and also DDL (data definition language)
|
---|
98 | statements, e.g. <tt>CREATE TABLE</tt>. It can also be used to
|
---|
99 | execute database-specific commands which are not standard SQL
|
---|
100 | (e.g. <tt>SET DATESTYLE=ISO</tt> for PostgreSQL).
|
---|
101 | <p> Successfully executed SQL statements set the query's state to
|
---|
102 | active (<a href="#isActive">isActive</a>() returns TRUE); otherwise the query's state is
|
---|
103 | set to inactive. In either case, when executing a new SQL
|
---|
104 | statement, the query is positioned on an invalid record; an active
|
---|
105 | query must be navigated to a valid record (so that <a href="#isValid">isValid</a>()
|
---|
106 | returns TRUE) before values can be retrieved.
|
---|
107 | <p> Navigating records is performed with the following functions:
|
---|
108 | <p> <ul>
|
---|
109 | <li> <a href="#next">next</a>()
|
---|
110 | <li> <a href="#prev">prev</a>()
|
---|
111 | <li> <a href="#first">first</a>()
|
---|
112 | <li> <a href="#last">last</a>()
|
---|
113 | <li> <tt></tt><a href="#seek">seek</a>(int)
|
---|
114 | </ul>
|
---|
115 | <p> These functions allow the programmer to move forward, backward or
|
---|
116 | arbitrarily through the records returned by the query. If you only
|
---|
117 | need to move forward through the results, e.g. using <a href="#next">next</a>() or
|
---|
118 | using <a href="#seek">seek</a>() with a positive offset, you can use <a href="#setForwardOnly">setForwardOnly</a>()
|
---|
119 | and save a significant amount of memory overhead. Once an active
|
---|
120 | query is positioned on a valid record, data can be retrieved using
|
---|
121 | <a href="#value">value</a>(). All data is transferred from the SQL backend using
|
---|
122 | QVariants.
|
---|
123 | <p> For example:
|
---|
124 | <p> <pre>
|
---|
125 | QSqlQuery query( "SELECT name FROM customer" );
|
---|
126 | while ( query.<a href="#next">next</a>() ) {
|
---|
127 | <a href="qstring.html">QString</a> name = query.<a href="#value">value</a>(0).toString();
|
---|
128 | doSomething( name );
|
---|
129 | }
|
---|
130 | </pre>
|
---|
131 |
|
---|
132 | <p> To access the data returned by a query, use the value() method.
|
---|
133 | Each field in the data returned by a SELECT statement is accessed
|
---|
134 | by passing the field's position in the statement, starting from 0.
|
---|
135 | Information about the fields can be obtained via <a href="qsqldatabase.html#record">QSqlDatabase::record</a>().
|
---|
136 | For the sake of efficiency there are no functions to access a field
|
---|
137 | by name. (The <a href="qsqlcursor.html">QSqlCursor</a> class provides a higher-level interface
|
---|
138 | with field access by name and automatic SQL generation.)
|
---|
139 | <p> QSqlQuery supports prepared query execution and the binding of
|
---|
140 | parameter values to placeholders. Some databases don't support
|
---|
141 | these features, so for them Qt emulates the required
|
---|
142 | functionality. For example, the Oracle and ODBC drivers have
|
---|
143 | proper prepared query support, and Qt makes use of it; but for
|
---|
144 | databases that don't have this support, Qt implements the feature
|
---|
145 | itself, e.g. by replacing placeholders with actual values when a
|
---|
146 | query is executed. The exception is positional binding using named
|
---|
147 | placeholders, which requires that the database supports prepared
|
---|
148 | queries.
|
---|
149 | <p> Oracle databases identify placeholders by using a colon-name
|
---|
150 | syntax, e.g <tt>:name</tt>. ODBC simply uses <tt>?</tt> characters. Qt
|
---|
151 | supports both syntaxes (although you can't mix them in the same
|
---|
152 | query).
|
---|
153 | <p> Below we present the same example using each of the four different
|
---|
154 | binding approaches.
|
---|
155 | <p> <b>Named binding using named placeholders</b>
|
---|
156 | <pre>
|
---|
157 | QSqlQuery query;
|
---|
158 | query.<a href="#prepare">prepare</a>( "INSERT INTO atable (id, forename, surname) "
|
---|
159 | "VALUES (:id, :forename, :surname)" );
|
---|
160 | query.<a href="#bindValue">bindValue</a>( ":id", 1001 );
|
---|
161 | query.<a href="#bindValue">bindValue</a>( ":forename", "Bart" );
|
---|
162 | query.<a href="#bindValue">bindValue</a>( ":surname", "Simpson" );
|
---|
163 | query.<a href="#exec">exec</a>();
|
---|
164 | </pre>
|
---|
165 |
|
---|
166 | <p> <b>Positional binding using named placeholders</b>
|
---|
167 | <pre>
|
---|
168 | QSqlQuery query;
|
---|
169 | query.<a href="#prepare">prepare</a>( "INSERT INTO atable (id, forename, surname) "
|
---|
170 | "VALUES (:id, :forename, :surname)" );
|
---|
171 | query.<a href="#bindValue">bindValue</a>( 0, 1001 );
|
---|
172 | query.<a href="#bindValue">bindValue</a>( 1, "Bart" );
|
---|
173 | query.<a href="#bindValue">bindValue</a>( 2, "Simpson" );
|
---|
174 | query.<a href="#exec">exec</a>();
|
---|
175 | </pre>
|
---|
176 |
|
---|
177 | <b>Note:</b> Using positional binding with named placeholders will
|
---|
178 | only work if the database supports prepared queries. This can be
|
---|
179 | checked with <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>() using QSqlDriver::PreparedQueries
|
---|
180 | as argument for driver feature.
|
---|
181 | <p> <b>Binding values using positional placeholders #1</b>
|
---|
182 | <pre>
|
---|
183 | QSqlQuery query;
|
---|
184 | query.<a href="#prepare">prepare</a>( "INSERT INTO atable (id, forename, surname) "
|
---|
185 | "VALUES (?, ?, ?)" );
|
---|
186 | query.<a href="#bindValue">bindValue</a>( 0, 1001 );
|
---|
187 | query.<a href="#bindValue">bindValue</a>( 1, "Bart" );
|
---|
188 | query.<a href="#bindValue">bindValue</a>( 2, "Simpson" );
|
---|
189 | query.<a href="#exec">exec</a>();
|
---|
190 | </pre>
|
---|
191 |
|
---|
192 | <p> <b>Binding values using positional placeholders #2</b>
|
---|
193 | <pre>
|
---|
194 | query.prepare( "INSERT INTO atable (id, forename, surname) "
|
---|
195 | "VALUES (?, ?, ?)" );
|
---|
196 | query.addBindValue( 1001 );
|
---|
197 | query.addBindValue( "Bart" );
|
---|
198 | query.addBindValue( "Simpson" );
|
---|
199 | query.exec();
|
---|
200 | </pre>
|
---|
201 |
|
---|
202 | <p> <b>Binding values to a stored procedure</b>
|
---|
203 | This code calls a stored procedure called <tt>AsciiToInt()</tt>, passing
|
---|
204 | it a character through its in parameter, and taking its result in
|
---|
205 | the out parameter.
|
---|
206 | <pre>
|
---|
207 | QSqlQuery query;
|
---|
208 | query.<a href="#prepare">prepare</a>( "call AsciiToInt(?, ?)" );
|
---|
209 | query.<a href="#bindValue">bindValue</a>( 0, "A" );
|
---|
210 | query.<a href="#bindValue">bindValue</a>( 1, 0, QSql::Out );
|
---|
211 | query.<a href="#exec">exec</a>();
|
---|
212 | int i = query.<a href="#boundValue">boundValue</a>( 1 ).toInt(); // i is 65.
|
---|
213 | </pre>
|
---|
214 |
|
---|
215 | <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>.
|
---|
216 |
|
---|
217 | <hr><h2>Member Function Documentation</h2>
|
---|
218 | <h3 class=fn><a name="QSqlQuery"></a>QSqlQuery::QSqlQuery ( <a href="qsqlresult.html">QSqlResult</a> * r )
|
---|
219 | </h3>
|
---|
220 | Creates a QSqlQuery object which uses the <a href="qsqlresult.html">QSqlResult</a> <em>r</em> to
|
---|
221 | communicate with a database.
|
---|
222 |
|
---|
223 | <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 )
|
---|
224 | </h3>
|
---|
225 | Creates a QSqlQuery object using the SQL <em>query</em> and the database
|
---|
226 | <em>db</em>. If <em>db</em> is 0, (the default), the application's default
|
---|
227 | database is used. If <em>query</em> is not a null string, it will be
|
---|
228 | executed.
|
---|
229 | <p> <p>See also <a href="qsqldatabase.html">QSqlDatabase</a>.
|
---|
230 |
|
---|
231 | <h3 class=fn>explicit <a name="QSqlQuery-3"></a>QSqlQuery::QSqlQuery ( <a href="qsqldatabase.html">QSqlDatabase</a> * db )
|
---|
232 | </h3>
|
---|
233 | Creates a QSqlQuery object using the database <em>db</em>. If <em>db</em> is
|
---|
234 | 0, the application's default database is used.
|
---|
235 | <p> <p>See also <a href="qsqldatabase.html">QSqlDatabase</a>.
|
---|
236 |
|
---|
237 | <h3 class=fn><a name="QSqlQuery-4"></a>QSqlQuery::QSqlQuery ( const <a href="qsqlquery.html">QSqlQuery</a> & other )
|
---|
238 | </h3>
|
---|
239 | Constructs a copy of <em>other</em>.
|
---|
240 |
|
---|
241 | <h3 class=fn><a name="~QSqlQuery"></a>QSqlQuery::~QSqlQuery ()<tt> [virtual]</tt>
|
---|
242 | </h3>
|
---|
243 | Destroys the object and frees any allocated resources.
|
---|
244 |
|
---|
245 | <h3 class=fn>void <a name="addBindValue"></a>QSqlQuery::addBindValue ( const <a href="qvariant.html">QVariant</a> & val, <a href="qsql.html#ParameterType-enum">QSql::ParameterType</a> type )
|
---|
246 | </h3>
|
---|
247 | Adds the value <em>val</em> to the list of values when using positional
|
---|
248 | value binding. The order of the <a href="#addBindValue">addBindValue</a>() calls determines
|
---|
249 | which placeholder a value will be bound to in the prepared query.
|
---|
250 | If <em>type</em> is <a href="qsql.html#ParameterType-enum">QSql::Out</a> or <a href="qsql.html#ParameterType-enum">QSql::InOut</a>, the placeholder will
|
---|
251 | be overwritten with data from the database after the <a href="#exec">exec</a>() call.
|
---|
252 | <p> <p>See also <a href="#bindValue">bindValue</a>(), <a href="#prepare">prepare</a>(), and <a href="#exec">exec</a>().
|
---|
253 |
|
---|
254 | <h3 class=fn>void <a name="addBindValue-2"></a>QSqlQuery::addBindValue ( const <a href="qvariant.html">QVariant</a> & val )
|
---|
255 | </h3>
|
---|
256 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
257 | <p> Binds the placeholder with type <a href="qsql.html#ParameterType-enum">QSql::In</a>.
|
---|
258 |
|
---|
259 | <h3 class=fn>void <a name="afterSeek"></a>QSqlQuery::afterSeek ()<tt> [virtual protected]</tt>
|
---|
260 | </h3>
|
---|
261 | Protected virtual function called after the internal record
|
---|
262 | pointer is moved to a new record. The default implementation does
|
---|
263 | nothing.
|
---|
264 |
|
---|
265 | <h3 class=fn>int <a name="at"></a>QSqlQuery::at () const
|
---|
266 | </h3>
|
---|
267 | Returns the current internal position of the query. The first
|
---|
268 | record is at position zero. If the position is invalid, a
|
---|
269 | QSql::Location will be returned indicating the invalid position.
|
---|
270 | <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>().
|
---|
271 |
|
---|
272 | <p>Example: <a href="sql.html#x2169">sql/overview/navigating/main.cpp</a>.
|
---|
273 | <h3 class=fn>void <a name="beforeSeek"></a>QSqlQuery::beforeSeek ()<tt> [virtual protected]</tt>
|
---|
274 | </h3>
|
---|
275 | Protected virtual function called before the internal record
|
---|
276 | pointer is moved to a new record. The default implementation does
|
---|
277 | nothing.
|
---|
278 |
|
---|
279 | <h3 class=fn>void <a name="bindValue"></a>QSqlQuery::bindValue ( const <a href="qstring.html">QString</a> & placeholder, const <a href="qvariant.html">QVariant</a> & val, <a href="qsql.html#ParameterType-enum">QSql::ParameterType</a> type )
|
---|
280 | </h3>
|
---|
281 | Set the placeholder <em>placeholder</em> to be bound to value <em>val</em> in
|
---|
282 | the prepared statement. Note that the placeholder mark (e.g <tt>:</tt>)
|
---|
283 | must be included when specifying the placeholder name. If <em>type</em>
|
---|
284 | is <a href="qsql.html#ParameterType-enum">QSql::Out</a> or <a href="qsql.html#ParameterType-enum">QSql::InOut</a>, the placeholder will be
|
---|
285 | overwritten with data from the database after the <a href="#exec">exec</a>() call.
|
---|
286 | <p> <p>See also <a href="#addBindValue">addBindValue</a>(), <a href="#prepare">prepare</a>(), and <a href="#exec">exec</a>().
|
---|
287 |
|
---|
288 | <h3 class=fn>void <a name="bindValue-2"></a>QSqlQuery::bindValue ( const <a href="qstring.html">QString</a> & placeholder, const <a href="qvariant.html">QVariant</a> & val )
|
---|
289 | </h3>
|
---|
290 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
291 | <p> Binds the placeholder with type <a href="qsql.html#ParameterType-enum">QSql::In</a>.
|
---|
292 |
|
---|
293 | <h3 class=fn>void <a name="bindValue-3"></a>QSqlQuery::bindValue ( int pos, const <a href="qvariant.html">QVariant</a> & val )
|
---|
294 | </h3>
|
---|
295 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
296 | <p> Binds the placeholder at position <em>pos</em> with type <a href="qsql.html#ParameterType-enum">QSql::In</a>.
|
---|
297 |
|
---|
298 | <h3 class=fn>void <a name="bindValue-4"></a>QSqlQuery::bindValue ( int pos, const <a href="qvariant.html">QVariant</a> & val, <a href="qsql.html#ParameterType-enum">QSql::ParameterType</a> type )
|
---|
299 | </h3>
|
---|
300 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
301 | <p> Set the placeholder in position <em>pos</em> to be bound to value <em>val</em>
|
---|
302 | in the prepared statement. Field numbering starts at 0. If <em>type</em>
|
---|
303 | is <a href="qsql.html#ParameterType-enum">QSql::Out</a> or <a href="qsql.html#ParameterType-enum">QSql::InOut</a>, the placeholder will be
|
---|
304 | overwritten with data from the database after the <a href="#exec">exec</a>() call.
|
---|
305 | <p> <p>See also <a href="#addBindValue">addBindValue</a>(), <a href="#prepare">prepare</a>(), and <a href="#exec">exec</a>().
|
---|
306 |
|
---|
307 | <h3 class=fn><a href="qvariant.html">QVariant</a> <a name="boundValue"></a>QSqlQuery::boundValue ( const <a href="qstring.html">QString</a> & placeholder ) const
|
---|
308 | </h3>
|
---|
309 | Returns the value for the <em>placeholder</em>.
|
---|
310 |
|
---|
311 | <h3 class=fn><a href="qvariant.html">QVariant</a> <a name="boundValue-2"></a>QSqlQuery::boundValue ( int pos ) const
|
---|
312 | </h3>
|
---|
313 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
314 | <p> Returns the value for the placeholder at position <em>pos</em>.
|
---|
315 |
|
---|
316 | <h3 class=fn><a href="qmap.html">QMap</a><QString, QVariant> <a name="boundValues"></a>QSqlQuery::boundValues () const
|
---|
317 | </h3>
|
---|
318 | Returns a map of the bound values.
|
---|
319 | <p> The bound values can be examined in the following way:
|
---|
320 | <pre>
|
---|
321 | QSqlQuery query;
|
---|
322 | ...
|
---|
323 | // Examine the bound values - bound using named binding
|
---|
324 | QMap<QString, QVariant>::ConstIterator it;
|
---|
325 | <a href="qmap.html">QMap</a><QString, QVariant> vals = query.<a href="#boundValues">boundValues</a>();
|
---|
326 | for ( it = vals.<a href="qmap.html#begin">begin</a>(); it != vals.<a href="qmap.html#end">end</a>(); ++it )
|
---|
327 | <a href="qapplication.html#qWarning">qWarning</a>( "Placeholder: " + it.key() + ", Value: " + (*it).toString() );
|
---|
328 | ...
|
---|
329 |
|
---|
330 | // Examine the bound values - bound using positional binding
|
---|
331 | QValueList<QVariant>::ConstIterator it;
|
---|
332 | <a href="qvaluelist.html">QValueList</a><QVariant> list = query.<a href="#boundValues">boundValues</a>().values();
|
---|
333 | int i = 0;
|
---|
334 | for ( it = list.<a href="qvaluelist.html#begin">begin</a>(); it != list.<a href="qvaluelist.html#end">end</a>(); ++it )
|
---|
335 | <a href="qapplication.html#qWarning">qWarning</a>( "Placeholder pos: %d, Value: " + (*it).toString(), i++ );
|
---|
336 | ...
|
---|
337 |
|
---|
338 | </pre>
|
---|
339 |
|
---|
340 |
|
---|
341 | <h3 class=fn>const <a href="qsqldriver.html">QSqlDriver</a> * <a name="driver"></a>QSqlQuery::driver () const
|
---|
342 | </h3>
|
---|
343 | Returns the database driver associated with the query.
|
---|
344 |
|
---|
345 | <h3 class=fn>bool <a name="exec"></a>QSqlQuery::exec ( const <a href="qstring.html">QString</a> & query )<tt> [virtual]</tt>
|
---|
346 | </h3>
|
---|
347 | Executes the SQL in <em>query</em>. Returns TRUE and sets the query
|
---|
348 | state to active if the query was successful; otherwise returns
|
---|
349 | FALSE and sets the query state to inactive. The <em>query</em> string
|
---|
350 | must use syntax appropriate for the SQL database being queried,
|
---|
351 | for example, standard SQL.
|
---|
352 | <p> After the query is executed, the query is positioned on an <em>invalid</em> record, and must be navigated to a valid record before
|
---|
353 | data values can be retrieved, e.g. using <a href="#next">next</a>().
|
---|
354 | <p> Note that the last error for this query is reset when <a href="#exec">exec</a>() is
|
---|
355 | called.
|
---|
356 | <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>().
|
---|
357 |
|
---|
358 | <p>Examples: <a href="sql.html#x2157">sql/overview/basicbrowsing/main.cpp</a>, <a href="sql.html#x2161">sql/overview/basicbrowsing2/main.cpp</a>, <a href="sql.html#x2166">sql/overview/basicdatamanip/main.cpp</a>, and <a href="sql.html#x2154">sql/overview/connection.cpp</a>.
|
---|
359 | <h3 class=fn>bool <a name="exec-2"></a>QSqlQuery::exec ()
|
---|
360 | </h3>
|
---|
361 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
362 | <p> Executes a previously prepared SQL query. Returns TRUE if the
|
---|
363 | query executed successfully; otherwise returns FALSE.
|
---|
364 | <p> <p>See also <a href="#prepare">prepare</a>(), <a href="#bindValue">bindValue</a>(), and <a href="#addBindValue">addBindValue</a>().
|
---|
365 |
|
---|
366 | <h3 class=fn><a href="qstring.html">QString</a> <a name="executedQuery"></a>QSqlQuery::executedQuery () const
|
---|
367 | </h3>
|
---|
368 | Returns the last query that was executed.
|
---|
369 | <p> In most cases this function returns the same as <a href="#lastQuery">lastQuery</a>(). If a
|
---|
370 | prepared query with placeholders is executed on a DBMS that does
|
---|
371 | not support it, the preparation of this query is emulated. The
|
---|
372 | placeholders in the original query are replaced with their bound
|
---|
373 | values to form a new query. This function returns the modified
|
---|
374 | query. Useful for debugging purposes.
|
---|
375 | <p> <p>See also <a href="#lastQuery">lastQuery</a>().
|
---|
376 |
|
---|
377 | <h3 class=fn>bool <a name="first"></a>QSqlQuery::first ()<tt> [virtual]</tt>
|
---|
378 | </h3>
|
---|
379 | Retrieves the first record in the result, if available, and
|
---|
380 | positions the query on the retrieved record. Note that the result
|
---|
381 | must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before
|
---|
382 | calling this function or it will do nothing and return FALSE.
|
---|
383 | Returns TRUE if successful. If unsuccessful the query position is
|
---|
384 | set to an invalid position and FALSE is returned.
|
---|
385 | <p> <p>See also <a href="#next">next</a>(), <a href="#prev">prev</a>(), <a href="#last">last</a>(), <a href="#seek">seek</a>(), <a href="#at">at</a>(), <a href="#isActive">isActive</a>(), and <a href="#isValid">isValid</a>().
|
---|
386 |
|
---|
387 | <p>Example: <a href="sql.html#x2170">sql/overview/navigating/main.cpp</a>.
|
---|
388 | <h3 class=fn>bool <a name="isActive"></a>QSqlQuery::isActive () const
|
---|
389 | </h3>
|
---|
390 | Returns TRUE if the query is currently active; otherwise returns
|
---|
391 | FALSE.
|
---|
392 |
|
---|
393 | <p>Examples: <a href="sql.html#x2158">sql/overview/basicbrowsing/main.cpp</a>, <a href="sql.html#x2162">sql/overview/basicbrowsing2/main.cpp</a>, <a href="sql.html#x2167">sql/overview/basicdatamanip/main.cpp</a>, <a href="sql.html#x2171">sql/overview/navigating/main.cpp</a>, and <a href="sql.html#x2175">sql/overview/retrieve1/main.cpp</a>.
|
---|
394 | <h3 class=fn>bool <a name="isForwardOnly"></a>QSqlQuery::isForwardOnly () const
|
---|
395 | </h3>
|
---|
396 | Returns TRUE if you can only scroll <em>forward</em> through a result
|
---|
397 | set; otherwise returns FALSE.
|
---|
398 | <p> <p>See also <a href="#setForwardOnly">setForwardOnly</a>().
|
---|
399 |
|
---|
400 | <h3 class=fn>bool <a name="isNull"></a>QSqlQuery::isNull ( int field ) const
|
---|
401 | </h3>
|
---|
402 | Returns TRUE if the query is active and positioned on a valid
|
---|
403 | record and the <em>field</em> is NULL; otherwise returns FALSE. Note
|
---|
404 | that for some drivers <a href="#isNull">isNull</a>() will not return accurate
|
---|
405 | information until after an attempt is made to retrieve data.
|
---|
406 | <p> <p>See also <a href="#isActive">isActive</a>(), <a href="#isValid">isValid</a>(), and <a href="#value">value</a>().
|
---|
407 |
|
---|
408 | <h3 class=fn>bool <a name="isSelect"></a>QSqlQuery::isSelect () const
|
---|
409 | </h3>
|
---|
410 | Returns TRUE if the current query is a <tt>SELECT</tt> statement;
|
---|
411 | otherwise returns FALSE.
|
---|
412 |
|
---|
413 | <h3 class=fn>bool <a name="isValid"></a>QSqlQuery::isValid () const
|
---|
414 | </h3>
|
---|
415 | Returns TRUE if the query is currently positioned on a valid
|
---|
416 | record; otherwise returns FALSE.
|
---|
417 |
|
---|
418 | <h3 class=fn>bool <a name="last"></a>QSqlQuery::last ()<tt> [virtual]</tt>
|
---|
419 | </h3>
|
---|
420 | Retrieves the last record in the result, if available, and
|
---|
421 | positions the query on the retrieved record. Note that the result
|
---|
422 | must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before
|
---|
423 | calling this function or it will do nothing and return FALSE.
|
---|
424 | Returns TRUE if successful. If unsuccessful the query position is
|
---|
425 | set to an invalid position and FALSE is returned.
|
---|
426 | <p> <p>See also <a href="#next">next</a>(), <a href="#prev">prev</a>(), <a href="#first">first</a>(), <a href="#seek">seek</a>(), <a href="#at">at</a>(), <a href="#isActive">isActive</a>(), and <a href="#isValid">isValid</a>().
|
---|
427 |
|
---|
428 | <p>Example: <a href="sql.html#x2172">sql/overview/navigating/main.cpp</a>.
|
---|
429 | <h3 class=fn><a href="qsqlerror.html">QSqlError</a> <a name="lastError"></a>QSqlQuery::lastError () const
|
---|
430 | </h3>
|
---|
431 | Returns error information about the last error (if any) that
|
---|
432 | occurred.
|
---|
433 | <p> <p>See also <a href="qsqlerror.html">QSqlError</a>.
|
---|
434 |
|
---|
435 | <h3 class=fn><a href="qstring.html">QString</a> <a name="lastQuery"></a>QSqlQuery::lastQuery () const
|
---|
436 | </h3>
|
---|
437 | Returns the text of the current query being used, or <a href="qstring.html#QString-null">QString::null</a>
|
---|
438 | if there is no current query text.
|
---|
439 | <p> <p>See also <a href="#executedQuery">executedQuery</a>().
|
---|
440 |
|
---|
441 | <h3 class=fn>bool <a name="next"></a>QSqlQuery::next ()<tt> [virtual]</tt>
|
---|
442 | </h3>
|
---|
443 | Retrieves the next record in the result, if available, and
|
---|
444 | positions the query on the retrieved record. Note that the result
|
---|
445 | must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before
|
---|
446 | calling this function or it will do nothing and return FALSE.
|
---|
447 | <p> The following rules apply:
|
---|
448 | <p> <ul>
|
---|
449 | <li> If the result is currently located before the first
|
---|
450 | record, e.g. immediately after a query is executed, an attempt is
|
---|
451 | made to retrieve the first record.
|
---|
452 | <p> <li> If the result is currently located after the last record,
|
---|
453 | there is no change and FALSE is returned.
|
---|
454 | <p> <li> If the result is located somewhere in the middle, an attempt
|
---|
455 | is made to retrieve the next record.
|
---|
456 | </ul>
|
---|
457 | <p> If the record could not be retrieved, the result is positioned after
|
---|
458 | the last record and FALSE is returned. If the record is successfully
|
---|
459 | retrieved, TRUE is returned.
|
---|
460 | <p> <p>See also <a href="#prev">prev</a>(), <a href="#first">first</a>(), <a href="#last">last</a>(), <a href="#seek">seek</a>(), <a href="#at">at</a>(), <a href="#isActive">isActive</a>(), and <a href="#isValid">isValid</a>().
|
---|
461 |
|
---|
462 | <p>Examples: <a href="sql.html#x2159">sql/overview/basicbrowsing/main.cpp</a>, <a href="sql.html#x2163">sql/overview/basicbrowsing2/main.cpp</a>, <a href="sql.html#x2208">sql/overview/delete/main.cpp</a>, <a href="sql.html#x2183">sql/overview/order1/main.cpp</a>, <a href="sql.html#x2176">sql/overview/retrieve1/main.cpp</a>, <a href="sql.html#x2265">sql/overview/subclass4/main.cpp</a>, and <a href="sql.html#x2268">sql/overview/subclass5/main.cpp</a>.
|
---|
463 | <h3 class=fn>int <a name="numRowsAffected"></a>QSqlQuery::numRowsAffected () const
|
---|
464 | </h3>
|
---|
465 | Returns the number of rows affected by the result's SQL statement,
|
---|
466 | or -1 if it cannot be determined. Note that for <tt>SELECT</tt>
|
---|
467 | statements, the value is undefined; see <a href="#size">size</a>() instead. If the
|
---|
468 | query is not active (<a href="#isActive">isActive</a>() returns FALSE), -1 is returned.
|
---|
469 | <p> <p>See also <a href="#size">size</a>() and <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>().
|
---|
470 |
|
---|
471 | <p>Examples: <a href="sql.html#x2164">sql/overview/basicbrowsing2/main.cpp</a> and <a href="sql.html#x2168">sql/overview/basicdatamanip/main.cpp</a>.
|
---|
472 | <h3 class=fn><a href="qsqlquery.html">QSqlQuery</a> & <a name="operator-eq"></a>QSqlQuery::operator= ( const <a href="qsqlquery.html">QSqlQuery</a> & other )
|
---|
473 | </h3>
|
---|
474 | Assigns <em>other</em> to the query.
|
---|
475 |
|
---|
476 | <h3 class=fn>bool <a name="prepare"></a>QSqlQuery::prepare ( const <a href="qstring.html">QString</a> & query )
|
---|
477 | </h3>
|
---|
478 | Prepares the SQL query <em>query</em> for execution. The query may
|
---|
479 | contain placeholders for binding values. Both Oracle style
|
---|
480 | colon-name (e.g. <tt>:surname</tt>), and ODBC style (e.g. <tt>?</tt>)
|
---|
481 | placeholders are supported; but they cannot be mixed in the same
|
---|
482 | query. See the <a href="#details">Description</a> for examples.
|
---|
483 | <p> <p>See also <a href="#exec">exec</a>(), <a href="#bindValue">bindValue</a>(), and <a href="#addBindValue">addBindValue</a>().
|
---|
484 |
|
---|
485 | <h3 class=fn>bool <a name="prev"></a>QSqlQuery::prev ()<tt> [virtual]</tt>
|
---|
486 | </h3>
|
---|
487 | Retrieves the previous record in the result, if available, and
|
---|
488 | positions the query on the retrieved record. Note that the result
|
---|
489 | must be in an active state and <a href="#isSelect">isSelect</a>() must return TRUE before
|
---|
490 | calling this function or it will do nothing and return FALSE.
|
---|
491 | <p> The following rules apply:
|
---|
492 | <p> <ul>
|
---|
493 | <li> If the result is currently located before the first record,
|
---|
494 | there is no change and FALSE is returned.
|
---|
495 | <p> <li> If the result is currently located after the last record, an
|
---|
496 | attempt is made to retrieve the last record.
|
---|
497 | <p> <li> If the result is somewhere in the middle, an attempt is made
|
---|
498 | to retrieve the previous record.
|
---|
499 | </ul>
|
---|
500 | <p> If the record could not be retrieved, the result is positioned
|
---|
501 | before the first record and FALSE is returned. If the record is
|
---|
502 | successfully retrieved, TRUE is returned.
|
---|
503 | <p> <p>See also <a href="#next">next</a>(), <a href="#first">first</a>(), <a href="#last">last</a>(), <a href="#seek">seek</a>(), <a href="#at">at</a>(), <a href="#isActive">isActive</a>(), and <a href="#isValid">isValid</a>().
|
---|
504 |
|
---|
505 | <h3 class=fn>const <a href="qsqlresult.html">QSqlResult</a> * <a name="result"></a>QSqlQuery::result () const
|
---|
506 | </h3>
|
---|
507 | Returns the result associated with the query.
|
---|
508 |
|
---|
509 | <h3 class=fn>bool <a name="seek"></a>QSqlQuery::seek ( int i, bool relative = FALSE )<tt> [virtual]</tt>
|
---|
510 | </h3>
|
---|
511 | Retrieves the record at position (offset) <em>i</em>, if available, and
|
---|
512 | positions the query on the retrieved record. The first record is
|
---|
513 | at position 0. Note that the query must be in an active state and
|
---|
514 | <a href="#isSelect">isSelect</a>() must return TRUE before calling this function.
|
---|
515 | <p> If <em>relative</em> is FALSE (the default), the following rules apply:
|
---|
516 | <p> <ul>
|
---|
517 | <li> If <em>i</em> is negative, the result is positioned before the
|
---|
518 | first record and FALSE is returned.
|
---|
519 | <li> Otherwise, an attempt is made to move to the record at position
|
---|
520 | <em>i</em>. If the record at position <em>i</em> could not be retrieved, the
|
---|
521 | result is positioned after the last record and FALSE is returned. If
|
---|
522 | the record is successfully retrieved, TRUE is returned.
|
---|
523 | </ul>
|
---|
524 | <p> If <em>relative</em> is TRUE, the following rules apply:
|
---|
525 | <p> <ul>
|
---|
526 | <li> If the result is currently positioned before the first
|
---|
527 | record or on the first record, and <em>i</em> is negative, there is no
|
---|
528 | change, and FALSE is returned.
|
---|
529 | <li> If the result is currently located after the last record, and
|
---|
530 | <em>i</em> is positive, there is no change, and FALSE is returned.
|
---|
531 | <li> If the result is currently located somewhere in the middle,
|
---|
532 | and the relative offset <em>i</em> moves the result below zero, the
|
---|
533 | result is positioned before the first record and FALSE is
|
---|
534 | returned.
|
---|
535 | <li> Otherwise, an attempt is made to move to the record <em>i</em>
|
---|
536 | records ahead of the current record (or <em>i</em> records behind the
|
---|
537 | current record if <em>i</em> is negative). If the record at offset <em>i</em>
|
---|
538 | could not be retrieved, the result is positioned after the last
|
---|
539 | record if <em>i</em> >= 0, (or before the first record if <em>i</em> is
|
---|
540 | negative), and FALSE is returned. If the record is successfully
|
---|
541 | retrieved, TRUE is returned.
|
---|
542 | </ul>
|
---|
543 | <p> <p>See also <a href="#next">next</a>(), <a href="#prev">prev</a>(), <a href="#first">first</a>(), <a href="#last">last</a>(), <a href="#at">at</a>(), <a href="#isActive">isActive</a>(), and <a href="#isValid">isValid</a>().
|
---|
544 |
|
---|
545 | <p>Example: <a href="sql.html#x2173">sql/overview/navigating/main.cpp</a>.
|
---|
546 | <h3 class=fn>void <a name="setForwardOnly"></a>QSqlQuery::setForwardOnly ( bool forward )
|
---|
547 | </h3>
|
---|
548 | Sets forward only mode to <em>forward</em>. If forward is TRUE only
|
---|
549 | <a href="#next">next</a>(), and <a href="#seek">seek</a>() with positive values, are allowed for
|
---|
550 | navigating the results. Forward only mode needs far less memory
|
---|
551 | since results do not need to be cached.
|
---|
552 | <p> Forward only mode is off by default.
|
---|
553 | <p> Forward only mode cannot be used with data aware widgets like
|
---|
554 | <a href="qdatatable.html">QDataTable</a>, since they must to be able to scroll backward as well
|
---|
555 | as forward.
|
---|
556 | <p> <p>See also <a href="#isForwardOnly">isForwardOnly</a>(), <a href="#next">next</a>(), and <a href="#seek">seek</a>().
|
---|
557 |
|
---|
558 | <h3 class=fn>int <a name="size"></a>QSqlQuery::size () const
|
---|
559 | </h3>
|
---|
560 | Returns the size of the result, (number of rows returned), or -1
|
---|
561 | if the size cannot be determined or if the database does not
|
---|
562 | support reporting information about query sizes. Note that for
|
---|
563 | non-<tt>SELECT</tt> statements (<a href="#isSelect">isSelect</a>() returns FALSE), <a href="#size">size</a>() will
|
---|
564 | return -1. If the query is not active (<a href="#isActive">isActive</a>() returns FALSE),
|
---|
565 | -1 is returned.
|
---|
566 | <p> To determine the number of rows affected by a non-SELECT
|
---|
567 | statement, use <a href="#numRowsAffected">numRowsAffected</a>().
|
---|
568 | <p> <p>See also <a href="#isActive">isActive</a>(), <a href="#numRowsAffected">numRowsAffected</a>(), and <a href="qsqldriver.html#hasFeature">QSqlDriver::hasFeature</a>().
|
---|
569 |
|
---|
570 | <p>Example: <a href="sql.html#x2174">sql/overview/navigating/main.cpp</a>.
|
---|
571 | <h3 class=fn><a href="qvariant.html">QVariant</a> <a name="value"></a>QSqlQuery::value ( int i ) const<tt> [virtual]</tt>
|
---|
572 | </h3>
|
---|
573 | Returns the value of the <em>i</em>-th field in the query (zero based).
|
---|
574 | <p> The fields are numbered from left to right using the text of the
|
---|
575 | <tt>SELECT</tt> statement, e.g. in <tt>SELECT forename, surname FROM people</tt>,
|
---|
576 | field 0 is <tt>forename</tt> and field 1 is <tt>surname</tt>. Using <tt>SELECT *</tt>
|
---|
577 | is not recommended because the order of the fields in the query is
|
---|
578 | undefined.
|
---|
579 | <p> An invalid <a href="qvariant.html">QVariant</a> is returned if field <em>i</em> does not exist, if
|
---|
580 | the query is inactive, or if the query is positioned on an invalid
|
---|
581 | record.
|
---|
582 | <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>().
|
---|
583 |
|
---|
584 | <p>Examples: <a href="sql.html#x2160">sql/overview/basicbrowsing/main.cpp</a>, <a href="sql.html#x2165">sql/overview/basicbrowsing2/main.cpp</a>, <a href="sql.html#x2177">sql/overview/retrieve1/main.cpp</a>, <a href="sql.html#x2262">sql/overview/subclass3/main.cpp</a>, <a href="sql.html#x2266">sql/overview/subclass4/main.cpp</a>, <a href="sql.html#x2269">sql/overview/subclass5/main.cpp</a>, and <a href="sql.html#x2252">sql/overview/table4/main.cpp</a>.
|
---|
585 | <!-- eof -->
|
---|
586 | <hr><p>
|
---|
587 | This file is part of the <a href="index.html">Qt toolkit</a>.
|
---|
588 | Copyright © 1995-2007
|
---|
589 | <a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
|
---|
590 | <table width=100% cellspacing=0 border=0><tr>
|
---|
591 | <td>Copyright © 2007
|
---|
592 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
593 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
594 | </table></div></address></body>
|
---|
595 | </html>
|
---|