📄 qsqlcursor.3qt
字号:
.br double newprice = price * 1.05;.br buffer->setValue( "price", newprice );.br cur.update();.br }.fi.PPTo edit an existing database record, first move to the record you wish to update. Call primeUpdate() to get the pointer to the cursor's edit buffer. Then use this pointer to modify the values in the edit buffer. Finally, call update() to save the changes to the database. The values in the edit buffer will be used to locate the appropriate record when updating the database (see primaryIndex())..PPSimilarly, when deleting an existing database record, first move to the record you wish to delete. Then, call primeDelete() to get the pointer to the edit buffer. Finally, call del() to delete the record from the database. Again, the values in the edit buffer will be used to locate and delete the appropriate record..PPTo insert a new record, call primeInsert() to get the pointer to the edit buffer. Use this pointer to populate the edit buffer with new values and then insert() the record into the database..PPAfter calling insert(), update() or del(), the cursor is no longer positioned on a valid record and can no longer be navigated (isValid() return FALSE). The reason for this is that any changes made to the database will not be visible until select() is called to refresh the cursor. You can change this behavior by passing FALSE to insert(), update() or del() which will prevent the cursor from becoming invalid. The edits will still not be visible when navigating the cursor until select() is called..PPQSqlCursor contains virtual methods which allow editing behavior to be customized by subclasses. This allows custom cursors to be created that encapsulate the editing behavior of a database table for an entire application. For example, a cursor can be customized to always auto-number primary index fields, or provide fields with suitable default values, when inserting new records. QSqlCursor generates SQL statements which are sent to the database engine; you can control which fields are included in these statements using setGenerated()..PPNote that QSqlCursor does not inherit from QObject. This means that you are responsible for destroying instances of this class yourself. However if you create a QSqlCursor and use it in a QDataTable, QDataBrowser or a QDataView these classes will usually take ownership of the cursor and destroy it when they don't need it anymore. The documentation for QDataTable, QDataBrowser and QDataView explicitly states which calls take ownership of the cursor..PPSee also Database Classes..SS "Member Type Documentation".SH "QSqlCursor::Mode"This enum type describes how QSqlCursor operates on records in the database..TP\fCQSqlCursor::ReadOnly\fR - the cursor can only SELECT records from the database..TP\fCQSqlCursor::Insert\fR - the cursor can INSERT records into the database..TP\fCQSqlCursor::Update\fR - the cursor can UPDATE records in the database..TP\fCQSqlCursor::Delete\fR - the cursor can DELETE records from the database..TP\fCQSqlCursor::Writable\fR - the cursor can INSERT, UPDATE and DELETE records in the database..SH MEMBER FUNCTION DOCUMENTATION.SH "QSqlCursor::QSqlCursor ( const QString & name = QString::null, bool autopopulate = TRUE, QSqlDatabase * db = 0 )"Constructs a cursor on database \fIdb\fR using table or view \fIname\fR..PPIf \fIautopopulate\fR is TRUE (the default), the \fIname\fR of the cursor must correspond to an existing table or view name in the database so that field information can be automatically created. If the table or view does not exist, the cursor will not be functional..PPThe cursor is created with an initial mode of QSqlCursor::Writable (meaning that records can be inserted, updated or deleted using the cursor). If the cursor does not have a unique primary index, update and deletes cannot be performed..PPNote that \fIautopopulate\fR refers to populating the cursor with meta-data, e.g. the names of the table's fields, not with retrieving data. The select() function is used to populate the cursor with data..PPSee also setName() and setMode()..SH "QSqlCursor::QSqlCursor ( const QSqlCursor & other )"Constructs a copy of \fIother\fR..SH "QSqlCursor::~QSqlCursor ()"Destroys the object and frees any allocated resources..SH "void QSqlCursor::append ( const QSqlFieldInfo & fieldInfo )\fC [virtual]\fR"Append a copy of field \fIfieldInfo\fR to the end of the cursor. Note that all references to the cursor edit buffer become invalidated..SH "QVariant QSqlCursor::calculateField ( const QString & name )\fC [virtual protected]\fR"Protected virtual function which is called whenever a field needs to be calculated. If calculated fields are being used, derived classes must reimplement this function and return the appropriate value for field \fIname\fR. The default implementation returns an invalid QVariant..PPSee also setCalculated()..PPExamples:.)l sql/overview/subclass3/main.cpp and sql/overview/subclass4/main.cpp..SH "bool QSqlCursor::canDelete () const"Returns TRUE if the cursor will perform deletes; otherwise returns FALSE..PPSee also setMode()..SH "bool QSqlCursor::canInsert () const"Returns TRUE if the cursor will perform inserts; otherwise returns FALSE..PPSee also setMode()..SH "bool QSqlCursor::canUpdate () const"Returns TRUE if the cursor will perform updates; otherwise returns FALSE..PPSee also setMode()..SH "void QSqlCursor::clear ()\fC [virtual]\fR"Removes all fields from the cursor. Note that all references to the cursor edit buffer become invalidated..PPReimplemented from QSqlRecord..SH "int QSqlCursor::del ( bool invalidate = TRUE )\fC [virtual]\fR"Deletes a record from the database using the cursor's primary index and the contents of the cursor edit buffer. Returns the number of records which were deleted. For error information, use lastError()..PPOnly records which meet the filter criteria specified by the cursor's primary index are deleted. If the cursor does not contain a primary index, no delete is performed and 0 is returned. If \fIinvalidate\fR is TRUE (the default), the current cursor can no longer be navigated. A new select() call must be made before you can move to a valid record. For example:.PP.nf.br QSqlCursor cur( "prices" );.br cur.select( "id=999" );.br if ( cur.next() ) {.br cur.primeDelete();.br cur.del();.br }.fi.PPIn the above example, a cursor is created on the 'prices' table and positioned to the record to be deleted. First primeDelete() is called to populate the edit buffer with the current cursor values, e.g. with an id of 999, and then del() is called to actually delete the record from the database. Remember: all edit operations (insert(), update() and delete()) operate on the contents of the cursor edit buffer and not on the contents of the cursor itself..PPSee also primeDelete(), setMode(), and lastError()..PPExample: sql/overview/delete/main.cpp..SH "int QSqlCursor::del ( const QString & filter, bool invalidate = TRUE )\fC [virtual protected]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPDeletes the current cursor record from the database using the filter \fIfilter\fR. Only records which meet the filter criteria are deleted. Returns the number of records which were deleted. If \fIinvalidate\fR is TRUE (the default), the current cursor can no longer be navigated. A new select() call must be made before you can move to a valid record. For error information, use lastError()..PPThe \fIfilter\fR is an SQL \fCWHERE\fR clause, e.g. \fCid=500\fR..PPSee also setMode() and lastError()..SH "QSqlRecord * QSqlCursor::editBuffer ( bool copy = FALSE )\fC [virtual]\fR"Returns the current internal edit buffer. If \fIcopy\fR is TRUE (the default is FALSE), the current cursor field values are first copied into the edit buffer. The edit buffer is valid as long as the cursor remains valid. The cursor retains ownership of the returned pointer, so it must not be deleted or modified..PPSee also primeInsert(), primeUpdate(), and primeDelete()..SH "QString QSqlCursor::filter () const"Returns the current filter, or an empty string if there is no current filter..SH "QSqlIndex QSqlCursor::index ( const QStringList & fieldNames ) const\fC [virtual]\fR"Returns an index composed of \fIfieldNames\fR, all in ASCending order. Note that all field names must exist in the cursor, otherwise an empty index is returned..PPSee also QSqlIndex..PPExamples:.)l sql/overview/extract/main.cpp, sql/overview/order1/main.cpp, sql/overview/order2/main.cpp, and sql/overview/table3/main.cpp..SH "QSqlIndex QSqlCursor::index ( const QString & fieldName ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns an index based on \fIfieldName\fR..SH "QSqlIndex QSqlCursor::index ( const char * fieldName ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns an index based on \fIfieldName\fR..SH "void QSqlCursor::insert ( int pos, const QSqlFieldInfo & fieldInfo )\fC [virtual]\fR"Insert a copy of \fIfieldInfo\fR at position \fIpos\fR. If a field already exists at \fIpos\fR, it is removed. Note that all references to the cursor edit buffer become invalidated..PPExamples:.)l sql/overview/insert/main.cpp and sql/overview/insert2/main.cpp..SH "int QSqlCursor::insert ( bool invalidate = TRUE )\fC [virtual]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPInserts the current contents of the cursor's edit record buffer into the database, if the cursor allows inserts. Returns the number of rows affected by the insert. For error information, use lastError()..PPIf \fIinvalidate\fR is TRUE (the default), the cursor will no longer be positioned on a valid record and can no longer be navigated. A new select() call must be made before navigating to a valid record..PP.nf.br QSqlCursor cur( "prices" );.br QSqlRecord *buffer = cur.primeInsert();.br buffer->setValue( "id", 53981 );.br buffer->setValue( "name", "Thingy" );.br buffer->setValue( "price", 105.75 );.br cur.insert();.fi.PPIn the above example, a cursor is created on the 'prices' table and a pointer to the insert buffer is aquired using primeInsert(). Each field's value is set to the desired value and then insert() is called to insert the data into the database. Remember: all edit operations (insert(), update() and delete()) operate on the contents of the cursor edit buffer and not on the contents of the cursor itself..PPSee also setMode() and lastError()..SH "bool QSqlCursor::isCalculated ( const QString & name ) const"Returns TRUE if the field \fIname\fR exists and is calculated; otherwise returns FALSE..PPSee also setCalculated()..SH "bool QSqlCursor::isNull ( int i ) const"Returns TRUE if the field \fIi\fR is NULL or if there is no field at position \fIi\fR; otherwise returns FALSE..PPThis is the same as calling QSqlRecord::isNull( \fIi\fR ).SH "bool QSqlCursor::isNull ( const QString & name ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns TRUE if the field called \fIname\fR is NULL or if there is no field called \fIname\fR; otherwise returns FALSE..PPThis is the same as calling QSqlRecord::isNull( \fIname\fR ).SH "bool QSqlCursor::isReadOnly () const"Returns TRUE if the cursor is read-only; otherwise returns FALSE. The default is FALSE. Read-only cursors cannot be edited using insert(), update() or del()..PPSee also setMode()..SH "bool QSqlCursor::isTrimmed ( const QString & name ) const"Returns TRUE if the field \fIname\fR exists and is trimmed; otherwise returns FALSE..PPWhen a trimmed field of type string or cstring is read from the database any trailing (right-most) spaces are removed..PPSee also setTrimmed()..SH "int QSqlCursor::mode () const"Returns the current cursor mode..PPSee also setMode()..SH "QString QSqlCursor::name () const"Returns the name of the cursor..SH "QSqlCursor & QSqlCursor::operator= ( const QSqlCursor & other )"Sets the cursor equal to \fIother\fR..SH "QSqlIndex QSqlCursor::primaryIndex ( bool setFromCursor = TRUE ) const\fC [virtual]\fR"Returns the primary index associated with the cursor as defined in the database, or an empty index if there is no primary index. If \fIsetFromCursor\fR is TRUE (the default), the index fields are populated with the corresponding values in the cursor's current record..SH "QSqlRecord * QSqlCursor::primeDelete ()\fC [virtual]\fR"This function primes the edit buffer's field values for delete and returns the edit buffer. The default implementation copies the field values from the current cursor record into the edit buffer (therefore, this function is equivalent to calling editBuffer( TRUE ) ). The cursor retains ownership of the returned pointer, so it must not be deleted or modified..PPSee also editBuffer() and del()..PPExample: sql/overview/delete/main.cpp..SH "QSqlRecord * QSqlCursor::primeInsert ()\fC [virtual]\fR"This function primes the edit buffer's field values for insert and returns the edit buffer. The default implementation clears all field values in the edit buffer. The cursor retains ownership of the returned pointer, so it must not be deleted or modified..PPSee also editBuffer() and insert()..PPExamples:.)l sql/overview/insert/main.cpp, sql/overview/insert2/main.cpp, sql/overview/subclass5/main.cpp, and sql/sqltable/main.cpp..SH "QSqlRecord * QSqlCursor::primeUpdate ()\fC [virtual]\fR"This function primes the edit buffer's field values for update and returns the edit buffer. The default implementation copies the field values from the current cursor record into the edit buffer (therefore, this function is equivalent to calling editBuffer( TRUE ) ). The cursor retains ownership of the returned pointer, so it must not be deleted or modified..PPSee also editBuffer() and update()..PPExamples:.)l sql/overview/custom1/main.cpp, sql/overview/form1/main.cpp, and sql/overview/update/main.cpp..SH "void QSqlCursor::remove ( int pos )\fC [virtual]\fR"Removes the field at \fIpos\fR. If \fIpos\fR does not exist, nothing happens. Note that all references to the cursor edit buffer become invalidated..PPReimplemented from QSqlRecord..SH "bool QSqlCursor::select ( const QString & filter, const QSqlIndex & sort = QSqlIndex ( ) )\fC [virtual]\fR"Selects all fields in the cursor from the database matching the filter criteria \fIfilter\fR. The data is returned in the order specified by the index \fIsort\fR. Returns TRUE if the data was successfully selected; otherwise returns FALSE..PP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -