[190] | 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/qsqlcursor.cpp:122 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>QSqlCursor 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>QSqlCursor Class Reference<br><small>[<a href="sql.html">sql module</a>]</small></h1>
|
---|
| 33 |
|
---|
| 34 | <p>The QSqlCursor class provides browsing and editing of SQL
|
---|
| 35 | tables and views.
|
---|
| 36 | <a href="#details">More...</a>
|
---|
| 37 | <p><tt>#include <<a href="qsqlcursor-h.html">qsqlcursor.h</a>></tt>
|
---|
| 38 | <p>Inherits <a href="qsqlrecord.html">QSqlRecord</a> and <a href="qsqlquery.html">QSqlQuery</a>.
|
---|
| 39 | <p>Inherited by <a href="qsqlselectcursor.html">QSqlSelectCursor</a>.
|
---|
| 40 | <p><a href="qsqlcursor-members.html">List of all member functions.</a>
|
---|
| 41 | <h2>Public Members</h2>
|
---|
| 42 | <ul>
|
---|
| 43 | <li class=fn><a href="#QSqlCursor"><b>QSqlCursor</b></a> ( const QString & name = QString::null, bool autopopulate = TRUE, QSqlDatabase * db = 0 )</li>
|
---|
| 44 | <li class=fn><a href="#QSqlCursor-2"><b>QSqlCursor</b></a> ( const QSqlCursor & other )</li>
|
---|
| 45 | <li class=fn>QSqlCursor & <a href="#operator-eq"><b>operator=</b></a> ( const QSqlCursor & other )</li>
|
---|
| 46 | <li class=fn><a href="#~QSqlCursor"><b>~QSqlCursor</b></a> ()</li>
|
---|
| 47 | <li class=fn>enum <a href="#Mode-enum"><b>Mode</b></a> { ReadOnly = 0, Insert = 1, Update = 2, Delete = 4, Writable = 7 }</li>
|
---|
| 48 | <li class=fn>virtual QSqlIndex <a href="#primaryIndex"><b>primaryIndex</b></a> ( bool setFromCursor = TRUE ) const</li>
|
---|
| 49 | <li class=fn>virtual QSqlIndex <a href="#index"><b>index</b></a> ( const QStringList & fieldNames ) const</li>
|
---|
| 50 | <li class=fn>QSqlIndex <a href="#index-2"><b>index</b></a> ( const QString & fieldName ) const</li>
|
---|
| 51 | <li class=fn>QSqlIndex <a href="#index-3"><b>index</b></a> ( const char * fieldName ) const</li>
|
---|
| 52 | <li class=fn>virtual void <a href="#setPrimaryIndex"><b>setPrimaryIndex</b></a> ( const QSqlIndex & idx )</li>
|
---|
| 53 | <li class=fn>virtual void <a href="#append"><b>append</b></a> ( const QSqlFieldInfo & fieldInfo )</li>
|
---|
| 54 | <li class=fn>virtual void <a href="#insert"><b>insert</b></a> ( int pos, const QSqlFieldInfo & fieldInfo )</li>
|
---|
| 55 | <li class=fn>virtual void <a href="#remove"><b>remove</b></a> ( int pos )</li>
|
---|
| 56 | <li class=fn>virtual void <a href="#clear"><b>clear</b></a> ()</li>
|
---|
| 57 | <li class=fn>virtual void <a href="#setGenerated"><b>setGenerated</b></a> ( const QString & name, bool generated )</li>
|
---|
| 58 | <li class=fn>virtual void <a href="#setGenerated-2"><b>setGenerated</b></a> ( int i, bool generated )</li>
|
---|
| 59 | <li class=fn>virtual QSqlRecord * <a href="#editBuffer"><b>editBuffer</b></a> ( bool copy = FALSE )</li>
|
---|
| 60 | <li class=fn>virtual QSqlRecord * <a href="#primeInsert"><b>primeInsert</b></a> ()</li>
|
---|
| 61 | <li class=fn>virtual QSqlRecord * <a href="#primeUpdate"><b>primeUpdate</b></a> ()</li>
|
---|
| 62 | <li class=fn>virtual QSqlRecord * <a href="#primeDelete"><b>primeDelete</b></a> ()</li>
|
---|
| 63 | <li class=fn>virtual int <a href="#insert-2"><b>insert</b></a> ( bool invalidate = TRUE )</li>
|
---|
| 64 | <li class=fn>virtual int <a href="#update"><b>update</b></a> ( bool invalidate = TRUE )</li>
|
---|
| 65 | <li class=fn>virtual int <a href="#del"><b>del</b></a> ( bool invalidate = TRUE )</li>
|
---|
| 66 | <li class=fn>virtual void <a href="#setMode"><b>setMode</b></a> ( int mode )</li>
|
---|
| 67 | <li class=fn>int <a href="#mode"><b>mode</b></a> () const</li>
|
---|
| 68 | <li class=fn>virtual void <a href="#setCalculated"><b>setCalculated</b></a> ( const QString & name, bool calculated )</li>
|
---|
| 69 | <li class=fn>bool <a href="#isCalculated"><b>isCalculated</b></a> ( const QString & name ) const</li>
|
---|
| 70 | <li class=fn>virtual void <a href="#setTrimmed"><b>setTrimmed</b></a> ( const QString & name, bool trim )</li>
|
---|
| 71 | <li class=fn>bool <a href="#isTrimmed"><b>isTrimmed</b></a> ( const QString & name ) const</li>
|
---|
| 72 | <li class=fn>bool <a href="#isReadOnly"><b>isReadOnly</b></a> () const</li>
|
---|
| 73 | <li class=fn>bool <a href="#canInsert"><b>canInsert</b></a> () const</li>
|
---|
| 74 | <li class=fn>bool <a href="#canUpdate"><b>canUpdate</b></a> () const</li>
|
---|
| 75 | <li class=fn>bool <a href="#canDelete"><b>canDelete</b></a> () const</li>
|
---|
| 76 | <li class=fn>bool <a href="#select-2"><b>select</b></a> ()</li>
|
---|
| 77 | <li class=fn>bool <a href="#select-3"><b>select</b></a> ( const QSqlIndex & sort )</li>
|
---|
| 78 | <li class=fn>bool <a href="#select-4"><b>select</b></a> ( const QSqlIndex & filter, const QSqlIndex & sort )</li>
|
---|
| 79 | <li class=fn>virtual bool <a href="#select"><b>select</b></a> ( const QString & filter, const QSqlIndex & sort = QSqlIndex ( ) )</li>
|
---|
| 80 | <li class=fn>virtual void <a href="#setSort"><b>setSort</b></a> ( const QSqlIndex & sort )</li>
|
---|
| 81 | <li class=fn>QSqlIndex <a href="#sort"><b>sort</b></a> () const</li>
|
---|
| 82 | <li class=fn>virtual void <a href="#setFilter"><b>setFilter</b></a> ( const QString & filter )</li>
|
---|
| 83 | <li class=fn>QString <a href="#filter"><b>filter</b></a> () const</li>
|
---|
| 84 | <li class=fn>virtual void <a href="#setName"><b>setName</b></a> ( const QString & name, bool autopopulate = TRUE )</li>
|
---|
| 85 | <li class=fn>QString <a href="#name"><b>name</b></a> () const</li>
|
---|
| 86 | <li class=fn>bool <a href="#isNull"><b>isNull</b></a> ( int i ) const</li>
|
---|
| 87 | <li class=fn>bool <a href="#isNull-2"><b>isNull</b></a> ( const QString & name ) const</li>
|
---|
| 88 | </ul>
|
---|
| 89 | <h2>Protected Members</h2>
|
---|
| 90 | <ul>
|
---|
| 91 | <li class=fn>virtual QVariant <a href="#calculateField"><b>calculateField</b></a> ( const QString & name )</li>
|
---|
| 92 | <li class=fn>virtual int <a href="#update-2"><b>update</b></a> ( const QString & filter, bool invalidate = TRUE )</li>
|
---|
| 93 | <li class=fn>virtual int <a href="#del-2"><b>del</b></a> ( const QString & filter, bool invalidate = TRUE )</li>
|
---|
| 94 | <li class=fn>virtual QString <a href="#toString-3"><b>toString</b></a> ( const QString & prefix, QSqlField * field, const QString & fieldSep ) const</li>
|
---|
| 95 | <li class=fn>virtual QString <a href="#toString"><b>toString</b></a> ( QSqlRecord * rec, const QString & prefix, const QString & fieldSep, const QString & sep ) const</li>
|
---|
| 96 | <li class=fn>virtual QString <a href="#toString-4"><b>toString</b></a> ( const QSqlIndex & i, QSqlRecord * rec, const QString & prefix, const QString & fieldSep, const QString & sep ) const</li>
|
---|
| 97 | </ul>
|
---|
| 98 | <hr><a name="details"></a><h2>Detailed Description</h2>
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | The QSqlCursor class provides browsing and editing of SQL
|
---|
| 102 | tables and views.
|
---|
| 103 | <p>
|
---|
| 104 |
|
---|
| 105 | <p> A QSqlCursor is a database record (see <a href="qsqlrecord.html">QSqlRecord</a>) that
|
---|
| 106 | corresponds to a table or view within an SQL database (see <a href="qsqldatabase.html">QSqlDatabase</a>). There are two buffers in a cursor, one used for
|
---|
| 107 | browsing and one used for editing records. Each buffer contains a
|
---|
| 108 | list of fields which correspond to the fields in the table or
|
---|
| 109 | view.
|
---|
| 110 | <p> When positioned on a valid record, the browse buffer contains the
|
---|
| 111 | values of the current record's fields from the database. The edit
|
---|
| 112 | buffer is separate, and is used for editing existing records and
|
---|
| 113 | inserting new records.
|
---|
| 114 | <p> For browsing data, a cursor must first <a href="#select">select</a>() data from the
|
---|
| 115 | database. After a successful select() the cursor is active
|
---|
| 116 | (<a href="qsqlquery.html#isActive">isActive</a>() returns TRUE), but is initially not positioned on a
|
---|
| 117 | valid record (<a href="qsqlquery.html#isValid">isValid</a>() returns FALSE). To position the cursor on
|
---|
| 118 | a valid record, use one of the navigation functions, <a href="qsqlquery.html#next">next</a>(),
|
---|
| 119 | <a href="qsqlquery.html#prev">prev</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), or <a href="qsqlquery.html#seek">seek</a>(). Once positioned on a valid
|
---|
| 120 | record, data can be retrieved from the browse buffer using
|
---|
| 121 | <a href="qsqlquery.html#value">value</a>(). If a navigation function is not successful, it returns
|
---|
| 122 | FALSE, the cursor will no longer be positioned on a valid record
|
---|
| 123 | and the values returned by value() are undefined.
|
---|
| 124 | <p> For example:
|
---|
| 125 | <p>
|
---|
| 126 |
|
---|
| 127 | <pre> QSqlCursor cur( "staff" ); // Specify the table/view name
|
---|
| 128 | <a name="x2178"></a> cur.<a href="#select">select</a>(); // We'll retrieve every record
|
---|
| 129 | while ( cur.<a href="qsqlquery.html#next">next</a>() ) {
|
---|
| 130 | <a name="x2179"></a> <a href="qapplication.html#qDebug">qDebug</a>( cur.<a href="qsqlquery.html#value">value</a>( "id" ).toString() + ": " +
|
---|
| 131 | cur.<a href="qsqlquery.html#value">value</a>( "surname" ).toString() + " " +
|
---|
| 132 | cur.<a href="qsqlquery.html#value">value</a>( "salary" ).toString() );
|
---|
| 133 | }
|
---|
| 134 | </pre>
|
---|
| 135 | <p> In the above example, a cursor is created specifying a table or
|
---|
| 136 | view name in the database. Then, <a href="#select">select</a>() is called, which can be
|
---|
| 137 | optionally parameterised to filter and order the records
|
---|
| 138 | retrieved. Each record in the cursor is retrieved using <a href="qsqlquery.html#next">next</a>().
|
---|
| 139 | When next() returns FALSE, there are no more records to process,
|
---|
| 140 | and the loop terminates.
|
---|
| 141 | <p> For editing records (rows of data), a cursor contains a separate
|
---|
| 142 | edit buffer which is independent of the fields used when browsing.
|
---|
| 143 | The functions <a href="#insert">insert</a>(), <a href="#update">update</a>() and <a href="#del">del</a>() operate on the edit
|
---|
| 144 | buffer. This allows the cursor to be repositioned to other
|
---|
| 145 | records while simultaneously maintaining a separate buffer for
|
---|
| 146 | edits. You can get a pointer to the edit buffer using
|
---|
| 147 | <a href="#editBuffer">editBuffer</a>(). The <a href="#primeInsert">primeInsert</a>(), <a href="#primeUpdate">primeUpdate</a>() and <a href="#primeDelete">primeDelete</a>()
|
---|
| 148 | functions also return a pointer to the edit buffer and prepare it
|
---|
| 149 | for insert, update and delete respectively. Edit operations only
|
---|
| 150 | affect a single row at a time. Note that update() and del()
|
---|
| 151 | require that the table or view contain a <a href="#primaryIndex">primaryIndex</a>() to ensure
|
---|
| 152 | that edit operations affect a unique record within the database.
|
---|
| 153 | <p> For example:
|
---|
| 154 | <p>
|
---|
| 155 |
|
---|
| 156 | <pre> QSqlCursor cur( "prices" );
|
---|
| 157 | <a name="x2200"></a> cur.<a href="#select">select</a>( "id=202" );
|
---|
| 158 | if ( cur.<a href="qsqlquery.html#next">next</a>() ) {
|
---|
| 159 | <a name="x2199"></a> <a href="qsqlrecord.html">QSqlRecord</a> *buffer = cur.<a href="#primeUpdate">primeUpdate</a>();
|
---|
| 160 | <a name="x2204"></a> double price = buffer-><a href="qsqlrecord.html#value">value</a>( "price" ).toDouble();
|
---|
| 161 | double newprice = price * 1.05;
|
---|
| 162 | <a name="x2203"></a> buffer-><a href="qsqlrecord.html#setValue">setValue</a>( "price", newprice );
|
---|
| 163 | <a name="x2201"></a> cur.<a href="#update">update</a>();
|
---|
| 164 | }
|
---|
| 165 | </pre>
|
---|
| 166 | <p> To edit an existing database record, first move to the record you
|
---|
| 167 | wish to update. Call <a href="#primeUpdate">primeUpdate</a>() to get the pointer to the
|
---|
| 168 | cursor's edit buffer. Then use this pointer to modify the values
|
---|
| 169 | in the edit buffer. Finally, call <a href="#update">update</a>() to save the changes to
|
---|
| 170 | the database. The values in the edit buffer will be used to
|
---|
| 171 | locate the appropriate record when updating the database (see
|
---|
| 172 | <a href="#primaryIndex">primaryIndex</a>()).
|
---|
| 173 | <p> Similarly, when deleting an existing database record, first move
|
---|
| 174 | to the record you wish to delete. Then, call <a href="#primeDelete">primeDelete</a>() to get
|
---|
| 175 | the pointer to the edit buffer. Finally, call <a href="#del">del</a>() to delete the
|
---|
| 176 | record from the database. Again, the values in the edit buffer
|
---|
| 177 | will be used to locate and delete the appropriate record.
|
---|
| 178 | <p> To insert a new record, call <a href="#primeInsert">primeInsert</a>() to get the pointer to
|
---|
| 179 | the edit buffer. Use this pointer to populate the edit buffer
|
---|
| 180 | with new values and then <a href="#insert">insert</a>() the record into the database.
|
---|
| 181 | <p> After calling insert(), update() or del(), the cursor is no longer
|
---|
| 182 | positioned on a valid record and can no longer be navigated
|
---|
| 183 | (<a href="qsqlquery.html#isValid">isValid</a>() return FALSE). The reason for this is that any changes
|
---|
| 184 | made to the database will not be visible until <a href="#select">select</a>() is called
|
---|
| 185 | to refresh the cursor. You can change this behavior by passing
|
---|
| 186 | FALSE to insert(), <a href="#update">update</a>() or del() which will prevent the cursor
|
---|
| 187 | from becoming invalid. The edits will still not be visible when
|
---|
| 188 | navigating the cursor until select() is called.
|
---|
| 189 | <p> QSqlCursor contains virtual methods which allow editing behavior
|
---|
| 190 | to be customized by subclasses. This allows custom cursors to be
|
---|
| 191 | created that encapsulate the editing behavior of a database table
|
---|
| 192 | for an entire application. For example, a cursor can be customized
|
---|
| 193 | to always auto-number primary index fields, or provide fields with
|
---|
| 194 | suitable default values, when inserting new records. QSqlCursor
|
---|
| 195 | generates SQL statements which are sent to the database engine;
|
---|
| 196 | you can control which fields are included in these statements
|
---|
| 197 | using <a href="#setGenerated">setGenerated</a>().
|
---|
| 198 | <p> Note that QSqlCursor does not inherit from <a href="qobject.html">QObject</a>. This means
|
---|
| 199 | that you are responsible for destroying instances of this class
|
---|
| 200 | yourself. However if you create a QSqlCursor and use it in a
|
---|
| 201 | <a href="qdatatable.html">QDataTable</a>, <a href="qdatabrowser.html">QDataBrowser</a> or a <a href="qdataview.html">QDataView</a> these classes will
|
---|
| 202 | usually take ownership of the cursor and destroy it when they
|
---|
| 203 | don't need it anymore. The documentation for <a href="qdatatable.html">QDataTable</a>,
|
---|
| 204 | <a href="qdatabrowser.html">QDataBrowser</a> and <a href="qdataview.html">QDataView</a> explicitly states which calls take
|
---|
| 205 | ownership of the cursor.
|
---|
| 206 | <p>See also <a href="database.html">Database Classes</a>.
|
---|
| 207 |
|
---|
| 208 | <hr><h2>Member Type Documentation</h2>
|
---|
| 209 | <h3 class=fn><a name="Mode-enum"></a>QSqlCursor::Mode</h3>
|
---|
| 210 |
|
---|
| 211 | <p> This enum type describes how QSqlCursor operates on records in the
|
---|
| 212 | database.
|
---|
| 213 | <ul>
|
---|
| 214 | <li><tt>QSqlCursor::ReadOnly</tt> - the cursor can only SELECT records from the
|
---|
| 215 | database.
|
---|
| 216 | <li><tt>QSqlCursor::Insert</tt> - the cursor can INSERT records into the database.
|
---|
| 217 | <li><tt>QSqlCursor::Update</tt> - the cursor can UPDATE records in the database.
|
---|
| 218 | <li><tt>QSqlCursor::Delete</tt> - the cursor can DELETE records from the database.
|
---|
| 219 | <li><tt>QSqlCursor::Writable</tt> - the cursor can INSERT, UPDATE and DELETE records
|
---|
| 220 | in the database.
|
---|
| 221 | </ul>
|
---|
| 222 | <hr><h2>Member Function Documentation</h2>
|
---|
| 223 | <h3 class=fn><a name="QSqlCursor"></a>QSqlCursor::QSqlCursor ( const <a href="qstring.html">QString</a> & name = QString::null, bool autopopulate = TRUE, <a href="qsqldatabase.html">QSqlDatabase</a> * db = 0 )
|
---|
| 224 | </h3>
|
---|
| 225 | Constructs a cursor on database <em>db</em> using table or view <em>name</em>.
|
---|
| 226 | <p> If <em>autopopulate</em> is TRUE (the default), the <em>name</em> of the
|
---|
| 227 | cursor must correspond to an existing table or view name in the
|
---|
| 228 | database so that field information can be automatically created.
|
---|
| 229 | If the table or view does not exist, the cursor will not be
|
---|
| 230 | functional.
|
---|
| 231 | <p> The cursor is created with an initial mode of QSqlCursor::Writable
|
---|
| 232 | (meaning that records can be inserted, updated or deleted using
|
---|
| 233 | the cursor). If the cursor does not have a unique primary index,
|
---|
| 234 | update and deletes cannot be performed.
|
---|
| 235 | <p> Note that <em>autopopulate</em> refers to populating the cursor with
|
---|
| 236 | meta-data, e.g. the names of the table's fields, not with
|
---|
| 237 | retrieving data. The <a href="#select">select</a>() function is used to populate the
|
---|
| 238 | cursor with data.
|
---|
| 239 | <p> <p>See also <a href="#setName">setName</a>() and <a href="#setMode">setMode</a>().
|
---|
| 240 |
|
---|
| 241 | <h3 class=fn><a name="QSqlCursor-2"></a>QSqlCursor::QSqlCursor ( const <a href="qsqlcursor.html">QSqlCursor</a> & other )
|
---|
| 242 | </h3>
|
---|
| 243 | Constructs a copy of <em>other</em>.
|
---|
| 244 |
|
---|
| 245 | <h3 class=fn><a name="~QSqlCursor"></a>QSqlCursor::~QSqlCursor ()
|
---|
| 246 | </h3>
|
---|
| 247 | Destroys the object and frees any allocated resources.
|
---|
| 248 |
|
---|
| 249 | <h3 class=fn>void <a name="append"></a>QSqlCursor::append ( const <a href="qsqlfieldinfo.html">QSqlFieldInfo</a> & fieldInfo )<tt> [virtual]</tt>
|
---|
| 250 | </h3>
|
---|
| 251 | Append a copy of field <em>fieldInfo</em> to the end of the cursor. Note
|
---|
| 252 | that all references to the cursor edit buffer become invalidated.
|
---|
| 253 |
|
---|
| 254 | <h3 class=fn><a href="qvariant.html">QVariant</a> <a name="calculateField"></a>QSqlCursor::calculateField ( const <a href="qstring.html">QString</a> & name )<tt> [virtual protected]</tt>
|
---|
| 255 | </h3>
|
---|
| 256 | Protected virtual function which is called whenever a field needs
|
---|
| 257 | to be calculated. If calculated fields are being used, derived
|
---|
| 258 | classes must reimplement this function and return the appropriate
|
---|
| 259 | value for field <em>name</em>. The default implementation returns an
|
---|
| 260 | invalid <a href="qvariant.html">QVariant</a>.
|
---|
| 261 | <p> <p>See also <a href="#setCalculated">setCalculated</a>().
|
---|
| 262 |
|
---|
| 263 | <p>Examples: <a href="sql.html#x2259">sql/overview/subclass3/main.cpp</a> and <a href="sql.html#x2263">sql/overview/subclass4/main.cpp</a>.
|
---|
| 264 | <h3 class=fn>bool <a name="canDelete"></a>QSqlCursor::canDelete () const
|
---|
| 265 | </h3>
|
---|
| 266 | Returns TRUE if the cursor will perform deletes; otherwise returns
|
---|
| 267 | FALSE.
|
---|
| 268 | <p> <p>See also <a href="#setMode">setMode</a>().
|
---|
| 269 |
|
---|
| 270 | <h3 class=fn>bool <a name="canInsert"></a>QSqlCursor::canInsert () const
|
---|
| 271 | </h3>
|
---|
| 272 | Returns TRUE if the cursor will perform inserts; otherwise returns
|
---|
| 273 | FALSE.
|
---|
| 274 | <p> <p>See also <a href="#setMode">setMode</a>().
|
---|
| 275 |
|
---|
| 276 | <h3 class=fn>bool <a name="canUpdate"></a>QSqlCursor::canUpdate () const
|
---|
| 277 | </h3>
|
---|
| 278 | Returns TRUE if the cursor will perform updates; otherwise returns
|
---|
| 279 | FALSE.
|
---|
| 280 | <p> <p>See also <a href="#setMode">setMode</a>().
|
---|
| 281 |
|
---|
| 282 | <h3 class=fn>void <a name="clear"></a>QSqlCursor::clear ()<tt> [virtual]</tt>
|
---|
| 283 | </h3>
|
---|
| 284 | Removes all fields from the cursor. Note that all references to
|
---|
| 285 | the cursor edit buffer become invalidated.
|
---|
| 286 |
|
---|
| 287 | <p>Reimplemented from <a href="qsqlrecord.html#clear">QSqlRecord</a>.
|
---|
| 288 | <h3 class=fn>int <a name="del"></a>QSqlCursor::del ( bool invalidate = TRUE )<tt> [virtual]</tt>
|
---|
| 289 | </h3>
|
---|
| 290 | Deletes a record from the database using the cursor's primary
|
---|
| 291 | index and the contents of the cursor edit buffer. Returns the
|
---|
| 292 | number of records which were deleted.
|
---|
| 293 | For error information, use <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 294 | <p> Only records which meet the filter criteria specified by the
|
---|
| 295 | cursor's primary index are deleted. If the cursor does not contain
|
---|
| 296 | a primary index, no delete is performed and 0 is returned. If <em>invalidate</em> is TRUE (the default), the current cursor can no longer
|
---|
| 297 | be navigated. A new <a href="#select">select</a>() call must be made before you can move
|
---|
| 298 | to a valid record. For example:
|
---|
| 299 | <p>
|
---|
| 300 |
|
---|
| 301 | <pre> QSqlCursor cur( "prices" );
|
---|
| 302 | <a name="x2207"></a> cur.<a href="#select">select</a>( "id=999" );
|
---|
| 303 | <a name="x2208"></a> if ( cur.<a href="qsqlquery.html#next">next</a>() ) {
|
---|
| 304 | <a name="x2206"></a> cur.<a href="#primeDelete">primeDelete</a>();
|
---|
| 305 | <a name="x2205"></a> cur.<a href="#del">del</a>();
|
---|
| 306 | }
|
---|
| 307 | </pre>
|
---|
| 308 | <p> In the above example, a cursor is created on the 'prices' table
|
---|
| 309 | and positioned to the record to be deleted. First <a href="#primeDelete">primeDelete</a>() is
|
---|
| 310 | called to populate the edit buffer with the current cursor values,
|
---|
| 311 | e.g. with an id of 999, and then <a href="#del">del</a>() is called to actually
|
---|
| 312 | delete the record from the database. Remember: all edit operations
|
---|
| 313 | (<a href="#insert">insert</a>(), <a href="#update">update</a>() and delete()) operate on the contents of the
|
---|
| 314 | cursor edit buffer and not on the contents of the cursor itself.
|
---|
| 315 | <p> <p>See also <a href="#primeDelete">primeDelete</a>(), <a href="#setMode">setMode</a>(), and <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 316 |
|
---|
| 317 | <p>Example: <a href="sql.html#x2205">sql/overview/delete/main.cpp</a>.
|
---|
| 318 | <h3 class=fn>int <a name="del-2"></a>QSqlCursor::del ( const <a href="qstring.html">QString</a> & filter, bool invalidate = TRUE )<tt> [virtual protected]</tt>
|
---|
| 319 | </h3>
|
---|
| 320 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 321 | <p> Deletes the current cursor record from the database using the
|
---|
| 322 | filter <em>filter</em>. Only records which meet the filter criteria are
|
---|
| 323 | deleted. Returns the number of records which were deleted. If <em>invalidate</em> is TRUE (the default), the current cursor can no longer
|
---|
| 324 | be navigated. A new <a href="#select">select</a>() call must be made before you can move
|
---|
| 325 | to a valid record. For error information, use <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 326 | <p> The <em>filter</em> is an SQL <tt>WHERE</tt> clause, e.g. <tt>id=500</tt>.
|
---|
| 327 | <p> <p>See also <a href="#setMode">setMode</a>() and <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 328 |
|
---|
| 329 | <h3 class=fn><a href="qsqlrecord.html">QSqlRecord</a> * <a name="editBuffer"></a>QSqlCursor::editBuffer ( bool copy = FALSE )<tt> [virtual]</tt>
|
---|
| 330 | </h3>
|
---|
| 331 | Returns the current internal edit buffer. If <em>copy</em> is TRUE (the
|
---|
| 332 | default is FALSE), the current cursor field values are first
|
---|
| 333 | copied into the edit buffer. The edit buffer is valid as long as
|
---|
| 334 | the cursor remains valid. The cursor retains ownership of the
|
---|
| 335 | returned pointer, so it must not be deleted or modified.
|
---|
| 336 | <p> <p>See also <a href="#primeInsert">primeInsert</a>(), <a href="#primeUpdate">primeUpdate</a>(), and <a href="#primeDelete">primeDelete</a>().
|
---|
| 337 |
|
---|
| 338 | <h3 class=fn><a href="qstring.html">QString</a> <a name="filter"></a>QSqlCursor::filter () const
|
---|
| 339 | </h3>
|
---|
| 340 | Returns the current filter, or an empty string if there is no
|
---|
| 341 | current filter.
|
---|
| 342 |
|
---|
| 343 | <h3 class=fn><a href="qsqlindex.html">QSqlIndex</a> <a name="index"></a>QSqlCursor::index ( const <a href="qstringlist.html">QStringList</a> & fieldNames ) const<tt> [virtual]</tt>
|
---|
| 344 | </h3>
|
---|
| 345 | Returns an index composed of <em>fieldNames</em>, all in ASCending
|
---|
| 346 | order. Note that all field names must exist in the cursor,
|
---|
| 347 | otherwise an empty index is returned.
|
---|
| 348 | <p> <p>See also <a href="qsqlindex.html">QSqlIndex</a>.
|
---|
| 349 |
|
---|
| 350 | <p>Examples: <a href="sql.html#x2188">sql/overview/extract/main.cpp</a>, <a href="sql.html#x2181">sql/overview/order1/main.cpp</a>, <a href="sql.html#x2184">sql/overview/order2/main.cpp</a>, and <a href="sql.html#x2241">sql/overview/table3/main.cpp</a>.
|
---|
| 351 | <h3 class=fn><a href="qsqlindex.html">QSqlIndex</a> <a name="index-2"></a>QSqlCursor::index ( const <a href="qstring.html">QString</a> & fieldName ) const
|
---|
| 352 | </h3>
|
---|
| 353 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 354 | <p> Returns an index based on <em>fieldName</em>.
|
---|
| 355 |
|
---|
| 356 | <h3 class=fn><a href="qsqlindex.html">QSqlIndex</a> <a name="index-3"></a>QSqlCursor::index ( const char * fieldName ) const
|
---|
| 357 | </h3>
|
---|
| 358 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 359 | <p> Returns an index based on <em>fieldName</em>.
|
---|
| 360 |
|
---|
| 361 | <h3 class=fn>void <a name="insert"></a>QSqlCursor::insert ( int pos, const <a href="qsqlfieldinfo.html">QSqlFieldInfo</a> & fieldInfo )<tt> [virtual]</tt>
|
---|
| 362 | </h3>
|
---|
| 363 | Insert a copy of <em>fieldInfo</em> at position <em>pos</em>. If a field
|
---|
| 364 | already exists at <em>pos</em>, it is removed. Note that all references
|
---|
| 365 | to the cursor edit buffer become invalidated.
|
---|
| 366 |
|
---|
| 367 | <p>Examples: <a href="sql.html#x2194">sql/overview/insert/main.cpp</a> and <a href="qsqlcursor.html#x2446">sql/overview/insert2/main.cpp</a>.
|
---|
| 368 | <h3 class=fn>int <a name="insert-2"></a>QSqlCursor::insert ( bool invalidate = TRUE )<tt> [virtual]</tt>
|
---|
| 369 | </h3>
|
---|
| 370 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 371 | <p> Inserts the current contents of the cursor's edit record buffer
|
---|
| 372 | into the database, if the cursor allows inserts. Returns the
|
---|
| 373 | number of rows affected by the insert. For error information, use
|
---|
| 374 | <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 375 | <p> If <em>invalidate</em> is TRUE (the default), the cursor will no longer
|
---|
| 376 | be positioned on a valid record and can no longer be navigated. A
|
---|
| 377 | new <a href="#select">select</a>() call must be made before navigating to a valid
|
---|
| 378 | record.
|
---|
| 379 | <p>
|
---|
| 380 |
|
---|
| 381 | <pre> QSqlCursor cur( "prices" );
|
---|
| 382 | <a name="x2447"></a> <a href="qsqlrecord.html">QSqlRecord</a> *buffer = cur.<a href="#primeInsert">primeInsert</a>();
|
---|
| 383 | <a name="x2448"></a> buffer-><a href="qsqlrecord.html#setValue">setValue</a>( "id", 53981 );
|
---|
| 384 | buffer-><a href="qsqlrecord.html#setValue">setValue</a>( "name", "Thingy" );
|
---|
| 385 | buffer-><a href="qsqlrecord.html#setValue">setValue</a>( "price", 105.75 );
|
---|
| 386 | <a name="x2446"></a> cur.<a href="#insert">insert</a>();
|
---|
| 387 | </pre>
|
---|
| 388 | <p> In the above example, a cursor is created on the 'prices' table
|
---|
| 389 | and a pointer to the insert buffer is aquired using <a href="#primeInsert">primeInsert</a>().
|
---|
| 390 | Each field's value is set to the desired value and then <a href="#insert">insert</a>()
|
---|
| 391 | is called to insert the data into the database. Remember: all edit
|
---|
| 392 | operations (insert(), <a href="#update">update</a>() and delete()) operate on the
|
---|
| 393 | contents of the cursor edit buffer and not on the contents of the
|
---|
| 394 | cursor itself.
|
---|
| 395 | <p> <p>See also <a href="#setMode">setMode</a>() and <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 396 |
|
---|
| 397 | <h3 class=fn>bool <a name="isCalculated"></a>QSqlCursor::isCalculated ( const <a href="qstring.html">QString</a> & name ) const
|
---|
| 398 | </h3>
|
---|
| 399 | Returns TRUE if the field <em>name</em> exists and is calculated;
|
---|
| 400 | otherwise returns FALSE.
|
---|
| 401 | <p> <p>See also <a href="#setCalculated">setCalculated</a>().
|
---|
| 402 |
|
---|
| 403 | <h3 class=fn>bool <a name="isNull"></a>QSqlCursor::isNull ( int i ) const
|
---|
| 404 | </h3>
|
---|
| 405 | Returns TRUE if the field <em>i</em> is NULL or if there is no field at
|
---|
| 406 | position <em>i</em>; otherwise returns FALSE.
|
---|
| 407 | <p> This is the same as calling <a href="qsqlrecord.html#isNull">QSqlRecord::isNull</a>( <em>i</em> )
|
---|
| 408 |
|
---|
| 409 | <h3 class=fn>bool <a name="isNull-2"></a>QSqlCursor::isNull ( const <a href="qstring.html">QString</a> & name ) const
|
---|
| 410 | </h3>
|
---|
| 411 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 412 | <p> Returns TRUE if the field called <em>name</em> is NULL or if there is no
|
---|
| 413 | field called <em>name</em>; otherwise returns FALSE.
|
---|
| 414 | <p> This is the same as calling <a href="qsqlrecord.html#isNull">QSqlRecord::isNull</a>( <em>name</em> )
|
---|
| 415 |
|
---|
| 416 | <h3 class=fn>bool <a name="isReadOnly"></a>QSqlCursor::isReadOnly () const
|
---|
| 417 | </h3>
|
---|
| 418 | Returns TRUE if the cursor is read-only; otherwise returns FALSE.
|
---|
| 419 | The default is FALSE. Read-only cursors cannot be edited using
|
---|
| 420 | <a href="#insert">insert</a>(), <a href="#update">update</a>() or <a href="#del">del</a>().
|
---|
| 421 | <p> <p>See also <a href="#setMode">setMode</a>().
|
---|
| 422 |
|
---|
| 423 | <h3 class=fn>bool <a name="isTrimmed"></a>QSqlCursor::isTrimmed ( const <a href="qstring.html">QString</a> & name ) const
|
---|
| 424 | </h3>
|
---|
| 425 | Returns TRUE if the field <em>name</em> exists and is trimmed; otherwise
|
---|
| 426 | returns FALSE.
|
---|
| 427 | <p> When a trimmed field of type string or cstring is read from the
|
---|
| 428 | database any trailing (right-most) spaces are removed.
|
---|
| 429 | <p> <p>See also <a href="#setTrimmed">setTrimmed</a>().
|
---|
| 430 |
|
---|
| 431 | <h3 class=fn>int <a name="mode"></a>QSqlCursor::mode () const
|
---|
| 432 | </h3>
|
---|
| 433 | Returns the current cursor mode.
|
---|
| 434 | <p> <p>See also <a href="#setMode">setMode</a>().
|
---|
| 435 |
|
---|
| 436 | <h3 class=fn><a href="qstring.html">QString</a> <a name="name"></a>QSqlCursor::name () const
|
---|
| 437 | </h3>
|
---|
| 438 | Returns the name of the cursor.
|
---|
| 439 |
|
---|
| 440 | <h3 class=fn><a href="qsqlcursor.html">QSqlCursor</a> & <a name="operator-eq"></a>QSqlCursor::operator= ( const <a href="qsqlcursor.html">QSqlCursor</a> & other )
|
---|
| 441 | </h3>
|
---|
| 442 | Sets the cursor equal to <em>other</em>.
|
---|
| 443 |
|
---|
| 444 | <h3 class=fn><a href="qsqlindex.html">QSqlIndex</a> <a name="primaryIndex"></a>QSqlCursor::primaryIndex ( bool setFromCursor = TRUE ) const<tt> [virtual]</tt>
|
---|
| 445 | </h3>
|
---|
| 446 | Returns the primary index associated with the cursor as defined in
|
---|
| 447 | the database, or an empty index if there is no primary index. If
|
---|
| 448 | <em>setFromCursor</em> is TRUE (the default), the index fields are
|
---|
| 449 | populated with the corresponding values in the cursor's current
|
---|
| 450 | record.
|
---|
| 451 |
|
---|
| 452 | <h3 class=fn><a href="qsqlrecord.html">QSqlRecord</a> * <a name="primeDelete"></a>QSqlCursor::primeDelete ()<tt> [virtual]</tt>
|
---|
| 453 | </h3>
|
---|
| 454 | This function primes the edit buffer's field values for delete and
|
---|
| 455 | returns the edit buffer. The default implementation copies the
|
---|
| 456 | field values from the current cursor record into the edit buffer
|
---|
| 457 | (therefore, this function is equivalent to calling <a href="#editBuffer">editBuffer</a>(
|
---|
| 458 | TRUE ) ). The cursor retains ownership of the returned pointer, so
|
---|
| 459 | it must not be deleted or modified.
|
---|
| 460 | <p> <p>See also <a href="#editBuffer">editBuffer</a>() and <a href="#del">del</a>().
|
---|
| 461 |
|
---|
| 462 | <p>Example: <a href="sql.html#x2206">sql/overview/delete/main.cpp</a>.
|
---|
| 463 | <h3 class=fn><a href="qsqlrecord.html">QSqlRecord</a> * <a name="primeInsert"></a>QSqlCursor::primeInsert ()<tt> [virtual]</tt>
|
---|
| 464 | </h3>
|
---|
| 465 | This function primes the edit buffer's field values for insert and
|
---|
| 466 | returns the edit buffer. The default implementation clears all
|
---|
| 467 | field values in the edit buffer. The cursor retains ownership of
|
---|
| 468 | the returned pointer, so it must not be deleted or modified.
|
---|
| 469 | <p> <p>See also <a href="#editBuffer">editBuffer</a>() and <a href="#insert">insert</a>().
|
---|
| 470 |
|
---|
| 471 | <p>Examples: <a href="sql.html#x2195">sql/overview/insert/main.cpp</a>, <a href="qsqlcursor.html#x2447">sql/overview/insert2/main.cpp</a>, <a href="sql.html#x2267">sql/overview/subclass5/main.cpp</a>, and <a href="sqltable-example.html#x2453">sql/sqltable/main.cpp</a>.
|
---|
| 472 | <h3 class=fn><a href="qsqlrecord.html">QSqlRecord</a> * <a name="primeUpdate"></a>QSqlCursor::primeUpdate ()<tt> [virtual]</tt>
|
---|
| 473 | </h3>
|
---|
| 474 | This function primes the edit buffer's field values for update and
|
---|
| 475 | returns the edit buffer. The default implementation copies the
|
---|
| 476 | field values from the current cursor record into the edit buffer
|
---|
| 477 | (therefore, this function is equivalent to calling <a href="#editBuffer">editBuffer</a>(
|
---|
| 478 | TRUE ) ). The cursor retains ownership of the returned pointer, so
|
---|
| 479 | it must not be deleted or modified.
|
---|
| 480 | <p> <p>See also <a href="#editBuffer">editBuffer</a>() and <a href="#update">update</a>().
|
---|
| 481 |
|
---|
| 482 | <p>Examples: <a href="sql.html#x2236">sql/overview/custom1/main.cpp</a>, <a href="sql.html#x2223">sql/overview/form1/main.cpp</a>, and <a href="sql.html#x2199">sql/overview/update/main.cpp</a>.
|
---|
| 483 | <h3 class=fn>void <a name="remove"></a>QSqlCursor::remove ( int pos )<tt> [virtual]</tt>
|
---|
| 484 | </h3>
|
---|
| 485 | Removes the field at <em>pos</em>. If <em>pos</em> does not exist, nothing
|
---|
| 486 | happens. Note that all references to the cursor edit buffer become
|
---|
| 487 | invalidated.
|
---|
| 488 |
|
---|
| 489 | <p>Reimplemented from <a href="qsqlrecord.html#remove">QSqlRecord</a>.
|
---|
| 490 | <h3 class=fn>bool <a name="select"></a>QSqlCursor::select ( const <a href="qstring.html">QString</a> & filter, const <a href="qsqlindex.html">QSqlIndex</a> & sort = QSqlIndex ( ) )<tt> [virtual]</tt>
|
---|
| 491 | </h3>
|
---|
| 492 | Selects all fields in the cursor from the database matching the
|
---|
| 493 | filter criteria <em>filter</em>. The data is returned in the order
|
---|
| 494 | specified by the index <em>sort</em>. Returns TRUE if the data was
|
---|
| 495 | successfully selected; otherwise returns FALSE.
|
---|
| 496 | <p> The <em>filter</em> is a string containing a SQL <tt>WHERE</tt> clause but
|
---|
| 497 | without the 'WHERE' keyword. The cursor is initially positioned at
|
---|
| 498 | an invalid row after this function is called. To move to a valid
|
---|
| 499 | row, use <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#prev">prev</a>() or <a href="qsqlquery.html#next">next</a>().
|
---|
| 500 | <p> Example:
|
---|
| 501 | <pre>
|
---|
| 502 | QSqlCursor cur( "Employee" ); // Use the Employee table or view
|
---|
| 503 | cur.<a href="#select">select</a>( "deptno=10" ); // select all records in department 10
|
---|
| 504 | while( cur.<a href="qsqlquery.html#next">next</a>() ) {
|
---|
| 505 | ... // process data
|
---|
| 506 | }
|
---|
| 507 | ...
|
---|
| 508 | // select records in other departments, ordered by department number
|
---|
| 509 | cur.<a href="#select">select</a>( "deptno>10", cur.<a href="#index">index</a>( "deptno" ) );
|
---|
| 510 | ...
|
---|
| 511 | </pre>
|
---|
| 512 |
|
---|
| 513 | <p> The filter will apply to any subsequent <a href="#select">select</a>() calls that do not
|
---|
| 514 | explicitly specify another filter. Similarly the sort will apply
|
---|
| 515 | to any subsequent select() calls that do not explicitly specify
|
---|
| 516 | another sort.
|
---|
| 517 | <p> <pre>
|
---|
| 518 | QSqlCursor cur( "Employee" );
|
---|
| 519 | cur.<a href="#select">select</a>( "deptno=10" ); // select all records in department 10
|
---|
| 520 | while( cur.<a href="qsqlquery.html#next">next</a>() ) {
|
---|
| 521 | ... // process data
|
---|
| 522 | }
|
---|
| 523 | ...
|
---|
| 524 | cur.<a href="#select">select</a>(); // re-selects all records in department 10
|
---|
| 525 | ...
|
---|
| 526 | </pre>
|
---|
| 527 |
|
---|
| 528 | <p>
|
---|
| 529 | <p>Examples: <a href="sql.html#x2207">sql/overview/delete/main.cpp</a>, <a href="sql.html#x2189">sql/overview/extract/main.cpp</a>, <a href="sql.html#x2182">sql/overview/order1/main.cpp</a>, <a href="sql.html#x2185">sql/overview/order2/main.cpp</a>, <a href="sql.html#x2178">sql/overview/retrieve2/main.cpp</a>, <a href="sql.html#x2242">sql/overview/table3/main.cpp</a>, and <a href="sql.html#x2200">sql/overview/update/main.cpp</a>.
|
---|
| 530 | <h3 class=fn>bool <a name="select-2"></a>QSqlCursor::select ()
|
---|
| 531 | </h3>
|
---|
| 532 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 533 | <p> Selects all fields in the cursor from the database. The rows are
|
---|
| 534 | returned in the order specified by the last call to <a href="#setSort">setSort</a>() or
|
---|
| 535 | the last call to <a href="#select">select</a>() that specified a sort, whichever is the
|
---|
| 536 | most recent. If there is no current sort, the order in which the
|
---|
| 537 | rows are returned is undefined. The records are filtered according
|
---|
| 538 | to the filter specified by the last call to <a href="#setFilter">setFilter</a>() or the
|
---|
| 539 | last call to select() that specified a filter, whichever is the
|
---|
| 540 | most recent. If there is no current filter, all records are
|
---|
| 541 | returned. The cursor is initially positioned at an invalid row. To
|
---|
| 542 | move to a valid row, use <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#first">first</a>(), <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#prev">prev</a>() or
|
---|
| 543 | <a href="qsqlquery.html#next">next</a>().
|
---|
| 544 | <p> <p>See also <a href="#setSort">setSort</a>() and <a href="#setFilter">setFilter</a>().
|
---|
| 545 |
|
---|
| 546 | <h3 class=fn>bool <a name="select-3"></a>QSqlCursor::select ( const <a href="qsqlindex.html">QSqlIndex</a> & sort )
|
---|
| 547 | </h3>
|
---|
| 548 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 549 | <p> Selects all fields in the cursor from the database. The data is
|
---|
| 550 | returned in the order specified by the index <em>sort</em>. The records
|
---|
| 551 | are filtered according to the filter specified by the last call to
|
---|
| 552 | <a href="#setFilter">setFilter</a>() or the last call to <a href="#select">select</a>() that specified a filter,
|
---|
| 553 | whichever is the most recent. The cursor is initially positioned
|
---|
| 554 | at an invalid row. To move to a valid row, use <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#first">first</a>(),
|
---|
| 555 | <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#prev">prev</a>() or <a href="qsqlquery.html#next">next</a>().
|
---|
| 556 |
|
---|
| 557 | <h3 class=fn>bool <a name="select-4"></a>QSqlCursor::select ( const <a href="qsqlindex.html">QSqlIndex</a> & filter, const <a href="qsqlindex.html">QSqlIndex</a> & sort )
|
---|
| 558 | </h3>
|
---|
| 559 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 560 | <p> Selects all fields in the cursor matching the filter index <em>filter</em>. The data is returned in the order specified by the index
|
---|
| 561 | <em>sort</em>. The <em>filter</em> index works by constructing a WHERE clause
|
---|
| 562 | using the names of the fields from the <em>filter</em> and their values
|
---|
| 563 | from the current cursor record. The cursor is initially positioned
|
---|
| 564 | at an invalid row. To move to a valid row, use <a href="qsqlquery.html#seek">seek</a>(), <a href="qsqlquery.html#first">first</a>(),
|
---|
| 565 | <a href="qsqlquery.html#last">last</a>(), <a href="qsqlquery.html#prev">prev</a>() or <a href="qsqlquery.html#next">next</a>(). This function is useful, for example,
|
---|
| 566 | for retrieving data based upon a table's primary index:
|
---|
| 567 | <p> <pre>
|
---|
| 568 | QSqlCursor cur( "Employee" );
|
---|
| 569 | <a href="qsqlindex.html">QSqlIndex</a> pk = cur.<a href="#primaryIndex">primaryIndex</a>();
|
---|
| 570 | cur.<a href="qsqlrecord.html#setValue">setValue</a>( "id", 10 );
|
---|
| 571 | cur.<a href="#select">select</a>( pk, pk ); // generates "SELECT ... FROM Employee WHERE id=10 ORDER BY id"
|
---|
| 572 | ...
|
---|
| 573 | </pre>
|
---|
| 574 |
|
---|
| 575 | <p> In this example the <a href="qsqlindex.html">QSqlIndex</a>, pk, is used for two different
|
---|
| 576 | purposes. When used as the filter (first) argument, the field
|
---|
| 577 | names it contains are used to construct the WHERE clause, each set
|
---|
| 578 | to the current cursor value, <tt>WHERE id=10</tt>, in this case. When
|
---|
| 579 | used as the sort (second) argument the field names it contains are
|
---|
| 580 | used for the ORDER BY clause, <tt>ORDER BY id</tt> in this example.
|
---|
| 581 |
|
---|
| 582 | <h3 class=fn>void <a name="setCalculated"></a>QSqlCursor::setCalculated ( const <a href="qstring.html">QString</a> & name, bool calculated )<tt> [virtual]</tt>
|
---|
| 583 | </h3>
|
---|
| 584 | Sets field <em>name</em> to <em>calculated</em>. If the field <em>name</em> does not
|
---|
| 585 | exist, nothing happens. The value of a calculated field is set by
|
---|
| 586 | the <a href="#calculateField">calculateField</a>() virtual function which you must reimplement
|
---|
| 587 | (or the field value will be an invalid <a href="qvariant.html">QVariant</a>). Calculated
|
---|
| 588 | fields do not appear in generated SQL statements sent to the
|
---|
| 589 | database.
|
---|
| 590 | <p> <p>See also <a href="#calculateField">calculateField</a>() and <a href="qsqlrecord.html#setGenerated">QSqlRecord::setGenerated</a>().
|
---|
| 591 |
|
---|
| 592 | <h3 class=fn>void <a name="setFilter"></a>QSqlCursor::setFilter ( const <a href="qstring.html">QString</a> & filter )<tt> [virtual]</tt>
|
---|
| 593 | </h3>
|
---|
| 594 | Sets the current filter to <em>filter</em>. Note that no new records are
|
---|
| 595 | selected. To select new records, use <a href="#select">select</a>(). The <em>filter</em> will
|
---|
| 596 | apply to any subsequent select() calls that do not explicitly
|
---|
| 597 | specify a filter.
|
---|
| 598 | <p> The filter is a SQL <tt>WHERE</tt> clause without the keyword 'WHERE',
|
---|
| 599 | e.g. <tt>name='Dave'</tt> which will be processed by the DBMS.
|
---|
| 600 |
|
---|
| 601 | <h3 class=fn>void <a name="setGenerated"></a>QSqlCursor::setGenerated ( const <a href="qstring.html">QString</a> & name, bool generated )<tt> [virtual]</tt>
|
---|
| 602 | </h3>
|
---|
| 603 | Sets the generated flag for the field <em>name</em> to <em>generated</em>. If
|
---|
| 604 | the field does not exist, nothing happens. Only fields that have
|
---|
| 605 | <em>generated</em> set to TRUE are included in the SQL that is
|
---|
| 606 | generated by <a href="#insert">insert</a>(), <a href="#update">update</a>() or <a href="#del">del</a>().
|
---|
| 607 | <p> <p>See also <a href="qsqlrecord.html#isGenerated">isGenerated</a>().
|
---|
| 608 |
|
---|
| 609 | <p>Reimplemented from <a href="qsqlrecord.html#setGenerated">QSqlRecord</a>.
|
---|
| 610 | <h3 class=fn>void <a name="setGenerated-2"></a>QSqlCursor::setGenerated ( int i, bool generated )<tt> [virtual]</tt>
|
---|
| 611 | </h3>
|
---|
| 612 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 613 | <p> Sets the generated flag for the field <em>i</em> to <em>generated</em>.
|
---|
| 614 | <p> <p>See also <a href="qsqlrecord.html#isGenerated">isGenerated</a>().
|
---|
| 615 |
|
---|
| 616 | <p>Reimplemented from <a href="qsqlrecord.html#setGenerated-2">QSqlRecord</a>.
|
---|
| 617 | <h3 class=fn>void <a name="setMode"></a>QSqlCursor::setMode ( int mode )<tt> [virtual]</tt>
|
---|
| 618 | </h3>
|
---|
| 619 | Sets the cursor mode to <em>mode</em>. This value can be an OR'ed
|
---|
| 620 | combination of <a href="#Mode-enum">QSqlCursor::Mode</a> values. The default mode for a
|
---|
| 621 | cursor is <a href="#Mode-enum">QSqlCursor::Writable</a>.
|
---|
| 622 | <p> <pre>
|
---|
| 623 | QSqlCursor cur( "Employee" );
|
---|
| 624 | cur.<a href="#setMode">setMode</a>( QSqlCursor::<a href="#Mode-enum">Writable</a> ); // allow insert/update/delete
|
---|
| 625 | ...
|
---|
| 626 | cur.<a href="#setMode">setMode</a>( QSqlCursor::<a href="#Mode-enum">Insert</a> | QSqlCursor::<a href="#Mode-enum">Update</a> ); // allow inserts and updates only
|
---|
| 627 | ...
|
---|
| 628 | cur.<a href="#setMode">setMode</a>( QSqlCursor::<a href="#Mode-enum">ReadOnly</a> ); // no inserts/updates/deletes allowed
|
---|
| 629 |
|
---|
| 630 | </pre>
|
---|
| 631 |
|
---|
| 632 |
|
---|
| 633 | <h3 class=fn>void <a name="setName"></a>QSqlCursor::setName ( const <a href="qstring.html">QString</a> & name, bool autopopulate = TRUE )<tt> [virtual]</tt>
|
---|
| 634 | </h3>
|
---|
| 635 | Sets the name of the cursor to <em>name</em>. If <em>autopopulate</em> is TRUE
|
---|
| 636 | (the default), the <em>name</em> must correspond to a valid table or
|
---|
| 637 | view name in the database. Also, note that all references to the
|
---|
| 638 | cursor edit buffer become invalidated when fields are
|
---|
| 639 | auto-populated. See the QSqlCursor constructor documentation for
|
---|
| 640 | more information.
|
---|
| 641 |
|
---|
| 642 | <h3 class=fn>void <a name="setPrimaryIndex"></a>QSqlCursor::setPrimaryIndex ( const <a href="qsqlindex.html">QSqlIndex</a> & idx )<tt> [virtual]</tt>
|
---|
| 643 | </h3>
|
---|
| 644 | Sets the primary index associated with the cursor to the index <em>idx</em>. Note that this index must contain a field or set of fields
|
---|
| 645 | which identify a unique record within the underlying database
|
---|
| 646 | table or view so that <a href="#update">update</a>() and <a href="#del">del</a>() will execute as expected.
|
---|
| 647 | <p> <p>See also <a href="#update">update</a>() and <a href="#del">del</a>().
|
---|
| 648 |
|
---|
| 649 | <h3 class=fn>void <a name="setSort"></a>QSqlCursor::setSort ( const <a href="qsqlindex.html">QSqlIndex</a> & sort )<tt> [virtual]</tt>
|
---|
| 650 | </h3>
|
---|
| 651 | Sets the current sort to <em>sort</em>. Note that no new records are
|
---|
| 652 | selected. To select new records, use <a href="#select">select</a>(). The <em>sort</em> will
|
---|
| 653 | apply to any subsequent select() calls that do not explicitly
|
---|
| 654 | specify a sort.
|
---|
| 655 |
|
---|
| 656 | <h3 class=fn>void <a name="setTrimmed"></a>QSqlCursor::setTrimmed ( const <a href="qstring.html">QString</a> & name, bool trim )<tt> [virtual]</tt>
|
---|
| 657 | </h3>
|
---|
| 658 | Sets field <em>name</em>'s trimmed status to <em>trim</em>. If the field <em>name</em> does not exist, nothing happens.
|
---|
| 659 | <p> When a trimmed field of type string or cstring is read from the
|
---|
| 660 | database any trailing (right-most) spaces are removed.
|
---|
| 661 | <p> <p>See also <a href="#isTrimmed">isTrimmed</a>() and <a href="qvariant.html">QVariant</a>.
|
---|
| 662 |
|
---|
| 663 | <h3 class=fn><a href="qsqlindex.html">QSqlIndex</a> <a name="sort"></a>QSqlCursor::sort () const
|
---|
| 664 | </h3>
|
---|
| 665 | Returns the current sort, or an empty index if there is no current
|
---|
| 666 | sort.
|
---|
| 667 |
|
---|
| 668 | <h3 class=fn><a href="qstring.html">QString</a> <a name="toString"></a>QSqlCursor::toString ( <a href="qsqlrecord.html">QSqlRecord</a> * rec, const <a href="qstring.html">QString</a> & prefix, const <a href="qstring.html">QString</a> & fieldSep, const <a href="qstring.html">QString</a> & sep ) const<tt> [virtual protected]</tt>
|
---|
| 669 | </h3>
|
---|
| 670 | Returns a formatted string composed of all the fields in <em>rec</em>.
|
---|
| 671 | Each field is composed of the <em>prefix</em> (e.g. table or view name),
|
---|
| 672 | ".", the field name, the <em>fieldSep</em> and the field value. If the
|
---|
| 673 | <em>prefix</em> is empty then each field will begin with the field name.
|
---|
| 674 | The fields are then joined together separated by <em>sep</em>. Fields
|
---|
| 675 | where <a href="qsqlrecord.html#isGenerated">isGenerated</a>() returns FALSE are not included. This function
|
---|
| 676 | is useful for generating SQL statements.
|
---|
| 677 |
|
---|
| 678 | <h3 class=fn><a href="qstring.html">QString</a> <a name="toString-3"></a>QSqlCursor::toString ( const <a href="qstring.html">QString</a> & prefix, <a href="qsqlfield.html">QSqlField</a> * field, const <a href="qstring.html">QString</a> & fieldSep ) const<tt> [virtual protected]</tt>
|
---|
| 679 | </h3>
|
---|
| 680 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 681 | <p> Returns a formatted string composed of the <em>prefix</em> (e.g. table
|
---|
| 682 | or view name), ".", the <em>field</em> name, the <em>fieldSep</em> and the
|
---|
| 683 | field value. If the <em>prefix</em> is empty then the string will begin
|
---|
| 684 | with the <em>field</em> name. This function is useful for generating SQL
|
---|
| 685 | statements.
|
---|
| 686 |
|
---|
| 687 | <h3 class=fn><a href="qstring.html">QString</a> <a name="toString-4"></a>QSqlCursor::toString ( const <a href="qsqlindex.html">QSqlIndex</a> & i, <a href="qsqlrecord.html">QSqlRecord</a> * rec, const <a href="qstring.html">QString</a> & prefix, const <a href="qstring.html">QString</a> & fieldSep, const <a href="qstring.html">QString</a> & sep ) const<tt> [virtual protected]</tt>
|
---|
| 688 | </h3>
|
---|
| 689 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 690 | <p> Returns a formatted string composed of all the fields in the index
|
---|
| 691 | <em>i</em>. Each field is composed of the <em>prefix</em> (e.g. table or view
|
---|
| 692 | name), ".", the field name, the <em>fieldSep</em> and the field value.
|
---|
| 693 | If the <em>prefix</em> is empty then each field will begin with the field
|
---|
| 694 | name. The field values are taken from <em>rec</em>. The fields are then
|
---|
| 695 | joined together separated by <em>sep</em>. Fields where <a href="qsqlrecord.html#isGenerated">isGenerated</a>()
|
---|
| 696 | returns FALSE are ignored. This function is useful for generating
|
---|
| 697 | SQL statements.
|
---|
| 698 |
|
---|
| 699 | <h3 class=fn>int <a name="update"></a>QSqlCursor::update ( bool invalidate = TRUE )<tt> [virtual]</tt>
|
---|
| 700 | </h3>
|
---|
| 701 | Updates the database with the current contents of the edit buffer.
|
---|
| 702 | Returns the number of records which were updated.
|
---|
| 703 | For error information, use <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 704 | <p> Only records which meet the filter criteria specified by the
|
---|
| 705 | cursor's primary index are updated. If the cursor does not contain
|
---|
| 706 | a primary index, no update is performed and 0 is returned.
|
---|
| 707 | <p> If <em>invalidate</em> is TRUE (the default), the current cursor can no
|
---|
| 708 | longer be navigated. A new <a href="#select">select</a>() call must be made before you
|
---|
| 709 | can move to a valid record. For example:
|
---|
| 710 | <p>
|
---|
| 711 |
|
---|
| 712 | <pre> QSqlCursor cur( "prices" );
|
---|
| 713 | <a name="x2200"></a> cur.<a href="#select">select</a>( "id=202" );
|
---|
| 714 | if ( cur.<a href="qsqlquery.html#next">next</a>() ) {
|
---|
| 715 | <a name="x2199"></a> <a href="qsqlrecord.html">QSqlRecord</a> *buffer = cur.<a href="#primeUpdate">primeUpdate</a>();
|
---|
| 716 | <a name="x2204"></a> double price = buffer-><a href="qsqlrecord.html#value">value</a>( "price" ).toDouble();
|
---|
| 717 | double newprice = price * 1.05;
|
---|
| 718 | <a name="x2203"></a> buffer-><a href="qsqlrecord.html#setValue">setValue</a>( "price", newprice );
|
---|
| 719 | <a name="x2201"></a> cur.<a href="#update">update</a>();
|
---|
| 720 | }
|
---|
| 721 | </pre>
|
---|
| 722 | <p> In the above example, a cursor is created on the 'prices' table
|
---|
| 723 | and is positioned on the record to be updated. Then a pointer to
|
---|
| 724 | the cursor's edit buffer is acquired using <a href="#primeUpdate">primeUpdate</a>(). A new
|
---|
| 725 | value is calculated and placed into the edit buffer with the
|
---|
| 726 | <a href="qsqlrecord.html#setValue">setValue</a>() call. Finally, an <a href="#update">update</a>() call is made on the cursor
|
---|
| 727 | which uses the tables's primary index to update the record in the
|
---|
| 728 | database with the contents of the cursor's edit buffer. Remember:
|
---|
| 729 | all edit operations (<a href="#insert">insert</a>(), update() and delete()) operate on
|
---|
| 730 | the contents of the cursor edit buffer and not on the contents of
|
---|
| 731 | the cursor itself.
|
---|
| 732 | <p> Note that if the primary index does not uniquely distinguish
|
---|
| 733 | records the database may be changed into an inconsistent state.
|
---|
| 734 | <p> <p>See also <a href="#setMode">setMode</a>() and <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 735 |
|
---|
| 736 | <p>Example: <a href="sql.html#x2201">sql/overview/update/main.cpp</a>.
|
---|
| 737 | <h3 class=fn>int <a name="update-2"></a>QSqlCursor::update ( const <a href="qstring.html">QString</a> & filter, bool invalidate = TRUE )<tt> [virtual protected]</tt>
|
---|
| 738 | </h3>
|
---|
| 739 | This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
|
---|
| 740 | <p> Updates the database with the current contents of the cursor edit
|
---|
| 741 | buffer using the specified <em>filter</em>. Returns the number of
|
---|
| 742 | records which were updated.
|
---|
| 743 | For error information, use <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 744 | <p> Only records which meet the filter criteria are updated, otherwise
|
---|
| 745 | all records in the table are updated.
|
---|
| 746 | <p> If <em>invalidate</em> is TRUE (the default), the cursor can no longer
|
---|
| 747 | be navigated. A new <a href="#select">select</a>() call must be made before you can move
|
---|
| 748 | to a valid record.
|
---|
| 749 | <p> <p>See also <a href="#primeUpdate">primeUpdate</a>(), <a href="#setMode">setMode</a>(), and <a href="qsqlquery.html#lastError">lastError</a>().
|
---|
| 750 |
|
---|
| 751 | <!-- eof -->
|
---|
| 752 | <hr><p>
|
---|
| 753 | This file is part of the <a href="index.html">Qt toolkit</a>.
|
---|
| 754 | Copyright © 1995-2007
|
---|
| 755 | <a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
|
---|
| 756 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 757 | <td>Copyright © 2007
|
---|
| 758 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 759 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 760 | </table></div></address></body>
|
---|
| 761 | </html>
|
---|