File: qsqlquery.3qt

package info (click to toggle)
qt-copy 2%3A3.0.3-20020329-1woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 77,328 kB
  • ctags: 59,132
  • sloc: cpp: 413,292; ansic: 88,042; sh: 10,673; yacc: 2,874; xml: 2,310; makefile: 466; lex: 453; perl: 422; sql: 29; lisp: 15
file content (303 lines) | stat: -rw-r--r-- 15,136 bytes parent folder | download | duplicates (2)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
'\" t
.TH QSqlQuery 3qt "18 March 2002" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2001 Trolltech AS.  All rights reserved.  See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
QSqlQuery \- Means of executing and manipulating SQL statements
.SH SYNOPSIS
\fC#include <qsqlquery.h>\fR
.PP
Inherited by QSqlCursor.
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "\fBQSqlQuery\fR ( QSqlResult * r )"
.br
.ti -1c
.BI "\fBQSqlQuery\fR ( const QString & query = QString::null, QSqlDatabase * db = 0 )"
.br
.ti -1c
.BI "\fBQSqlQuery\fR ( const QSqlQuery & other )"
.br
.ti -1c
.BI "QSqlQuery & \fBoperator=\fR ( const QSqlQuery & other )"
.br
.ti -1c
.BI "virtual \fB~QSqlQuery\fR ()"
.br
.ti -1c
.BI "bool \fBisValid\fR () const"
.br
.ti -1c
.BI "bool \fBisActive\fR () const"
.br
.ti -1c
.BI "bool \fBisNull\fR ( int field ) const"
.br
.ti -1c
.BI "int \fBat\fR () const"
.br
.ti -1c
.BI "QString \fBlastQuery\fR () const"
.br
.ti -1c
.BI "int \fBnumRowsAffected\fR () const"
.br
.ti -1c
.BI "QSqlError \fBlastError\fR () const"
.br
.ti -1c
.BI "bool \fBisSelect\fR () const"
.br
.ti -1c
.BI "int \fBsize\fR () const"
.br
.ti -1c
.BI "const QSqlDriver * \fBdriver\fR () const"
.br
.ti -1c
.BI "const QSqlResult * \fBresult\fR () const"
.br
.ti -1c
.BI "virtual bool \fBexec\fR ( const QString & query )"
.br
.ti -1c
.BI "virtual QVariant \fBvalue\fR ( int i ) const"
.br
.ti -1c
.BI "virtual bool \fBseek\fR ( int i, bool relative = FALSE )"
.br
.ti -1c
.BI "virtual bool \fBnext\fR ()"
.br
.ti -1c
.BI "virtual bool \fBprev\fR ()"
.br
.ti -1c
.BI "virtual bool \fBfirst\fR ()"
.br
.ti -1c
.BI "virtual bool \fBlast\fR ()"
.br
.in -1c
.SS "Protected Members"
.in +1c
.ti -1c
.BI "virtual void \fBbeforeSeek\fR ()"
.br
.ti -1c
.BI "virtual void \fBafterSeek\fR ()"
.br
.in -1c
.SH DESCRIPTION
The QSqlQuery class provides a means of executing and manipulating SQL statements.
.PP
QSqlQuery encapsulates the functionality involved in creating, navigating and retrieving data from SQL queries which are executed on a QSqlDatabase. It can be used to execute DML (data manipulation language) statements, e.g. \fCSELECT\fR, \fCINSERT\fR, \fCUPDATE\fR and \fCDELETE\fR, and also DDL (data definition language) statements, e.g. \fCCREATE TABLE\fR. It can also be used to execute database-specific commands which are not standard SQL (e.g. \fCSET DATESTYLE=ISO\fR for PostgreSQL).
.PP
Successfully executed SQL statements set the query to an active state (isActive() returns TRUE) otherwise the query is set to an inactive state. In either case, when executing a new SQL statement, the query is positioned on an invalid record; an active query must be navigated to a valid record (so that isValid() returns TRUE) before values can be retrieved.
.PP
Navigating records is performed with the following functions:
.TP
next()
.TP
prev()
.TP
first()
.TP
last()
.TP
\fC\fRseek(int)
.PP
These functions allow the programmer to move forward, backward or arbitrarily through the records returned by the query. Once an active query is positioned on a valid record, data can be retrieved using value(). All data is transferred from the SQL backend using QVariants.
.PP
For example:
.PP
.nf
.br
    QSqlQuery query( "select name from customer" );
.br
    while ( query.next() ) {
.br
        QString name = query.value(0).toString();
.br
        doSomething( name );
.br
    }
