From ea318d1431c89e647598c510c4245c6571aa5f46 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Thu, 26 Jan 2012 23:32:43 -0600 Subject: Update to latest tqt3 automated conversion --- doc/html/sql.html | 894 +++++++++++++++++++++++++++--------------------------- 1 file changed, 447 insertions(+), 447 deletions(-) (limited to 'doc/html/sql.html') diff --git a/doc/html/sql.html b/doc/html/sql.html index 19db86b68..c57f7ab2a 100644 --- a/doc/html/sql.html +++ b/doc/html/sql.html @@ -35,26 +35,26 @@ body { background: #ffffff; color: black; }

- - - -
TQSql -TQSqlCursor -TQSqlDatabase -TQSqlDriver -TQSqlDriverPlugin +TQSql +TQSqlCursor +TQSqlDatabase +TQSqlDriver +TQSqlDriverPlugin
TQSqlEditorFactory -TQSqlError -TQSqlField +TQSqlEditorFactory +TQSqlError +TQSqlField TQSqlFieldInfo -TQSqlForm +TQSqlForm
TQSqlIndex -TQSqlPropertyMap -TQSqlQuery -TQSqlRecord +TQSqlIndex +TQSqlPropertyMap +TQSqlQuery +TQSqlRecord TQSqlRecordInfo
TQSqlResult -TQSqlSelectCursor +TQSqlResult +TQSqlSelectCursor See also: Supported Drivers

@@ -144,7 +144,7 @@ your TQt applications.

This overview assumes that you have at least a basic knowledge of SQL. You should be able to understand simple SELECT, INSERT, UPDATE -and DELETE commands. Although the TQSqlCursor class provides an +and DELETE commands. Although the TQSqlCursor class provides an interface to database browsing and editing that does not require a knowledge of SQL, a basic understanding of SQL is highly recommended. A standard text covering SQL databases is An Introduction to Database Systems (7th ed.) by C. J. Date, ISBN 0201385902. @@ -159,22 +159,22 @@ handle foreign key lookups.

SQL Module Architecture. This describes how the classes fit together.

Connecting to Databases. -This section explains how to set up database connections using the TQSqlDatabase class. +This section explains how to set up database connections using the TQSqlDatabase class.

Executing SQL Commands. This section demonstrates how to issue the standard data manipulation commands, SELECT, INSERT, UPDATE and DELETE on tables in the database (although any valid SQL statement can be sent to the -database). The focus is purely on database interaction using TQSqlQuery. +database). The focus is purely on database interaction using TQSqlQuery.

Using Cursors. This section explains -how to use the TQSqlCursor class which provides a simpler API than the -raw SQL used with TQSqlQuery. +how to use the TQSqlCursor class which provides a simpler API than the +raw SQL used with TQSqlQuery.

Data-Aware Widgets. This section shows how to programmatically link your database to the user interface. In -this section we introduce the TQDataTable, TQSqlForm, TQSqlPropertyMap and TQSqlEditorFactory classes and demonstrate how to +this section we introduce the TQDataTable, TQSqlForm, TQSqlPropertyMap and TQSqlEditorFactory classes and demonstrate how to use custom data-aware widgets. TQt Designer provides an easy visual way of achieving the same thing. See the TQt Designer manual, -TQDataBrowser and TQDataView for more information. +TQDataBrowser and TQDataView for more information.

Subclassing TQSqlCursor. This section gives examples of subclassing TQSqlCursor. Subclassing can be used to provide default and calculated values for fields (such as @@ -188,20 +188,20 @@ e.g. showing names rather than ids of foreign keys.

The SQL classes are divided into three layers:

User Interface Layer. These classes provide data-aware widgets that can be connected to tables or views in the database (by using a -TQSqlCursor as a data source). End users can interact directly with +TQSqlCursor as a data source). End users can interact directly with these widgets to browse or edit data. TQt Designer is fully integrated with the SQL classes and can be used to create data-aware forms. The data-aware widgets can also be programmed directly with your own C++ code. The classes that support -this layer include TQSqlEditorFactory, TQSqlForm, TQSqlPropertyMap, TQDataTable, TQDataBrowser and TQDataView. +this layer include TQSqlEditorFactory, TQSqlForm, TQSqlPropertyMap, TQDataTable, TQDataBrowser and TQDataView.

SQL API Layer. These classes provide access to databases. -Connections are made using the TQSqlDatabase class. Database -interaction is achieved either by using the TQSqlQuery class and -executing SQL commands directly or by using the higher level TQSqlCursor class which composes SQL commands automatically. In -addition to TQSqlDatabase, TQSqlCursor and TQSqlQuery, the SQL -API layer is supported by TQSqlError, TQSqlField, TQSqlFieldInfo, -TQSqlIndex, TQSqlRecord and TQSqlRecordInfo. -

Driver Layer. This comprises three classes, TQSqlResult, TQSqlDriver and TQSqlDriverFactoryInterface. This layer provides the +Connections are made using the TQSqlDatabase class. Database +interaction is achieved either by using the TQSqlQuery class and +executing SQL commands directly or by using the higher level TQSqlCursor class which composes SQL commands automatically. In +addition to TQSqlDatabase, TQSqlCursor and TQSqlQuery, the SQL +API layer is supported by TQSqlError, TQSqlField, TQSqlFieldInfo, +TQSqlIndex, TQSqlRecord and TQSqlRecordInfo. +

Driver Layer. This comprises three classes, TQSqlResult, TQSqlDriver and TQSqlDriverFactoryInterface. This layer provides the low level bridge between the database and the SQL classes. This layer is documented separately since it is only relevant to driver writers, and is rarely used in standard @@ -214,17 +214,17 @@ the Plugins.

The SQL driver documentation describes how to build plugins for specific database management systems.

Once a plugin is built, TQt will automatically load it, and the driver -will be available for use by TQSqlDatabase (see TQSqlDatabase::drivers() +will be available for use by TQSqlDatabase (see TQSqlDatabase::drivers() for more information).

Connecting to Databases

At least one database connection must be created and opened before the -TQSqlQuery or TQSqlCursor classes can be used. -

If the application only needs a single database connection, the TQSqlDatabase class can create a connection which is used by default +TQSqlQuery or TQSqlCursor classes can be used. +

If the application only needs a single database connection, the TQSqlDatabase class can create a connection which is used by default for all SQL operations. If multiple database connections are required these can easily be set up. -

TQSqlDatabase requires the qsqldatabase.h header file. +

TQSqlDatabase requires the ntqsqldatabase.h header file.

Connecting to a Single Database

@@ -233,21 +233,21 @@ the driver, set up the connection information, and open the connection.

-

    #include <qapplication.h>
-    #include <qsqldatabase.h>
+
    #include <ntqapplication.h>
+    #include <ntqsqldatabase.h>
     #include "../connection.h"
 
     int main( int argc, char *argv[] )
     {
-        TQApplication app( argc, argv, FALSE );
+        TQApplication app( argc, argv, FALSE );
 
-        TQSqlDatabase *defaultDB = TQSqlDatabase::addDatabase( DB_SALES_DRIVER );
-        defaultDB->setDatabaseName( DB_SALES_DBNAME );
-        defaultDB->setUserName( DB_SALES_USER );
-        defaultDB->setPassword( DB_SALES_PASSWD );
-        defaultDB->setHostName( DB_SALES_HOST );
+        TQSqlDatabase *defaultDB = TQSqlDatabase::addDatabase( DB_SALES_DRIVER );
+        defaultDB->setDatabaseName( DB_SALES_DBNAME );
+        defaultDB->setUserName( DB_SALES_USER );
+        defaultDB->setPassword( DB_SALES_PASSWD );
+        defaultDB->setHostName( DB_SALES_HOST );
 
-        if ( defaultDB->open() ) {
+        if ( defaultDB->open() ) {
             // Database successfully opened; we can now issue SQL commands.
         }
 
@@ -255,7 +255,7 @@ connection.
     }
 

From sql/overview/connect1/main.cpp

-

First we activate the driver by calling TQSqlDatabase::addDatabase(), +

First we activate the driver by calling TQSqlDatabase::addDatabase(), passing the name of the driver we wish to use for this connection. At the time of writing the available drivers are: TQODBC3 (Open Database Connectivity, includes Microsoft SQL Server support), TQOCI8 (Oracle 8 and 9), @@ -273,12 +273,12 @@ to setDatbaseName(). When connecting to ODBC data sources the Data Source Name (DSN) should be used in the setDatabaseName() call.

Third we call open() to open the database and give us access to the data. If this call fails it will return FALSE; error information can -be obtained from TQSqlDatabase::lastError(). +be obtained from TQSqlDatabase::lastError().

Connecting to Multiple Databases

Connecting to multiple databases is achieved using the two argument form -of TQSqlDatabase::addDatabase() where the second argument is a unique +of TQSqlDatabase::addDatabase() where the second argument is a unique identifier distinguishing the connection.

In the example below we have moved the connections into their own function, createConnections(), and added some basic error handling. @@ -302,45 +302,45 @@ bool createConnections(); function in connection.h.

-

    #include <qsqldatabase.h>
+
    #include <ntqsqldatabase.h>
     #include "connection.h"
 
     bool createConnections()
     {
 
-        TQSqlDatabase *defaultDB = TQSqlDatabase::addDatabase( DB_SALES_DRIVER );
-        defaultDB->setDatabaseName( DB_SALES_DBNAME );
-        defaultDB->setUserName( DB_SALES_USER );
-        defaultDB->setPassword( DB_SALES_PASSWD );
-        defaultDB->setHostName( DB_SALES_HOST );
-        if ( ! defaultDB->open() ) {
-            qWarning( "Failed to open sales database: " + defaultDB->lastError().text() );
+        TQSqlDatabase *defaultDB = TQSqlDatabase::addDatabase( DB_SALES_DRIVER );
+        defaultDB->setDatabaseName( DB_SALES_DBNAME );
+        defaultDB->setUserName( DB_SALES_USER );
+        defaultDB->setPassword( DB_SALES_PASSWD );
+        defaultDB->setHostName( DB_SALES_HOST );
+        if ( ! defaultDB->open() ) {
+            qWarning( "Failed to open sales database: " + defaultDB->lastError().text() );
             return FALSE;
         }
 
-        TQSqlDatabase *oracle = TQSqlDatabase::addDatabase( DB_ORDERS_DRIVER, "ORACLE" );
-        oracle->setDatabaseName( DB_ORDERS_DBNAME );
-        oracle->setUserName( DB_ORDERS_USER );
-        oracle->setPassword( DB_ORDERS_PASSWD );
-        oracle->setHostName( DB_ORDERS_HOST );
-        if ( ! oracle->open() ) {
-            qWarning( "Failed to open orders database: " + oracle->lastError().text() );
+        TQSqlDatabase *oracle = TQSqlDatabase::addDatabase( DB_ORDERS_DRIVER, "ORACLE" );
+        oracle->setDatabaseName( DB_ORDERS_DBNAME );
+        oracle->setUserName( DB_ORDERS_USER );
+        oracle->setPassword( DB_ORDERS_PASSWD );
+        oracle->setHostName( DB_ORDERS_HOST );
+        if ( ! oracle->open() ) {
+            qWarning( "Failed to open orders database: " + oracle->lastError().text() );
             return FALSE;
         }
 
-        TQSqlQuery q(TQString::null, defaultDB);
-        q.exec("create table people (id integer primary key, name char(40))");
-        q.exec("create table staff (id integer primary key, forename char(40), "
+        TQSqlQuery q(TQString::null, defaultDB);
+        q.exec("create table people (id integer primary key, name char(40))");
+        q.exec("create table staff (id integer primary key, forename char(40), "
                "surname char(40), salary float, statusid integer)");
-        q.exec("create table status (id integer primary key, name char(30))");
-        q.exec("create table creditors (id integer primary key, forename char(40), "
+        q.exec("create table status (id integer primary key, name char(30))");
+        q.exec("create table creditors (id integer primary key, forename char(40), "
                "surname char(40), city char(30))");
-        q.exec("create table prices (id integer primary key, name char(40), price float)");
-        q.exec("create table invoiceitem (id integer primary key, "
+        q.exec("create table prices (id integer primary key, name char(40), price float)");
+        q.exec("create table invoiceitem (id integer primary key, "
                "pricesid integer, quantity integer, paiddate date)");
 
-        TQSqlQuery q2(TQString::null, oracle);
-        q2.exec("create table people (id integer primary key, name char(40))");
+        TQSqlQuery q2(TQString::null, oracle);
+        q2.exec("create table people (id integer primary key, name char(40))");
 
         return TRUE;
     }
@@ -350,17 +350,17 @@ function in connection.h.
 

-

    #include <qapplication.h>
-    #include <qsqldatabase.h>
+
    #include <ntqapplication.h>
+    #include <ntqsqldatabase.h>
     #include "../connection.h"
 
     int main( int argc, char *argv[] )
     {
-        TQApplication app( argc, argv, FALSE );
+        TQApplication app( argc, argv, FALSE );
 
         if ( createConnections() ) {
             // Databases successfully opened; get pointers to them:
-            TQSqlDatabase *oracledb = TQSqlDatabase::database( "ORACLE" );
+            TQSqlDatabase *oracledb = TQSqlDatabase::database( "ORACLE" );
             // Now we can now issue SQL commands to the oracle connection
             // or to the default connection
         }
@@ -369,7 +369,7 @@ function in connection.h.
     }
 

From sql/overview/create_connections/main.cpp

-

The static function TQSqlDatabase::database() can be called from +

The static function TQSqlDatabase::database() can be called from anywhere to provide a pointer to a database connection. If we call it without a parameter it will return the default connection. If called with the identifier we've used for a connection, e.g. "ORACLE", in the @@ -381,57 +381,57 @@ preview correctly in TQt Designer will not run unless you implement your own database connections function.

Note that in the code above the ODBC connection was not named and is -therefore used as the default connection. TQSqlDatabase maintains +therefore used as the default connection. TQSqlDatabase maintains ownership of the pointers returned by the addDatabase() static function. To remove a database from the list of maintained -connections, first close the database with TQSqlDatabase::close(), and +connections, first close the database with TQSqlDatabase::close(), and then remove it using the static function -TQSqlDatabase::removeDatabase(). +TQSqlDatabase::removeDatabase().

-

Executing SQL Commands Using TQSqlQuery +

Executing SQL Commands Using TQSqlQuery

-

The TQSqlQuery class provides an interface for executing SQL commands. +

The TQSqlQuery class provides an interface for executing SQL commands. It also has functions for navigating through the result sets of SELECT queries and for retrieving individual records and field values. -

The TQSqlCursor class described in the next section inherits from TQSqlQuery and provides a higher level interface that composes SQL -commands for us. TQSqlCursor is particularly easy to integrate with +

The TQSqlCursor class described in the next section inherits from TQSqlQuery and provides a higher level interface that composes SQL +commands for us. TQSqlCursor is particularly easy to integrate with on-screen widgets. Programmers unfamiliar with SQL can safely skip this -section and use the TQSqlCursor class covered in +section and use the TQSqlCursor class covered in "Using TQSqlCursor".

Transactions

If the underlying database engine supports transactions -TQSqlDriver::hasFeature( TQSqlDriver::Transactions ) will return TRUE. -You can use TQSqlDatabase::transaction() to initiate a transaction, +TQSqlDriver::hasFeature( TQSqlDriver::Transactions ) will return TRUE. +You can use TQSqlDatabase::transaction() to initiate a transaction, followed by the SQL commands you want to execute within the context of -the transaction, and then either TQSqlDatabase::commit() or -TQSqlDatabase::rollback(). +the transaction, and then either TQSqlDatabase::commit() or +TQSqlDatabase::rollback().

Basic Browsing

-

    #include <qapplication.h>
-    #include <qsqldatabase.h>
-    #include <qsqlquery.h>
+
    #include <ntqapplication.h>
+    #include <ntqsqldatabase.h>
+    #include <ntqsqlquery.h>
     #include "../connection.h"
 
     int main( int argc, char *argv[] )
     {
-        TQApplication app( argc, argv, FALSE );
+        TQApplication app( argc, argv, FALSE );
 
         if ( createConnections() ) {
-            TQSqlDatabase *oracledb = TQSqlDatabase::database( "ORACLE" );
+            TQSqlDatabase *oracledb = TQSqlDatabase::database( "ORACLE" );
             // Copy data from the oracle database to the ODBC (default)
             // database
-            TQSqlQuery target;
-            TQSqlQuery query( "SELECT id, name FROM people", oracledb );
-            if ( query.isActive() ) {
-                while ( query.next() ) {
-                    target.exec( "INSERT INTO people ( id, name ) VALUES ( " +
-                                  query.value(0).toString() +
-                                  ", '" + query.value(1).toString() +  "' )" );
+            TQSqlQuery target;
+            TQSqlQuery query( "SELECT id, name FROM people", oracledb );
+            if ( query.isActive() ) {
+                while ( query.next() ) {
+                    target.exec( "INSERT INTO people ( id, name ) VALUES ( " +
+                                  query.value(0).toString() +
+                                  ", '" + query.value(1).toString() +  "' )" );
                 }
             }
         }
@@ -441,7 +441,7 @@ the transaction, and then either TQSqlDatabas
 

From sql/overview/basicbrowsing/main.cpp

In the example above we've added an additional header file, -qsqlquery.h. The first query we create, target, uses the default +ntqsqlquery.h. The first query we create, target, uses the default database and is initially empty. For the second query, q, we specify the "ORACLE" database that we want to retrieve records from. Both the database connections were set up in the createConnections() function we @@ -451,20 +451,20 @@ to see if the query executed successfully. The next() function is used to iterate through the query results. The value() function returns the contents of fields as TQVariants. The insertions are achieved by creating and executing queries against the default -database using the target TQSqlQuery. +database using the target TQSqlQuery.

Note that this example and all the other examples in this document use the tables defined in the Example Tables section.

            int count = 0;
-            if ( query.isActive() ) {
-                while ( query.next() ) {
-                    target.exec( "INSERT INTO people ( id, name ) VALUES ( " +
-                                  query.value(0).toString() +
-                                  ", '" + query.value(1).toString() +  "' )" );
-                    if ( target.isActive() )
-                        count += target.numRowsAffected();
+            if ( query.isActive() ) {
+                while ( query.next() ) {
+                    target.exec( "INSERT INTO people ( id, name ) VALUES ( " +
+                                  query.value(0).toString() +
+                                  ", '" + query.value(1).toString() +  "' )" );
+                    if ( target.isActive() )
+                        count += target.numRowsAffected();
                 }
             }
 

From sql/overview/basicbrowsing2/main.cpp @@ -487,29 +487,29 @@ cannot be determined, e.g. if the query fails. ** *****************************************************************************/ - #include <qapplication.h> - #include <qsqldatabase.h> - #include <qsqlquery.h> + #include <ntqapplication.h> + #include <ntqsqldatabase.h> + #include <ntqsqlquery.h> #include "../connection.h" bool createConnections(); int main( int argc, char *argv[] ) { - TQApplication app( argc, argv, FALSE ); + TQApplication app( argc, argv, FALSE ); int rows = 0; if ( createConnections() ) { - TQSqlQuery query( "INSERT INTO staff ( id, forename, surname, salary ) " + TQSqlQuery query( "INSERT INTO staff ( id, forename, surname, salary ) " "VALUES ( 1155, 'Ginger', 'Davis', 50000 )" ); - if ( query.isActive() ) rows += query.numRowsAffected() ; + if ( query.isActive() ) rows += query.numRowsAffected() ; - query.exec( "UPDATE staff SET salary=60000 WHERE id=1155" ); - if ( query.isActive() ) rows += query.numRowsAffected() ; + query.exec( "UPDATE staff SET salary=60000 WHERE id=1155" ); + if ( query.isActive() ) rows += query.numRowsAffected() ; - query.exec( "DELETE FROM staff WHERE id=1155" ); - if ( query.isActive() ) rows += query.numRowsAffected() ; + query.exec( "DELETE FROM staff WHERE id=1155" ); + if ( query.isActive() ) rows += query.numRowsAffected() ; } return ( rows == 3 ) ? 0 : 1; @@ -517,7 +517,7 @@ cannot be determined, e.g. if the query fails.

From sql/overview/basicdatamanip/main.cpp

This example demonstrates straightforward SQL DML (data manipulation -language) commands. Since we did not specify a database in the TQSqlQuery constructor the default database is used. TQSqlQuery objects +language) commands. Since we did not specify a database in the TQSqlQuery constructor the default database is used. TQSqlQuery objects can also be used to execute SQL DDL (data definition language) commands such as CREATE TABLE and CREATE INDEX.

@@ -526,7 +526,7 @@ such as CREATE TABLE and CREATE INDEX.

Once a SELECT query has been executed successfully we have access to the result set of records that matched the query criteria. We have already used one of the navigation functions, next(), which can be -used alone to step sequentially through the records. TQSqlQuery also +used alone to step sequentially through the records. TQSqlQuery also provides first(), last() and prev(). After any of these commands we can check that we are on a valid record by calling isValid().

We can also navigate to any arbitrary record using seek(). The @@ -536,16 +536,16 @@ size() - 1. Note that not all databases provide the size of a

        if ( createConnections() ) {
-            TQSqlQuery query( "SELECT id, name FROM people ORDER BY name" );
-            if ( ! query.isActive() ) return 1; // Query failed
+            TQSqlQuery query( "SELECT id, name FROM people ORDER BY name" );
+            if ( ! query.isActive() ) return 1; // Query failed
             int i;
-            i = query.size();               // In this example we have 9 records; i == 9.
-            query.first();                  // Moves to the first record.
-            i = query.at();                 // i == 0
-            query.last();                   // Moves to the last record.
-            i = query.at();                 // i == 8
-            query.seek( query.size() / 2 ); // Moves to the middle record.
-            i = query.at();                 // i == 4
+            i = query.size();               // In this example we have 9 records; i == 9.
+            query.first();                  // Moves to the first record.
+            i = query.at();                 // i == 0
+            query.last();                   // Moves to the last record.
+            i = query.at();                 // i == 8
+            query.seek( query.size() / 2 ); // Moves to the middle record.
+            i = query.at();                 // i == 4
         }
 

From sql/overview/navigating/main.cpp

@@ -553,8 +553,8 @@ size() - 1. Note that not all databases provide the size of a

Not all drivers support size(), but we can interrogate the driver to find out:

-    TQSqlDatabase* defaultDB = TQSqlDatabase::database();
-    if ( defaultDB->driver()->hasFeature( TQSqlDriver::QuerySize ) ) {
+    TQSqlDatabase* defaultDB = TQSqlDatabase::database();
+    if ( defaultDB->driver()->hasFeature( TQSqlDriver::QuerySize ) ) {
         // TQSqlQuery::size() supported
     }
     else {
@@ -567,11 +567,11 @@ retrieve data from it.
 

        if ( createConnections() ) {
-            TQSqlQuery query( "SELECT id, surname FROM staff" );
-            if ( query.isActive() ) {
-                while ( query.next() ) {
-                    qDebug( query.value(0).toString() + ": " +
-                            query.value(1).toString() );
+            TQSqlQuery query( "SELECT id, surname FROM staff" );
+            if ( query.isActive() ) {
+                while ( query.next() ) {
+                    qDebug( query.value(0).toString() + ": " +
+                            query.value(1).toString() );
                 }
             }
         }
@@ -583,27 +583,27 @@ only navigation function you need is next().
 executed. This can be useful to check that the query you think is being
 executed is the one actually being executed.
 

-

Using TQSqlCursor +

Using TQSqlCursor

-

The TQSqlCursor class provides a high level interface to browsing and +

The TQSqlCursor class provides a high level interface to browsing and editing records in SQL database tables or views without the need to write your own SQL. -

TQSqlCursor can do almost everything that TQSqlQuery can, with two +

TQSqlCursor can do almost everything that TQSqlQuery can, with two exceptions. Since cursors represent tables or views within the -database, by default, TQSqlCursor objects retrieve all the fields of +database, by default, TQSqlCursor objects retrieve all the fields of each record in the table or view whenever navigating to a new record. If only some fields are relevant simply confine your processing to those and ignore the others. Or, manually disable the -generation of certain fields using TQSqlRecord::setGenerated(). Another +generation of certain fields using TQSqlRecord::setGenerated(). Another approach is to create a VIEW which only presents the fields you're interested in; but note that some databases do not support editable views. So if you really don't want to retrieve all the fields in the -cursor, then you should use a TQSqlQuery instead, and customize the -query to suit your needs. You can edit records using a TQSqlCursor +cursor, then you should use a TQSqlQuery instead, and customize the +query to suit your needs. You can edit records using a TQSqlCursor providing that the table or view has a primary index that uniquely distinguishes each record. If this condition is not met then you'll -need to use a TQSqlQuery for edits. -

TQSqlCursor operates on a single record at a time. Whenever performing +need to use a TQSqlQuery for edits. +

TQSqlCursor operates on a single record at a time. Whenever performing an insert, update or delete using TQSqlCursor, only a single record in the database is affected. When navigating through records in the cursor, only one record at a time is available in application code. @@ -612,7 +612,7 @@ used to make changes to a single record in the database. The edit buffer is maintained in a separate memory area, and is unnaffected by the 'navigation buffer' which changes as the cursor moves from record to record. -

Before we can use TQSqlCursor objects we must first create and open +

Before we can use TQSqlCursor objects we must first create and open a database connection. Connecting is described in the Connecting to Databases section above. For the examples that follow we will assume that the connections have been created using the createConnections() function @@ -622,28 +622,28 @@ presented earlier. follows this one we show how to link widgets to database cursors. Once we have a knowledge of both cursors and data-aware widgets we can discuss subclassing TQSqlCursor. -

The TQSqlCursor class requires the qsqlcursor.h header file. +

The TQSqlCursor class requires the ntqsqlcursor.h header file.

Retrieving Records

-

    #include <qapplication.h>
-    #include <qsqldatabase.h>
-    #include <qsqlcursor.h>
+
    #include <ntqapplication.h>
+    #include <ntqsqldatabase.h>
+    #include <ntqsqlcursor.h>
     #include "../connection.h"
 
     int main( int argc, char *argv[] )
     {
-        TQApplication app( argc, argv );
+        TQApplication app( argc, argv );
 
         if ( createConnections() ) {
-            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() );
+            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() );
             }
         }
 
@@ -651,9 +651,9 @@ discuss subclassing TQSqlCursor.
     }
 

From sql/overview/retrieve2/main.cpp

-

We create the TQSqlCursor object, specifying the table or view to use. +

We create the TQSqlCursor object, specifying the table or view to use. If we need to use a database other than the default we can specify it -in the TQSqlCursor constructor. +in the TQSqlCursor constructor.

The SQL executed by the cur.select() call is

     SELECT staff.id, staff.forename, staff.surname, staff.salary, staff.statusid FROM staff
@@ -661,7 +661,7 @@ in the TQSqlCursor constructor.
  
 

Next, we iterate through the records returned by this select statement using cur.next(). Field values are retrieved in in a similar way to -TQSqlQuery, except that we pass field names rather than numeric indexes +TQSqlQuery, except that we pass field names rather than numeric indexes to value() and setValue().

Sorting and Filtering Records @@ -682,15 +682,15 @@ statement's WHERE clause).

This will retrieve only those staff whose id is greater than 100.

In addition to retrieving selected records we often want to specify a -sort order for the returned records. This is achieved by creating a TQSqlIndex object which contains the names of the field(s) we wish to +sort order for the returned records. This is achieved by creating a TQSqlIndex object which contains the names of the field(s) we wish to sort by and pass this object to the select() call.

-    TQSqlCursor cur( "staff" );
-    TQSqlIndex nameIndex = cur.index( "surname" ); 
-    cur.select( nameIndex );
+    TQSqlCursor cur( "staff" );
+    TQSqlIndex nameIndex = cur.index( "surname" ); 
+    cur.select( nameIndex );
 
-

Here we create a TQSqlIndex object with one field, "surname". When +

Here we create a TQSqlIndex object with one field, "surname". When we call the select() function we pass the index object, which specifies that the records should be returned sorted by staff.surname. Each field in the index object is used in the ORDER BY @@ -706,7 +706,7 @@ is straightforward. cur.select( "staff.surname LIKE 'A%'", nameIndex );

-

We pass in a filter string (the WHERE clause), and the TQSqlIndex +

We pass in a filter string (the WHERE clause), and the TQSqlIndex object to sort by (the ORDER BY clause). This produces

     SELECT staff.id, staff.forename, staff.surname, staff.salary, staff.statusid
@@ -715,18 +715,18 @@ object to sort by (the ORDER BY clause). This produces
  
 

To sort by more than one field, an index can be created which contains multiple fields. Ascending and descending order can be set using -TQSqlIndex::setDescending(); the default is ascending. +TQSqlIndex::setDescending(); the default is ascending.

-

            TQSqlCursor cur( "staff" );
-            TQStringList fields = TQStringList() << "surname" << "forename";
-            TQSqlIndex order = cur.index( fields );
-            cur.select( order );
-            while ( cur.next() ) {
+
            TQSqlCursor cur( "staff" );
+            TQStringList fields = TQStringList() << "surname" << "forename";
+            TQSqlIndex order = cur.index( fields );
+            cur.select( order );
+            while ( cur.next() ) {
 

From sql/overview/order1/main.cpp

Here we create a string list containing the fields we wish to sort by, -in the order they are to be used. Then we create a TQSqlIndex object +in the order they are to be used. Then we create a TQSqlIndex object based on these fields, finally executing the select() call using this index. This executes

@@ -738,13 +738,13 @@ index. This executes
 can create a filter based on an index.
 

-

            TQSqlCursor cur( "staff" );
-            TQStringList fields = TQStringList() << "id" << "forename";
-            TQSqlIndex order = cur.index( fields );
-            TQSqlIndex filter = cur.index( "surname" );
-            cur.setValue( "surname", "Bloggs" );
-            cur.select( filter, order );
-            while ( cur.next() ) {
+
            TQSqlCursor cur( "staff" );
+            TQStringList fields = TQStringList() << "id" << "forename";
+            TQSqlIndex order = cur.index( fields );
+            TQSqlIndex filter = cur.index( "surname" );
+            cur.setValue( "surname", "Bloggs" );
+            cur.select( filter, order );
+            while ( cur.next() ) {
 

From sql/overview/order2/main.cpp

This executes @@ -753,8 +753,8 @@ can create a filter based on an index. FROM staff WHERE staff.surname='Bloggs' ORDER BY staff.id ASC, staff.forename ASC

-

The "order" TQSqlIndex contains two fields, "id" and "forename" -which are used to order the results. The "filter" TQSqlIndex +

The "order" TQSqlIndex contains two fields, "id" and "forename" +which are used to order the results. The "filter" TQSqlIndex contains a single field, "surname". When an index is passed as a filter to the select() function, for each field in the filter, a fieldname=value subclause is created where the value @@ -765,27 +765,27 @@ setValue() to ensure that the value used is the one we want.

-

            TQSqlCursor cur( "creditors" );
-            TQStringList orderFields = TQStringList() << "surname" << "forename";
-            TQSqlIndex order = cur.index( orderFields );
+
            TQSqlCursor cur( "creditors" );
+            TQStringList orderFields = TQStringList() << "surname" << "forename";
+            TQSqlIndex order = cur.index( orderFields );
 
-            TQStringList filterFields = TQStringList() << "surname" << "city";
-            TQSqlIndex filter = cur.index( filterFields );
-            cur.setValue( "surname", "Chirac" );
-            cur.setValue( "city", "Paris" );
+            TQStringList filterFields = TQStringList() << "surname" << "city";
+            TQSqlIndex filter = cur.index( filterFields );
+            cur.setValue( "surname", "Chirac" );
+            cur.setValue( "city", "Paris" );
 
-            cur.select( filter, order );
+            cur.select( filter, order );
 
-            while ( cur.next() ) {
-                int id = cur.value( "id" ).toInt();
-                TQString name = cur.value( "forename" ).toString() + " " +
-                               cur.value( "surname" ).toString();
-                qDebug( TQString::number( id ) + ": " + name );
+            while ( cur.next() ) {
+                int id = cur.value( "id" ).toInt();
+                TQString name = cur.value( "forename" ).toString() + " " +
+                               cur.value( "surname" ).toString();
+                qDebug( TQString::number( id ) + ": " + name );
             }
 

From sql/overview/extract/main.cpp

In this example we begin by creating a cursor on the creditors table. -We create two TQSqlIndex objects. The first, "order", is created +We create two TQSqlIndex objects. The first, "order", is created from the "orderFields" string list. The second, "filter", is created from the "filterFields" string list. We set the values of the two fields used in the filter, "surname" and "city", to the values we're @@ -804,13 +804,13 @@ values ourselves with the setValue() calls. The order fields are used in the ORDER BY clause.

Now we iterate through each matching record (if any). We retrieve the contents of the id, forename and surname fields and pass them on to -some processing function, in this example a simple qDebug() call. +some processing function, in this example a simple qDebug() call.

Manipulating Records

Records can be inserted, updated or deleted in a table or view using a -TQSqlCursor providing that the table or view has a primary index -that uniquely distinguishes each record. If this is not the case a TQSqlQuery must be used instead. (Note that not all databases support +TQSqlCursor providing that the table or view has a primary index +that uniquely distinguishes each record. If this is not the case a TQSqlQuery must be used instead. (Note that not all databases support editable views.)

Each cursor has an internal 'edit buffer' which is used by all the edit operations (insert, update and delete). The editing process is @@ -831,8 +831,8 @@ quote is a special character in SQL.

The primeInsert(), primeUpdate() and primeDelete() methods all return a pointer to the internal edit buffer. Each method can potentially perform different operations on the edit buffer before returning it. -By default, TQSqlCursor::primeInsert() clears all the field values in -the edit buffer (see TQSqlRecord::clearValues()). Both TQSqlCursor::primeUpdate() and TQSqlCursor::primeDelete() initialize the +By default, TQSqlCursor::primeInsert() clears all the field values in +the edit buffer (see TQSqlRecord::clearValues()). Both TQSqlCursor::primeUpdate() and TQSqlCursor::primeDelete() initialize the edit buffer with the current contents of the cursor before returning it. All three of these functions are virtual, so you can redefine the behavior (for example, reimplementing primeInsert() to auto-number @@ -853,17 +853,17 @@ changes to the database are accurately reflected in the cursor.

-

            TQSqlCursor cur( "prices" );
-            TQStringList names = TQStringList() <<
+
            TQSqlCursor cur( "prices" );
+            TQStringList names = TQStringList() <<
                 "Screwdriver" << "Hammer" << "Wrench" << "Saw";
             int id = 20;
-            for ( TQStringList::Iterator name = names.begin();
-                  name != names.end(); ++name ) {
-                TQSqlRecord *buffer = cur.primeInsert();
-                buffer->setValue( "id", id );
-                buffer->setValue( "name", *name );
-                buffer->setValue( "price", 100.0 + (double)id );
-                count += cur.insert();
+            for ( TQStringList::Iterator name = names.begin();
+                  name != names.end(); ++name ) {
+                TQSqlRecord *buffer = cur.primeInsert();
+                buffer->setValue( "id", id );
+                buffer->setValue( "name", *name );
+                buffer->setValue( "price", 100.0 + (double)id );
+                count += cur.insert();
                 id++;
             }
 

From sql/overview/insert/main.cpp @@ -871,14 +871,14 @@ changes to the database are accurately reflected in the cursor.

In this example we create a cursor on the "prices" table. Next we create a list of product names which we iterate over. For each iteration we call the cursor's primeInsert() method. This method -returns a pointer to a TQSqlRecord buffer in which all the fields -are set to NULL. (Note that TQSqlCursor::primeInsert() is virtual, -and can be customized by derived classes. See TQSqlCursor). Next we +returns a pointer to a TQSqlRecord buffer in which all the fields +are set to NULL. (Note that TQSqlCursor::primeInsert() is virtual, +and can be customized by derived classes. See TQSqlCursor). Next we call setValue() for each field that requires a value. Finally we call insert() to insert the record. The insert() call returns the number of rows inserted. -

We obtained a pointer to a TQSqlRecord object from the primeInsert() -call. TQSqlRecord objects can hold the data for a single record plus some +

We obtained a pointer to a TQSqlRecord object from the primeInsert() +call. TQSqlRecord objects can hold the data for a single record plus some meta-data about the record. In practice most interaction with a TQSqlRecord consists of simple value() and setValue() calls as shown in this and the following example. @@ -887,29 +887,29 @@ this and the following example.

-

            TQSqlCursor cur( "prices" );
-            cur.select( "id=202" );
-            if ( cur.next() ) {
-                TQSqlRecord *buffer = cur.primeUpdate();
-                double price = buffer->value( "price" ).toDouble();
+
            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();
+                buffer->setValue( "price", newprice );
+                cur.update();
             }
 

From sql/overview/update/main.cpp

This example begins with the creation of a cursor over the prices table. We select the record we wish to update with the select() call and -move to it with the next() call. We call primeUpdate() to get a TQSqlRecord pointer to a buffer which is populated with the contents of +move to it with the next() call. We call primeUpdate() to get a TQSqlRecord pointer to a buffer which is populated with the contents of the current record. We retrieve the value of the price field, calculate a new price, and set the the price field to the newly calculated value. Finally we call update() to update the record. The update() call returns the number of rows updated.

If many identical updates need to be performed, for example increasing the price of every item in the price list, using a single SQL statement -with TQSqlQuery is more efficient, e.g. +with TQSqlQuery is more efficient, e.g.

-    TQSqlQuery query( "UPDATE prices SET price = price * 1.05"  );
+    TQSqlQuery query( "UPDATE prices SET price = price * 1.05"  );
 

@@ -917,22 +917,22 @@ with TQSqlQuery is more efficient, e.g.

-

            TQSqlCursor cur( "prices" );
-            cur.select( "id=999" );
-            if ( cur.next() ) {
-                cur.primeDelete();
-                cur.del();
+
            TQSqlCursor cur( "prices" );
+            cur.select( "id=999" );
+            if ( cur.next() ) {
+                cur.primeDelete();
+                cur.del();
 

From sql/overview/delete/main.cpp

To delete records, select the record to be deleted and navigate to it. Then call primeDelete() to populate the cursor with the primary key of the selected record, (in this example, the prices.id field), and -then call TQSqlCursor::del() to delete it. +then call TQSqlCursor::del() to delete it.

As with update(), if multiple deletions need to be made with some common criteria it is more efficient to do so using a single SQL statement, e.g.

-    TQSqlQuery query( "DELETE FROM prices WHERE id >= 2450 AND id <= 2500" );
+    TQSqlQuery query( "DELETE FROM prices WHERE id >= 2450 AND id <= 2500" );
 

@@ -950,58 +950,58 @@ examples provides additional information.

-

    #include <qapplication.h>
-    #include <qsqldatabase.h>
-    #include <qsqlcursor.h>
-    #include <qdatatable.h>
+
    #include <ntqapplication.h>
+    #include <ntqsqldatabase.h>
+    #include <ntqsqlcursor.h>
+    #include <ntqdatatable.h>
     #include "../connection.h"
 
     int main( int argc, char *argv[] )
     {
-        TQApplication app( argc, argv );
+        TQApplication app( argc, argv );
 
         if ( createConnections() ) {
-            TQSqlCursor staffCursor( "staff" );
-            TQDataTable *staffTable = new TQDataTable( &staffCursor, TRUE );
-            app.setMainWidget( staffTable );
-            staffTable->refresh();
-            staffTable->show();
+            TQSqlCursor staffCursor( "staff" );
+            TQDataTable *staffTable = new TQDataTable( &staffCursor, TRUE );
+            app.setMainWidget( staffTable );
+            staffTable->refresh();
+            staffTable->show();
 
-            return app.exec();
+            return app.exec();
         }
 
         return 0;
     }
 

From sql/overview/table1/main.cpp

-

Data-Aware tables require the qdatatable.h and qsqlcursor.h header +

Data-Aware tables require the ntqdatatable.h and ntqsqlcursor.h header files. We create our application object, call createConnections() and -create the cursor. We create the TQDataTable passing it a pointer to -the cursor, and set the autoPopulate flag to TRUE. Next we make our TQDataTable the main widget and call refresh() to populate it with data +create the cursor. We create the TQDataTable passing it a pointer to +the cursor, and set the autoPopulate flag to TRUE. Next we make our TQDataTable the main widget and call refresh() to populate it with data and call show() to make it visible. -

The autoPopulate flag tells the TQDataTable whether or nor it should +

The autoPopulate flag tells the TQDataTable whether or nor it should create columns based on the cursor. autoPopulate does not affect the loading of data into the table; that is achieved by the refresh() function.

-

            TQSqlCursor staffCursor( "staff" );
-            TQDataTable *staffTable = new TQDataTable( &staffCursor );
+
            TQSqlCursor staffCursor( "staff" );
+            TQDataTable *staffTable = new TQDataTable( &staffCursor );
 
-            app.setMainWidget( staffTable );
+            app.setMainWidget( staffTable );
 
-            staffTable->addColumn( "forename", "Forename" );
-            staffTable->addColumn( "surname",  "Surname" );
-            staffTable->addColumn( "salary",   "Annual Salary" );
+            staffTable->addColumn( "forename", "Forename" );
+            staffTable->addColumn( "surname",  "Surname" );
+            staffTable->addColumn( "salary",   "Annual Salary" );
 
-            TQStringList order = TQStringList() << "surname" << "forename";
-            staffTable->setSort( order );
+            TQStringList order = TQStringList() << "surname" << "forename";
+            staffTable->setSort( order );
 
-            staffTable->refresh();
-            staffTable->show();
+            staffTable->refresh();
+            staffTable->show();
 

From sql/overview/table2/main.cpp

-

We create an empty TQDataTable which we make into our main widget and +

We create an empty TQDataTable which we make into our main widget and then we manually add the columns we want in the order we wish them to appear. For each column we specify the field name and optionally a display label. @@ -1023,17 +1023,17 @@ the code below can be automatically generated by

-

    #include <qapplication.h>
-    #include <qdialog.h>
-    #include <qlabel.h>
-    #include <qlayout.h>
-    #include <qlineedit.h>
-    #include <qsqldatabase.h>
-    #include <qsqlcursor.h>
-    #include <qsqlform.h>
+
    #include <ntqapplication.h>
+    #include <ntqdialog.h>
+    #include <ntqlabel.h>
+    #include <ntqlayout.h>
+    #include <ntqlineedit.h>
+    #include <ntqsqldatabase.h>
+    #include <ntqsqlcursor.h>
+    #include <ntqsqlform.h>
     #include "../connection.h"
 
-    class FormDialog : public TQDialog
+    class FormDialog : public TQDialog
     {
         public:
             FormDialog();
@@ -1041,12 +1041,12 @@ the code below can be automatically generated by 
 
     FormDialog::FormDialog()
     {
-        TQLabel *forenameLabel   = new TQLabel( "Forename:", this );
-        TQLabel *forenameDisplay = new TQLabel( this );
-        TQLabel *surnameLabel    = new TQLabel( "Surname:", this );
-        TQLabel *surnameDisplay  = new TQLabel( this );
-        TQLabel *salaryLabel     = new TQLabel( "Salary:", this );
-        TQLineEdit *salaryEdit   = new TQLineEdit( this );
+        TQLabel *forenameLabel   = new TQLabel( "Forename:", this );
+        TQLabel *forenameDisplay = new TQLabel( this );
+        TQLabel *surnameLabel    = new TQLabel( "Surname:", this );
+        TQLabel *surnameDisplay  = new TQLabel( this );
+        TQLabel *salaryLabel     = new TQLabel( "Salary:", this );
+        TQLineEdit *salaryEdit   = new TQLineEdit( this );
 
         TQGridLayout *grid = new TQGridLayout( this );
         grid->addWidget( forenameLabel,     0, 0 );
@@ -1055,57 +1055,57 @@ the code below can be automatically generated by 
         grid->addWidget( surnameDisplay,    1, 1 );
         grid->addWidget( salaryLabel,       2, 0 );
         grid->addWidget( salaryEdit,        2, 1 );
-        grid->activate();
-
-        TQSqlCursor staffCursor( "staff" );
-        staffCursor.select();
-        staffCursor.next();
-
-        TQSqlForm sqlForm( this );
-        sqlForm.setRecord( staffCursor.primeUpdate() );
-        sqlForm.insert( forenameDisplay, "forename" );
-        sqlForm.insert( surnameDisplay, "surname" );
-        sqlForm.insert( salaryEdit, "salary" );
-        sqlForm.readFields();
+        grid->activate();
+
+        TQSqlCursor staffCursor( "staff" );
+        staffCursor.select();
+        staffCursor.next();
+
+        TQSqlForm sqlForm( this );
+        sqlForm.setRecord( staffCursor.primeUpdate() );
+        sqlForm.insert( forenameDisplay, "forename" );
+        sqlForm.insert( surnameDisplay, "surname" );
+        sqlForm.insert( salaryEdit, "salary" );
+        sqlForm.readFields();
     }
 
     int main( int argc, char *argv[] )
     {
-        TQApplication app( argc, argv );
+        TQApplication app( argc, argv );
 
         if ( ! createConnections() ) return 1;
 
         FormDialog *formDialog = new FormDialog();
-        formDialog->show();
-        app.setMainWidget( formDialog );
+        formDialog->show();
+        app.setMainWidget( formDialog );
 
-        return app.exec();
+        return app.exec();
     }
 

From sql/overview/form1/main.cpp

We include the header files for the widgets that we need. We also -include qsqldatabase.h and qsqlcursor.h as usual, but we now add -qsqlform.h. -

The form will be presented as a dialog so we subclass TQDialog with -our own FormDialog class. We use a TQLineEdit for the salary so that +include ntqsqldatabase.h and ntqsqlcursor.h as usual, but we now add +ntqsqlform.h. +

The form will be presented as a dialog so we subclass TQDialog with +our own FormDialog class. We use a TQLineEdit for the salary so that the user can change it. All the widgets are laid out using a grid.

We create a cursor on the staff table, select all records and move to the first record. -

Now we create a TQSqlForm object and set the TQSqlForm's record buffer +

Now we create a TQSqlForm object and set the TQSqlForm's record buffer to the cursor's update buffer. For each widget that we wish to make data-aware we insert a pointer to the widget and the associated field -name into the TQSqlForm. Finally we call readFields() to populate the +name into the TQSqlForm. Finally we call readFields() to populate the widgets with data from the database via the cursor's buffer.

Displaying a Record in a Data Form

-

TQDataView is a Widget that can hold a read-only TQSqlForm. In -addition to TQSqlForm it offers the slot refresh( TQSqlRecord * ) so it -can easily be linked together with a TQDataTable to display a detailed +

TQDataView is a Widget that can hold a read-only TQSqlForm. In +addition to TQSqlForm it offers the slot refresh( TQSqlRecord * ) so it +can easily be linked together with a TQDataTable to display a detailed view of a record:

-    connect( myDataTable, SIGNAL( currentChanged( TQSqlRecord* ) ), 
-             myDataView, SLOT( refresh( TQSqlRecord* ) ) );
+    connect( myDataTable, SIGNAL( currentChanged( TQSqlRecord* ) ), 
+             myDataView, SLOT( refresh( TQSqlRecord* ) ) );
 

@@ -1115,7 +1115,7 @@ view of a record: differences.

-

    class FormDialog : public TQDialog
+
    class FormDialog : public TQDialog
     {
         Q_OBJECT
         public:
@@ -1124,15 +1124,15 @@ differences.
         public slots:
             void save();
         private:
-            TQSqlCursor staffCursor;
-            TQSqlForm *sqlForm;
-            TQSqlIndex idIndex;
+            TQSqlCursor staffCursor;
+            TQSqlForm *sqlForm;
+            TQSqlIndex idIndex;
     };
 

From sql/overview/form2/main.h

The save slot will be used for a button that the user can press to -confirm their update. We also hold pointers to the TQSqlCursor and the -TQSqlForm since they will need to be accessed outside the constructor. +confirm their update. We also hold pointers to the TQSqlCursor and the +TQSqlForm since they will need to be accessed outside the constructor.

        staffCursor.setTrimmed( "forename", TRUE );
@@ -1142,15 +1142,15 @@ confirm their update. We also hold pointers to the TQS
 right pad the fields are removed when the fields are retrieved.
 

Properties that we might wish to apply to fields, such as alignment and validation are achieved in the conventional way, for example, by -calling TQLineEdit::setAlignment() and TQLineEdit::setValidator(). -

        TQLineEdit   *forenameEdit  = new TQLineEdit( this );
+calling TQLineEdit::setAlignment() and TQLineEdit::setValidator().
+

        TQLineEdit   *forenameEdit  = new TQLineEdit( this );
 
-

        TQPushButton *saveButton    = new TQPushButton( "&Save", this );
-        connect( saveButton, SIGNAL(clicked()), this, SLOT(save()) );
+

        TQPushButton *saveButton    = new TQPushButton( "&Save", this );
+        connect( saveButton, SIGNAL(clicked()), this, SLOT(save()) );
 

The FormDialog constructor is similar to the one in the previous example. We have changed the forename and surname widgets to -TQLineEdits to make them editable and have added a TQPushButton +TQLineEdits to make them editable and have added a TQPushButton the user can click to save their updates.

        grid->addWidget( saveButton,    3, 0 );
 
@@ -1159,19 +1159,19 @@ the user can click to save their updates. staffCursor.select( idIndex ); staffCursor.first();
-

We create a TQSqlIndex object and then execute a select() using the +

We create a TQSqlIndex object and then execute a select() using the index. We then move to the first record in the result set. -

        sqlForm = new TQSqlForm( this );
-        sqlForm->setRecord( staffCursor.primeUpdate() );
+

        sqlForm = new TQSqlForm( this );
+        sqlForm->setRecord( staffCursor.primeUpdate() );
 
-

We create a new TQSqlForm object and set it's record buffer to the +

We create a new TQSqlForm object and set it's record buffer to the cursor's update buffer. -

        sqlForm->insert( forenameEdit, "forename" );
-        sqlForm->insert( surnameEdit, "surname" );
-        sqlForm->insert( salaryEdit, "salary" );
-        sqlForm->readFields();
+

        sqlForm->insert( forenameEdit, "forename" );
+        sqlForm->insert( surnameEdit, "surname" );
+        sqlForm->insert( salaryEdit, "salary" );
+        sqlForm->readFields();
 
-

Now we link the buffer's fields to the TQLineEdit controls. (In the +

Now we link the buffer's fields to the TQLineEdit controls. (In the previous example we linked the cursor's fields.) The edit controls are populated by the readFields() call as before.

    FormDialog::~FormDialog()
@@ -1184,28 +1184,28 @@ since they are children of the form and will be deleted by TQt at the
 right time.
 

    void FormDialog::save()
     {
-        sqlForm->writeFields();
+        sqlForm->writeFields();
         staffCursor.update();
         staffCursor.select( idIndex );
         staffCursor.first();
     }
 

Finally we add the save functionality for when the user presses the -save button. We write back the data from the widgets to the TQSqlRecord buffer with the writeFields() call. Then we update the +save button. We write back the data from the widgets to the TQSqlRecord buffer with the writeFields() call. Then we update the database with the updated version of the record with the cursor's update() function. At this point the cursor is no longer positioned at -a valid record so we reissue the select() call using our TQSqlIndex +a valid record so we reissue the select() call using our TQSqlIndex and move to the first record. -

TQDataBrowser and TQDataView are widgets which provide a great deal of -the above functionality. TQDataBrowser provides a data form which -allows editing of and navigation through a cursor's records. TQDataView provides a read only form for data in a cursor or database +

TQDataBrowser and TQDataView are widgets which provide a great deal of +the above functionality. TQDataBrowser provides a data form which +allows editing of and navigation through a cursor's records. TQDataView provides a read only form for data in a cursor or database record. See the class documentation or the TQt Designer manual for more information on using these widgets.

Link to sql/overview/form2/main.cpp

Custom Editor Widgets

-

TQSqlForm uses TQSqlPropertyMap to handle the transfer of data between +

TQSqlForm uses TQSqlPropertyMap to handle the transfer of data between widgets and database fields. Custom widgets can also be used in a form by installing a property map that contains information about the properties of the custom widget which should be used to transfer the @@ -1214,47 +1214,47 @@ data. we will only cover the differences here. The full source is in sql/overview/custom1/main.h and sql/overview/custom1/main.cpp

-

    class CustomEdit : public TQLineEdit
+
    class CustomEdit : public TQLineEdit
     {
         Q_OBJECT
         Q_PROPERTY( TQString upperLine READ upperLine WRITE setUpperLine )
         public:
-            CustomEdit( TQWidget *parent=0, const char *name=0 );
-            TQString upperLine() const;
-            void setUpperLine( const TQString &line );
+            CustomEdit( TQWidget *parent=0, const char *name=0 );
+            TQString upperLine() const;
+            void setUpperLine( const TQString &line );
         public slots:
-            void changed( const TQString &line );
+            void changed( const TQString &line );
         private:
-            TQString upperLineText;
+            TQString upperLineText;
     };
 
-

We've created a simple subclass of TQLineEdit and added a property, +

We've created a simple subclass of TQLineEdit and added a property, upperLineText, which will hold an uppercase version of the text. We also created a slot, changed(). -

            TQSqlPropertyMap *propMap;
+

            TQSqlPropertyMap *propMap;
 

We will be using a property map so we add a pointer to a property map to our FormDialog's private data.

-

    CustomEdit::CustomEdit( TQWidget *parent, const char *name ) :
-        TQLineEdit( parent, name )
+
    CustomEdit::CustomEdit( TQWidget *parent, const char *name ) :
+        TQLineEdit( parent, name )
     {
-        connect( this, SIGNAL(textChanged(const TQString &)),
-                 this, SLOT(changed(const TQString &)) );
+        connect( this, SIGNAL(textChanged(const TQString &)),
+                 this, SLOT(changed(const TQString &)) );
     }
 

In the CustomEdit constructor we use the TQLineEdit constructor and add a connection between the textChanged signal and our own changed slot. -

    void CustomEdit::changed( const TQString &line )
+

    void CustomEdit::changed( const TQString &line )
     {
         setUpperLine( line );
     }
 

The changed() slot calls our setUpperLine() function. -

    void CustomEdit::setUpperLine( const TQString &line )
+

    void CustomEdit::setUpperLine( const TQString &line )
     {
-        upperLineText = line.upper();
+        upperLineText = line.upper();
         setText( upperLineText );
     }
 
@@ -1268,18 +1268,18 @@ CustomEdit instances directly to database fields.

        CustomEdit  *surnameEdit    = new CustomEdit( this );
 

We use the same FormDialog as we did before, but this time replace two -of the TQLineEdit widgets with our own CustomEdit widgets. +of the TQLineEdit widgets with our own CustomEdit widgets.

Laying out the grid and setting up the cursor is the same as before. -

        propMap = new TQSqlPropertyMap;
-        propMap->insert( forenameEdit->className(), "upperLine" );
+

        propMap = new TQSqlPropertyMap;
+        propMap->insert( forenameEdit->className(), "upperLine" );
 

We create a new property map on the heap and register our CustomEdit class and its upperLine property with the property map. -

        sqlForm = new TQSqlForm( this );
-        sqlForm->setRecord( staffCursor->primeUpdate() );
-        sqlForm->installPropertyMap( propMap );
+

        sqlForm = new TQSqlForm( this );
+        sqlForm->setRecord( staffCursor->primeUpdate() );
+        sqlForm->installPropertyMap( propMap );
 
-

The final change is to install the property map into the TQSqlForm once +

The final change is to install the property map into the TQSqlForm once the TQSqlForm has been created. This passes responsibility for the property map's memory to TQSqlForm which itself is owned by the FormDialog, so TQt will delete them at the right time. @@ -1289,50 +1289,50 @@ our CustomEdit widget.

Custom Editor Widgets for Tables

-

We must reimpliment TQSqlEditorFactory to use custom editor widgets in +

We must reimpliment TQSqlEditorFactory to use custom editor widgets in tables. In the following example we will create a custom editor based -on TQComboBox and a TQSqlEditorFactory subclass to show how a TQDataTable +on TQComboBox and a TQSqlEditorFactory subclass to show how a TQDataTable can use a custom editor.

-

    class StatusPicker : public TQComboBox
+
    class StatusPicker : public TQComboBox
     {
         Q_OBJECT
         Q_PROPERTY( int statusid READ statusId WRITE setStatusId )
         public:
-            StatusPicker( TQWidget *parent=0, const char *name=0 );
+            StatusPicker( TQWidget *parent=0, const char *name=0 );
             int statusId() const;
             void setStatusId( int id );
         private:
-            TQMap< int, int > index2id;
+            TQMap< int, int > index2id;
     };
 

From sql/overview/table3/main.h

We create a property, statusid, and define our READ and WRITE methods for it. The statusid's in the status table will probably be different -from the combobox's indexes so we create a TQMap to map combobox indexes +from the combobox's indexes so we create a TQMap to map combobox indexes to/from the statusids that we will list in the combobox. -

    class CustomSqlEditorFactory : public TQSqlEditorFactory
+

    class CustomSqlEditorFactory : public TQSqlEditorFactory
     {
         Q_OBJECT
         public:
-            TQWidget *createEditor( TQWidget *parent, const TQSqlField *field );
+            TQWidget *createEditor( TQWidget *parent, const TQSqlField *field );
     };
 
-

We also need to subclass TQSqlEditorFactory declaring a createEditor() +

We also need to subclass TQSqlEditorFactory declaring a createEditor() function since that is the only function we need to reimplement.

-

    StatusPicker::StatusPicker( TQWidget *parent, const char *name )
-        : TQComboBox( parent, name )
+
    StatusPicker::StatusPicker( TQWidget *parent, const char *name )
+        : TQComboBox( parent, name )
     {
-        TQSqlCursor cur( "status" );
-        cur.select( cur.index( "name" ) );
+        TQSqlCursor cur( "status" );
+        cur.select( cur.index( "name" ) );
 
         int i = 0;
-        while ( cur.next() ) {
-            insertItem( cur.value( "name" ).toString(), i );
-            index2id[i] = cur.value( "id" ).toInt();
+        while ( cur.next() ) {
+            insertItem( cur.value( "name" ).toString(), i );
+            index2id[i] = cur.value( "id" ).toInt();
             i++;
         }
 

From sql/overview/table3/main.cpp @@ -1340,7 +1340,7 @@ function since that is the only function we need to reimplement.

In the StatusPicker's constructor we create a cursor over the status table indexed by the name field. We then iterate over each record in the status table inserting each name into the combobox. We store the -statusid for each name in the index2id TQMap using the same TQMap index as +statusid for each name in the index2id TQMap using the same TQMap index as the combobox index.

    int StatusPicker::statusId() const
     {
@@ -1367,85 +1367,85 @@ index2id TQMap. We compare each index2id element's data (statusid) to
 the id parameter's value. If we have a match we set the combobox's
 current item to the index2id element's key (the combobox index), and
 leave the loop.
-

When the user edits the status field in the TQDataTable they will be +

When the user edits the status field in the TQDataTable they will be presented with a combobox of valid status names taken from the status table. However the status displayed is still the raw statusid. To display the status name when the field isn't being edited requires us to subclass TQDataTable and reimplement the paintField() function.

-

    class CustomTable : public TQDataTable
+
    class CustomTable : public TQDataTable
     {
         Q_OBJECT
     public:
         CustomTable(
-                TQSqlCursor *cursor, bool autoPopulate = FALSE,
-                TQWidget * parent = 0, const char * name = 0 ) :
-            TQDataTable( cursor, autoPopulate, parent, name ) {}
+                TQSqlCursor *cursor, bool autoPopulate = FALSE,
+                TQWidget * parent = 0, const char * name = 0 ) :
+            TQDataTable( cursor, autoPopulate, parent, name ) {}
         void paintField(
-                TQPainter * p, const TQSqlField* field, const TQRect & cr, bool );
+                TQPainter * p, const TQSqlField* field, const TQRect & cr, bool );
 
     };
 

From sql/overview/table4/main.h

-

We simply call the original TQDataTable constructor without changing +

We simply call the original TQDataTable constructor without changing anything. We also declare the paintField function.

-

    void CustomTable::paintField( TQPainter * p, const TQSqlField* field,
-                                  const TQRect & cr, bool b)
+
    void CustomTable::paintField( TQPainter * p, const TQSqlField* field,
+                                  const TQRect & cr, bool b)
     {
         if ( !field )
             return;
-        if ( field->name() == "statusid" ) {
-            TQSqlQuery query( "SELECT name FROM status WHERE id=" +
-                         field->value().toString() );
-            TQString text;
-            if ( query.next() ) {
-                text = query.value( 0 ).toString();
+        if ( field->name() == "statusid" ) {
+            TQSqlQuery query( "SELECT name FROM status WHERE id=" +
+                         field->value().toString() );
+            TQString text;
+            if ( query.next() ) {
+                text = query.value( 0 ).toString();
             }
-            p->drawText( 2,2, cr.width()-4, cr.height()-4, fieldAlignment( field ), text );
+            p->drawText( 2,2, cr.width()-4, cr.height()-4, fieldAlignment( field ), text );
         }
         else {
-            TQDataTable::paintField( p, field, cr, b) ;
+            TQDataTable::paintField( p, field, cr, b) ;
         }
 

From sql/overview/table4/main.cpp

-

The paintField code is based on TQDataTable's source code. We need to +

The paintField code is based on TQDataTable's source code. We need to make three changes. Firstly add an if clause field->name() == "statusid" and look up the textual value for the id with a -straighforward TQSqlQuery. Secondly call the superclass to handle other +straighforward TQSqlQuery. Secondly call the superclass to handle other fields. The last change is in our main function where we change staffTable from being a TQDataTable to being a CustomTable.

-

Subclassing TQSqlCursor +

Subclassing TQSqlCursor

-

    #include <qapplication.h>
-    #include <qsqldatabase.h>
-    #include <qsqlcursor.h>
-    #include <qdatatable.h>
+
    #include <ntqapplication.h>
+    #include <ntqsqldatabase.h>
+    #include <ntqsqlcursor.h>
+    #include <ntqdatatable.h>
     #include "../connection.h"
 
     int main( int argc, char *argv[] )
     {
-        TQApplication app( argc, argv );
+        TQApplication app( argc, argv );
 
         if ( createConnections() ) {
-            TQSqlCursor invoiceItemCursor( "invoiceitem" );
+            TQSqlCursor invoiceItemCursor( "invoiceitem" );
 
-            TQDataTable *invoiceItemTable = new TQDataTable( &invoiceItemCursor );
+            TQDataTable *invoiceItemTable = new TQDataTable( &invoiceItemCursor );
 
-            app.setMainWidget( invoiceItemTable );
+            app.setMainWidget( invoiceItemTable );
 
-            invoiceItemTable->addColumn( "pricesid", "PriceID" );
-            invoiceItemTable->addColumn( "quantity", "Quantity" );
-            invoiceItemTable->addColumn( "paiddate", "Paid" );
+            invoiceItemTable->addColumn( "pricesid", "PriceID" );
+            invoiceItemTable->addColumn( "quantity", "Quantity" );
+            invoiceItemTable->addColumn( "paiddate", "Paid" );
 
-            invoiceItemTable->refresh();
-            invoiceItemTable->show();
+            invoiceItemTable->refresh();
+            invoiceItemTable->show();
 
-            return app.exec();
+            return app.exec();
         }
 
         return 1;
@@ -1453,7 +1453,7 @@ staffTable from being a TQDataTable to being a CustomTable.
 

From sql/overview/subclass1/main.cpp

This example is very similar to the table1 example presented earlier. We -create a cursor, add the fields and their display labels to a TQDataTable, +create a cursor, add the fields and their display labels to a TQDataTable, call refresh() to load the data and call show() to show the widget.

Unfortunately this example is unsatisfactory. It is tedious to set the table name and any custom characteristics for the fields every time we @@ -1465,18 +1465,18 @@ useful (or even essential for primary keys) if we could default some of the values when the user adds a new record.

-

    class InvoiceItemCursor : public TQSqlCursor
+
    class InvoiceItemCursor : public TQSqlCursor
     {
         public:
             InvoiceItemCursor();
     };
 

From sql/overview/subclass2/main.h

-

We have created a separate header file and subclassed TQSqlCursor. +

We have created a separate header file and subclassed TQSqlCursor.

    InvoiceItemCursor::InvoiceItemCursor() :
-        TQSqlCursor( "invoiceitem" )
+        TQSqlCursor( "invoiceitem" )
     {
         // NOOP
     }
@@ -1493,7 +1493,7 @@ an InvoiceItemCursor instead of a generic TQSqlCursor.
 

        protected:
-            TQVariant calculateField( const TQString & name );
+            TQVariant calculateField( const TQString & name );
 

From sql/overview/subclass3/main.h

The change in the header file is minimal: we simply add the signature @@ -1501,34 +1501,34 @@ of the calculateField() function since we will be reimplementing it.

    InvoiceItemCursor::InvoiceItemCursor() :
-        TQSqlCursor( "invoiceitem" )
+        TQSqlCursor( "invoiceitem" )
     {
         TQSqlFieldInfo productName( "productname", TQVariant::String );
-        append( productName );
-        setCalculated( productName.name(), TRUE );
+        append( productName );
+        setCalculated( productName.name(), TRUE );
     }
 
-    TQVariant InvoiceItemCursor::calculateField( const TQString & name )
+    TQVariant InvoiceItemCursor::calculateField( const TQString & name )
     {
         if ( name == "productname" ) {
-            TQSqlQuery query( "SELECT name FROM prices WHERE id=" +
-                         field( "pricesid" )->value().toString() );
-            if ( query.next() )
-                return query.value( 0 );
+            TQSqlQuery query( "SELECT name FROM prices WHERE id=" +
+                         field( "pricesid" )->value().toString() );
+            if ( query.next() )
+                return query.value( 0 );
         }
 
-        return TQVariant( TQString::null );
+        return TQVariant( TQString::null );
     }
 

From sql/overview/subclass3/main.cpp

We have changed the InvoiceItemCursor constructor. We now create a new -TQSqlField called productname and append this to the +TQSqlField called productname and append this to the InvoiceItemCursor's set of fields. We call setCalculated() on productname to identify it as a calculated field. The first argument to setCalculated() is the field name, the second a bool which if TRUE signifies that calculateField() must be called to get the field's value. -

            invoiceItemTable->addColumn( "productname", "Product" );
+

            invoiceItemTable->addColumn( "productname", "Product" );
 

We add our new fields with addColumn() which adds them to the form and sets their display names. @@ -1545,46 +1545,46 @@ function require some simple expansion. We'll look at each in turn.

    InvoiceItemCursor::InvoiceItemCursor() :
-        TQSqlCursor( "invoiceitem" )
+        TQSqlCursor( "invoiceitem" )
     {
         TQSqlFieldInfo productName( "productname", TQVariant::String );
-        append( productName );
-        setCalculated( productName.name(), TRUE );
+        append( productName );
+        setCalculated( productName.name(), TRUE );
 
         TQSqlFieldInfo productPrice( "price", TQVariant::Double );
-        append( productPrice );
-        setCalculated( productPrice.name(), TRUE );
+        append( productPrice );
+        setCalculated( productPrice.name(), TRUE );
 
         TQSqlFieldInfo productCost( "cost", TQVariant::Double );
-        append( productCost );
-        setCalculated( productCost.name(), TRUE );
+        append( productCost );
+        setCalculated( productCost.name(), TRUE );
     }
 

From sql/overview/subclass4/main.cpp

We create two extra fields, price and cost, and append them to the cursor's set of fields. Both are registered as calculated fields with calls to setCalculated(). -

    TQVariant InvoiceItemCursor::calculateField( const TQString & name )
+

    TQVariant InvoiceItemCursor::calculateField( const TQString & name )
     {
 
         if ( name == "productname" ) {
-            TQSqlQuery query( "SELECT name FROM prices WHERE id=" +
-                         field( "pricesid" )->value().toString() );
-            if ( query.next() )
-                return query.value( 0 );
+            TQSqlQuery query( "SELECT name FROM prices WHERE id=" +
+                         field( "pricesid" )->value().toString() );
+            if ( query.next() )
+                return query.value( 0 );
         }
         else if ( name == "price" ) {
-            TQSqlQuery query( "SELECT price FROM prices WHERE id=" +
-                         field( "pricesid" )->value().toString() );
-            if ( query.next() )
-                return query.value( 0 );
+            TQSqlQuery query( "SELECT price FROM prices WHERE id=" +
+                         field( "pricesid" )->value().toString() );
+            if ( query.next() )
+                return query.value( 0 );
         }
         else if ( name == "cost" ) {
-            TQSqlQuery query( "SELECT price FROM prices WHERE id=" +
-                         field( "pricesid" )->value().toString() );
-            if ( query.next() )
-                return TQVariant( query.value( 0 ).toDouble() *
-                                 value( "quantity").toDouble() );
+            TQSqlQuery query( "SELECT price FROM prices WHERE id=" +
+                         field( "pricesid" )->value().toString() );
+            if ( query.next() )
+                return TQVariant( query.value( 0 ).toDouble() *
+                                 value( "quantity").toDouble() );
         }
 
         return TQVariant( TQString::null );
@@ -1596,7 +1596,7 @@ must calculate the value of three different fields. The productname
 and price fields are produced by looking up the corresponding values
 in the prices table keyed by pricesid. The cost field is calculated
 simply by multiplying the price by the quantity. Note that we cast the
-cost to a TQVariant since that is the type that calculateField() must
+cost to a TQVariant since that is the type that calculateField() must
 return.
 

We've written three separate queries rather than one to make the example more like a real application where it is more likely that each @@ -1605,7 +1605,7 @@ calculated field would be a lookup against a different table or view. user attempts to insert a new record.

-

            TQSqlRecord *primeInsert();
+
            TQSqlRecord *primeInsert();
 

From sql/overview/subclass5/main.h

We declare our own primeInsert() function since we will need to @@ -1613,14 +1613,14 @@ reimplement this.

The constructor and the calculateField() function remain unchanged.

-

    TQSqlRecord *InvoiceItemCursor::primeInsert()
+
    TQSqlRecord *InvoiceItemCursor::primeInsert()
     {
-        TQSqlRecord *buffer = editBuffer();
-        TQSqlQuery query( "SELECT NEXTVAL( 'invoiceitem_seq' )" );
-        if ( query.next() )
-            buffer->setValue( "id", query.value( 0 ) );
-        buffer->setValue( "paiddate", TQDate::currentDate() );
-        buffer->setValue( "quantity", 1 );
+        TQSqlRecord *buffer = editBuffer();
+        TQSqlQuery query( "SELECT NEXTVAL( 'invoiceitem_seq' )" );
+        if ( query.next() )
+            buffer->setValue( "id", query.value( 0 ) );
+        buffer->setValue( "paiddate", TQDate::currentDate() );
+        buffer->setValue( "quantity", 1 );
 
         return buffer;
     }
-- 
cgit v1.2.1