Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The TQSqlCursor class provides browsing and editing of SQL tables and views. More...
#include <qsqlcursor.h>
Inherits TQSqlRecord and TQSqlQuery.
Inherited by TQSqlSelectCursor.
A TQSqlCursor is a database record (see TQSqlRecord) that corresponds to a table or view within an SQL database (see TQSqlDatabase). There are two buffers in a cursor, one used for browsing and one used for editing records. Each buffer contains a list of fields which correspond to the fields in the table or view.
When positioned on a valid record, the browse buffer contains the values of the current record's fields from the database. The edit buffer is separate, and is used for editing existing records and inserting new records.
For browsing data, a cursor must first select() data from the database. After a successful select() the cursor is active (isActive() returns TRUE), but is initially not positioned on a valid record (isValid() returns FALSE). To position the cursor on a valid record, use one of the navigation functions, next(), prev(), first(), last(), or seek(). Once positioned on a valid record, data can be retrieved from the browse buffer using value(). If a navigation function is not successful, it returns FALSE, the cursor will no longer be positioned on a valid record and the values returned by value() are undefined.
For example:
TQSqlCursor cur( "staff" ); // Specify the table/view name cur.select(); // We'll retrieve every record while ( cur.next() ) { qDebug( cur.value( "id" ).toString() + ": " + cur.value( "surname" ).toString() + " " + cur.value( "salary" ).toString() ); }
In the above example, a cursor is created specifying a table or view name in the database. Then, select() is called, which can be optionally parameterised to filter and order the records retrieved. Each record in the cursor is retrieved using next(). When next() returns FALSE, there are no more records to process, and the loop terminates.
For editing records (rows of data), a cursor contains a separate edit buffer which is independent of the fields used when browsing. The functions insert(), update() and del() operate on the edit buffer. This allows the cursor to be repositioned to other records while simultaneously maintaining a separate buffer for edits. You can get a pointer to the edit buffer using editBuffer(). The primeInsert(), primeUpdate() and primeDelete() functions also return a pointer to the edit buffer and prepare it for insert, update and delete respectively. Edit operations only affect a single row at a time. Note that update() and del() retquire that the table or view contain a primaryIndex() to ensure that edit operations affect a unique record within the database.
For example:
TQSqlCursor cur( "prices" ); cur.select( "id=202" ); if ( cur.next() ) { TQSqlRecord *buffer = cur.primeUpdate(); double price = buffer->value( "price" ).toDouble(); double newprice = price * 1.05; buffer->setValue( "price", newprice ); cur.update(); }
To 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()).
Similarly, 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.
To 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.
After 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.
TQSqlCursor 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. TQSqlCursor generates SQL statements which are sent to the database engine; you can control which fields are included in these statements using setGenerated().
Note that TQSqlCursor does not inherit from TQObject. This means that you are responsible for destroying instances of this class yourself. However if you create a TQSqlCursor and use it in a TQDataTable, TQDataBrowser or a TQDataView these classes will usually take ownership of the cursor and destroy it when they don't need it anymore. The documentation for TQDataTable, TQDataBrowser and TQDataView explicitly states which calls take ownership of the cursor.
See also Database Classes.
This enum type describes how TQSqlCursor operates on records in the database.
If autopopulate is TRUE (the default), the name 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.
The cursor is created with an initial mode of TQSqlCursor::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.
Note that autopopulate 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.
See also setName() and setMode().
See also setCalculated().
Examples: sql/overview/subclass3/main.cpp and sql/overview/subclass4/main.cpp.
See also setMode().
See also setMode().
See also setMode().
Reimplemented from TQSqlRecord.
Only 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 invalidate 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:
TQSqlCursor cur( "prices" ); cur.select( "id=999" ); if ( cur.next() ) { cur.primeDelete(); cur.del(); }
In 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.
See also primeDelete(), setMode(), and lastError().
Example: sql/overview/delete/main.cpp.
Deletes the current cursor record from the database using the filter filter. Only records which meet the filter criteria are deleted. Returns the number of records which were deleted. If invalidate 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().
The filter is an SQL WHERE clause, e.g. id=500.
See also setMode() and lastError().
See also primeInsert(), primeUpdate(), and primeDelete().
See also TQSqlIndex.
Examples: sql/overview/extract/main.cpp, sql/overview/order1/main.cpp, sql/overview/order2/main.cpp, and sql/overview/table3/main.cpp.
Returns an index based on fieldName.
Returns an index based on fieldName.
Examples: sql/overview/insert/main.cpp and sql/overview/insert2/main.cpp.
Inserts 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().
If invalidate 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.
TQSqlCursor cur( "prices" ); TQSqlRecord *buffer = cur.primeInsert(); buffer->setValue( "id", 53981 ); buffer->setValue( "name", "Thingy" ); buffer->setValue( "price", 105.75 ); cur.insert();
In the above example, a cursor is created on the 'prices' table and a pointer to the insert buffer is atquired 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.
See also setMode() and lastError().
See also setCalculated().
This is the same as calling TQSqlRecord::isNull( i )
Returns TRUE if the field called name is NULL or if there is no field called name; otherwise returns FALSE.
This is the same as calling TQSqlRecord::isNull( name )
See also setMode().
When a trimmed field of type string or cstring is read from the database any trailing (right-most) spaces are removed.
See also setTrimmed().
See also setMode().
See also editBuffer() and del().
Example: sql/overview/delete/main.cpp.
See also editBuffer() and insert().
Examples: sql/overview/insert/main.cpp, sql/overview/insert2/main.cpp, sql/overview/subclass5/main.cpp, and sql/sqltable/main.cpp.
See also editBuffer() and update().
Examples: sql/overview/custom1/main.cpp, sql/overview/form1/main.cpp, and sql/overview/update/main.cpp.
Reimplemented from TQSqlRecord.
The filter is a string containing a SQL WHERE clause but without the 'WHERE' keyword. The cursor is initially positioned at an invalid row after this function is called. To move to a valid row, use seek(), first(), last(), prev() or next().
Example:
TQSqlCursor cur( "Employee" ); // Use the Employee table or view cur.select( "deptno=10" ); // select all records in department 10 while( cur.next() ) { ... // process data } ... // select records in other departments, ordered by department number cur.select( "deptno>10", cur.index( "deptno" ) ); ...
The filter will apply to any subsequent select() calls that do not explicitly specify another filter. Similarly the sort will apply to any subsequent select() calls that do not explicitly specify another sort.
TQSqlCursor cur( "Employee" ); cur.select( "deptno=10" ); // select all records in department 10 while( cur.next() ) { ... // process data } ... cur.select(); // re-selects all records in department 10 ...
Examples: sql/overview/delete/main.cpp, sql/overview/extract/main.cpp, sql/overview/order1/main.cpp, sql/overview/order2/main.cpp, sql/overview/retrieve2/main.cpp, sql/overview/table3/main.cpp, and sql/overview/update/main.cpp.
Selects all fields in the cursor from the database. The rows are returned in the order specified by the last call to setSort() or the last call to select() that specified a sort, whichever is the most recent. If there is no current sort, the order in which the rows are returned is undefined. The records are filtered according to the filter specified by the last call to setFilter() or the last call to select() that specified a filter, whichever is the most recent. If there is no current filter, all records are returned. The cursor is initially positioned at an invalid row. To move to a valid row, use seek(), first(), last(), prev() or next().
See also setSort() and setFilter().
Selects all fields in the cursor from the database. The data is returned in the order specified by the index sort. The records are filtered according to the filter specified by the last call to setFilter() or the last call to select() that specified a filter, whichever is the most recent. The cursor is initially positioned at an invalid row. To move to a valid row, use seek(), first(), last(), prev() or next().
Selects all fields in the cursor matching the filter index filter. The data is returned in the order specified by the index sort. The filter index works by constructing a WHERE clause using the names of the fields from the filter and their values from the current cursor record. The cursor is initially positioned at an invalid row. To move to a valid row, use seek(), first(), last(), prev() or next(). This function is useful, for example, for retrieving data based upon a table's primary index:
TQSqlCursor cur( "Employee" ); TQSqlIndex pk = cur.primaryIndex(); cur.setValue( "id", 10 ); cur.select( pk, pk ); // generates "SELECT ... FROM Employee WHERE id=10 ORDER BY id" ...
In this example the TQSqlIndex, pk, is used for two different purposes. When used as the filter (first) argument, the field names it contains are used to construct the WHERE clause, each set to the current cursor value, WHERE id=10, in this case. When used as the sort (second) argument the field names it contains are used for the ORDER BY clause, ORDER BY id in this example.
See also calculateField() and TQSqlRecord::setGenerated().
The filter is a SQL WHERE clause without the keyword 'WHERE', e.g. name='Dave' which will be processed by the DBMS.
See also isGenerated().
Reimplemented from TQSqlRecord.
Sets the generated flag for the field i to generated.
See also isGenerated().
Reimplemented from TQSqlRecord.
TQSqlCursor cur( "Employee" ); cur.setMode( TQSqlCursor::Writable ); // allow insert/update/delete ... cur.setMode( TQSqlCursor::Insert | TQSqlCursor::Update ); // allow inserts and updates only ... cur.setMode( TQSqlCursor::ReadOnly ); // no inserts/updates/deletes allowed
When a trimmed field of type string or cstring is read from the database any trailing (right-most) spaces are removed.
See also isTrimmed() and TQVariant.
Returns a formatted string composed of the prefix (e.g. table or view name), ".", the field name, the fieldSep and the field value. If the prefix is empty then the string will begin with the field name. This function is useful for generating SQL statements.
Returns a formatted string composed of all the fields in the index i. Each field is composed of the prefix (e.g. table or view name), ".", the field name, the fieldSep and the field value. If the prefix is empty then each field will begin with the field name. The field values are taken from rec. The fields are then joined together separated by sep. Fields where isGenerated() returns FALSE are ignored. This function is useful for generating SQL statements.
Only records which meet the filter criteria specified by the cursor's primary index are updated. If the cursor does not contain a primary index, no update is performed and 0 is returned.
If invalidate 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:
TQSqlCursor cur( "prices" ); cur.select( "id=202" ); if ( cur.next() ) { TQSqlRecord *buffer = cur.primeUpdate(); double price = buffer->value( "price" ).toDouble(); double newprice = price * 1.05; buffer->setValue( "price", newprice ); cur.update(); }
In the above example, a cursor is created on the 'prices' table and is positioned on the record to be updated. Then a pointer to the cursor's edit buffer is actquired using primeUpdate(). A new value is calculated and placed into the edit buffer with the setValue() call. Finally, an update() call is made on the cursor which uses the tables's primary index to update the record in the database with the contents of the cursor's edit buffer. 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.
Note that if the primary index does not uniquely distinguish records the database may be changed into an inconsistent state.
See also setMode() and lastError().
Example: sql/overview/update/main.cpp.
Updates the database with the current contents of the cursor edit buffer using the specified filter. Returns the number of records which were updated. For error information, use lastError().
Only records which meet the filter criteria are updated, otherwise all records in the table are updated.
If invalidate is TRUE (the default), the cursor can no longer be navigated. A new select() call must be made before you can move to a valid record.
See also primeUpdate(), setMode(), and lastError().
This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.
Copyright © 2007 Trolltech | Trademarks | TQt 3.3.8
|