.br
.fi
.PP
To access the data returned by a query, use the value() method. Each field in the data returned by a SELECT statement is accessed by passing the index number of the desired field, starting with 0. There are no methods to access a field by name to make sure the usage of QSqlQuery is as optimal as possible (see QSqlCursor for a more flexible interface for selecting data from a table or view in the database).
.PP
See also QSqlDatabase, QSqlCursor, QVariant and Database Classes.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QSqlQuery::QSqlQuery ( QSqlResult * r )"
Creates a QSqlQuery object which uses the QSqlResult \fIr\fR to communicate with a database.
.SH "QSqlQuery::QSqlQuery ( const QString & query = QString::null, QSqlDatabase * db = 0 )"
Creates a QSqlQuery object using the SQL \fIquery\fR and the database \fIdb\fR. If \fIdb\fR is 0, (the default), the application's default database is used.
.PP
See also QSqlDatabase.
.SH "QSqlQuery::QSqlQuery ( const QSqlQuery & other )"
Constructs a copy of \fIother\fR.
.SH "QSqlQuery::~QSqlQuery ()\fC [virtual]\fR"
Destroys the object and frees any allocated resources.
.SH "void QSqlQuery::afterSeek ()\fC [virtual protected]\fR"
Protected virtual function called after the internal record pointer is moved to a new record. The default implementation does nothing.
.SH "int QSqlQuery::at () const"
Returns the current internal position of the query. The first record is at position zero. If the position is invalid, a QSql::Location will be returned indicating the invalid position.
.PP
See also isValid().
.PP
Example: sql/overview/navigating/main.cpp.
.SH "void QSqlQuery::beforeSeek ()\fC [virtual protected]\fR"
Protected virtual function called before the internal record pointer is moved to a new record. The default implementation does nothing.
.SH "const QSqlDriver * QSqlQuery::driver () const"
Returns a pointer to the database driver associated with the query.
.SH "bool QSqlQuery::exec ( const QString & query )\fC [virtual]\fR"
Executes the SQL \fIquery\fR. Returns TRUE if the query was successful sets the query state to active, otherwise returns FALSE and the query becomes inactive. The \fIquery\fR string must use syntax appropriate for the SQL database being queried, for example, standard SQL.
.PP
After the query is executed, the query is positioned on an invalid record, and must be navigated to a valid record before data values can be retrieved.
.PP
See also isActive(), isValid(), next(), prev(), first(), last() and seek().
.PP
Examples:
.)l sql/overview/basicbrowsing/main.cpp, sql/overview/basicbrowsing2/main.cpp and sql/overview/basicdatamanip/main.cpp.
.SH "bool QSqlQuery::first ()\fC [virtual]\fR"
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 isSelect() 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.
.PP
Example: sql/overview/navigating/main.cpp.
.SH "bool QSqlQuery::isActive () const"
Returns TRUE if the query is currently active, otherwise returns FALSE.
.PP
Examples:
.)l sql/overview/basicbrowsing/main.cpp, sql/overview/basicbrowsing2/main.cpp, sql/overview/basicdatamanip/main.cpp, sql/overview/navigating/main.cpp and sql/overview/retrieve1/main.cpp.
.SH "bool QSqlQuery::isNull ( int field ) const"
Returns TRUE if \fIfield\fR is currently NULL, otherwise returns FALSE. The query must be active and positioned on a valid record before calling this function otherwise it returns FALSE. Note that, for some drivers, isNull() will not return accurate information until after an attempt is made to retrieve data.
.PP
See also isActive(), isValid() and value().
.SH "bool QSqlQuery::isSelect () const"
Returns TRUE if the current query is a \fCSELECT\fR statement, otherwise returns FALSE.
.SH "bool QSqlQuery::isValid () const"
Returns TRUE if the query is currently positioned on a valid record, otherwise returns FALSE.
.SH "bool QSqlQuery::last ()\fC [virtual]\fR"
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 isSelect() 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.
.PP
Example: sql/overview/navigating/main.cpp.
.SH "QSqlError QSqlQuery::lastError () const"
Returns error information about the last error (if any) that occurred.
.PP
See also QSqlError.
.SH "QString QSqlQuery::lastQuery () const"
Returns the text of the current query being used, or QString::null if there is no current query text.
.SH "bool QSqlQuery::next ()\fC [virtual]\fR"
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 isSelect() must return TRUE before calling this function or it will do nothing and return FALSE.
.PP
The following rules apply:
.TP
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.
.TP
If the result is currently located after the last record, there is no change and FALSE is returned.
.TP
If the result is located somewhere in the middle, an attempt is made to retrieve the next record.
.PP
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.
.PP
See also at() and isValid().
.PP
Examples:
.)l sql/overview/basicbrowsing/main.cpp, sql/overview/basicbrowsing2/main.cpp, sql/overview/retrieve1/main.cpp, sql/overview/subclass3/main.cpp, sql/overview/subclass4/main.cpp, sql/overview/subclass5/main.cpp and sql/sqltable/main.cpp.
.SH "int QSqlQuery::numRowsAffected () const"
Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. Note that for SELECT statements, this value will be the same as size(). If the query is not active (isActive() returns FALSE), -1 is returned.
.PP
See also size() and QSqlDriver::hasFeature().
.PP
Examples:
.)l sql/overview/basicbrowsing2/main.cpp and sql/overview/basicdatamanip/main.cpp.
.SH "QSqlQuery & QSqlQuery::operator= ( const QSqlQuery & other )"
Assigns \fIother\fR to the query.
.SH "bool QSqlQuery::prev ()\fC [virtual]\fR"
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 isSelect() must return TRUE before calling this function or it will do nothing and return FALSE.
.PP
The following rules apply:
.TP
If the result is currently located before the first record, there is no change and FALSE is returned.
.TP
If the result is currently located after the last record, an attempt is made to retrieve the last record.
.TP
If the result is somewhere in the middle, an attempt is made to retrieve the previous record.
.PP
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.
.PP
See also at().
.SH "const QSqlResult * QSqlQuery::result () const"
Returns a pointer to the result associated with the query.
.SH "bool QSqlQuery::seek ( int i, bool relative = FALSE )\fC [virtual]\fR"
Retrieves the record at position (or offset) \fIi\fR, if available, and positions the query on the retrieved record. The first record is at position zero. Note that the query must be in an active state and isSelect() must return TRUE before calling this function.
.PP
The following rules apply:
.PP
If \fIrelative\fR is FALSE (the default), the following rules apply:
.TP
If \fIi\fR is negative, the result is positioned before the first record and FALSE is returned.
.TP
Otherwise, an attempt is made to move to the record at position \fIi\fR. If the record at position \fIi\fR 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.
.PP
If \fIrelative\fR is TRUE, the following rules apply:
.TP
If the result is currently positioned before the first record or on the first record, and \fIi\fR is negative, there is no change, and FALSE is returned.
.TP
If the result is currently located after the last record, and \fIi\fR is positive, there is no change, and FALSE is returned.
.TP
If the result is currently located somewhere in the middle, and the relative offset \fIi\fR moves the result below zero, the result is positioned before the first record and FALSE is returned.
.TP
Otherwise, an attempt is made to move to the record \fIi\fR records ahead of the current record (or \fIi\fR records behind the current record if \fIi\fR is negative). If the record at offset \fIi\fR could not be retrieved, the result is positioned after the last record if \fIi\fR >= 0, (or before the first record if \fIi\fR is negative), and FALSE is returned. If the record is successfully retrieved, TRUE is returned.
.PP
Example: sql/overview/navigating/main.cpp.
.SH "int QSqlQuery::size () const"
Returns the size of the result, (number of rows returned), or -1 if the size cannot be determined or the database does not support reporting information about query sizes. Note that for non-SELECT statements (isSelect() returns FALSE), size() will return -1. If the query is not active (isActive() returns FALSE), -1 is returned.
.PP
To determine the number of rows affected by a non-SELECT statement, use numRowsAffected().
.PP
See also isActive(), numRowsAffected() and QSqlDriver::hasFeature().
.PP
Example: sql/overview/navigating/main.cpp.
.SH "QVariant QSqlQuery::value ( int i ) const\fC [virtual]\fR"
Returns the value of field \fIi\fR (zero based).
.PP
The fields are numbered from left to right using the text of the \fCSELECT\fR statement, e.g. in "select forename, surname from people", field 0 is forename and field 1 is surname. Using \fCSELECT *\fR is not recommended because the order of the fields in the query is undefined.
.PP
An invalid QVariant is returned if field \fIi\fR does not exist, if the query is inactive, or if the query is positioned on an invalid record.
.PP
See also prev(), next(), first(), last(), seek(), isActive() and isValid().
.PP
Examples:
.)l sql/overview/basicbrowsing/main.cpp, sql/overview/basicbrowsing2/main.cpp, sql/overview/retrieve1/main.cpp, sql/overview/subclass3/main.cpp, sql/overview/subclass4/main.cpp, sql/overview/subclass5/main.cpp and sql/sqltable/main.cpp.

.SH "SEE ALSO"
.BR http://doc.trolltech.com/qsqlquery.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2001 Trolltech AS, http://www.trolltech.com.  See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive Qt documentation is provided in HTML format; it is
located at $QTDIR/doc/html and can be read using Qt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech. 
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (qsqlquery.3qt) and the Qt
version (3.0.3).