diff options
Diffstat (limited to 'examples/sql')
86 files changed, 2217 insertions, 0 deletions
diff --git a/examples/sql/blob/blob.pro b/examples/sql/blob/blob.pro new file mode 100644 index 000000000..9240f2e42 --- /dev/null +++ b/examples/sql/blob/blob.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = blob + +CONFIG += qt warn_on release +win32:CONFIG += console + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp +INTERFACES = diff --git a/examples/sql/blob/main.cpp b/examples/sql/blob/main.cpp new file mode 100644 index 000000000..e66e9aaf5 --- /dev/null +++ b/examples/sql/blob/main.cpp @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include <qsqlcursor.h> +#include <qfile.h> + +#define DRIVER "TQPSQL7" /* see the TQt SQL documentation for a list of available drivers */ +#define DATABASE "" /* the name of your database */ +#define USER "" /* user name with appropriate rights */ +#define PASSWORD "" /* password for USER */ +#define HOST "" /* host on which the database is running */ + +int main( int argc, char ** argv ) +{ + + TQApplication a( argc, argv, FALSE ); + TQSqlDatabase * db = TQSqlDatabase::addDatabase( DRIVER ); + db->setDatabaseName( DATABASE ); + db->setUserName( USER ); + db->setPassword( PASSWORD ); + db->setHostName( HOST ); + if ( !db->open() ) { + qWarning( db->lastError().databaseText() ); + return 1; + } + + if ( argc < 2 ) { + qWarning( "Usage: %s <filename>", argv[0] ); + return 1; + } + + // read a file which we want to insert into the database + TQFile f( argv[1] ); + if ( !f.open( IO_ReadOnly ) ) { + qWarning( "Unable to open data file '%s' - exiting", argv[1] ); + return 1; + } + TQByteArray binaryData = f.readAll(); + qWarning( "Data size: %d", binaryData.size() ); + + // create a table with a binary field + TQSqlQuery q; + if ( !q.exec( "CREATE TABLE blobexample ( id INT PRIMARY KEY, binfield LONGBLOB )" ) ) { + qWarning( "Unable to create table - exiting" ); + return 1; + } + + // insert a BLOB into the table + if ( !q.prepare( "INSERT INTO blobexample ( id, binfield ) VALUES ( ?, ? )" ) ) { + qWarning( "Unable to prepare query - exiting" ); + return 1; + } + q.bindValue( 0, 1 ); + q.bindValue( 1, binaryData ); + if ( !q.exec() ) { + qWarning( "Unable to execute prepared query - exiting" ); + return 1; + } + + // read the BLOB back from the database + if ( !q.exec( "SELECT id, binfield FROM blobexample" ) ) { + qWarning( "Unable to execute query - exiting" ); + return 1; + } + qWarning( "\nTQSqlQuery:" ); + while ( q.next() ) { + qWarning( "BLOB id: %d", q.value( 0 ).toInt() ); + qWarning( "BLOB size: %d", q.value( 1 ).toByteArray().size() ); + } + + // write another BLOB using TQSqlCursor + TQSqlCursor cur( "blobexample" ); + TQSqlRecord * r = cur.primeInsert(); + r->setValue( "id", 2 ); + r->setValue( "binfield", binaryData ); + if ( !cur.insert() ) { + qWarning( "Unable to insert BLOB using TQSqlCursor - exiting" ); + return 1; + } + + // read the BLOBs back using TQSqlCursor + if ( !cur.select() ) { + qWarning( "Unable retrieve blobexample table using TQSqlCursor - exiting" ); + return 1; + } + qWarning( "\nTQSqlCursor:" ); + while ( cur.next() ) { + qWarning( "BLOB id: %d", cur.value( "id" ).toInt() ); + qWarning( "BLOB size: %d", cur.value( "binfield" ).toByteArray().size() ); + } + + if ( !q.exec( "DROP TABLE blobexample" ) ) { + qWarning( "Unable to drop table - exiting" ); + return 1; + } + return 0; +} diff --git a/examples/sql/overview/basicbrowsing/README b/examples/sql/overview/basicbrowsing/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/basicbrowsing/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/basicbrowsing/basicbrowsing.pro b/examples/sql/overview/basicbrowsing/basicbrowsing.pro new file mode 100644 index 000000000..b7245c992 --- /dev/null +++ b/examples/sql/overview/basicbrowsing/basicbrowsing.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp + diff --git a/examples/sql/overview/basicbrowsing/main.cpp b/examples/sql/overview/basicbrowsing/main.cpp new file mode 100644 index 000000000..a63182444 --- /dev/null +++ b/examples/sql/overview/basicbrowsing/main.cpp @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + 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() + "' )" ); + } + } + } + + return 0; +} diff --git a/examples/sql/overview/basicbrowsing2/README b/examples/sql/overview/basicbrowsing2/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/basicbrowsing2/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/basicbrowsing2/basicbrowsing2.pro b/examples/sql/overview/basicbrowsing2/basicbrowsing2.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/basicbrowsing2/basicbrowsing2.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/basicbrowsing2/main.cpp b/examples/sql/overview/basicbrowsing2/main.cpp new file mode 100644 index 000000000..5f7cbcb9f --- /dev/null +++ b/examples/sql/overview/basicbrowsing2/main.cpp @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + 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 ); + 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(); + } + } + } + + return 0; +} + + + + + diff --git a/examples/sql/overview/basicdatamanip/README b/examples/sql/overview/basicdatamanip/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/basicdatamanip/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/basicdatamanip/basicdatamanip.pro b/examples/sql/overview/basicdatamanip/basicdatamanip.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/basicdatamanip/basicdatamanip.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/basicdatamanip/main.cpp b/examples/sql/overview/basicdatamanip/main.cpp new file mode 100644 index 000000000..7777f9813 --- /dev/null +++ b/examples/sql/overview/basicdatamanip/main.cpp @@ -0,0 +1,40 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +bool createConnections(); + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + int rows = 0; + + if ( createConnections() ) { + TQSqlQuery query( "INSERT INTO staff ( id, forename, surname, salary ) " + "VALUES ( 1155, 'Ginger', 'Davis', 50000 )" ); + 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() ; + } + + return ( rows == 3 ) ? 0 : 1; +} + + + diff --git a/examples/sql/overview/connect1/README b/examples/sql/overview/connect1/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/connect1/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/connect1/connect1.pro b/examples/sql/overview/connect1/connect1.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/connect1/connect1.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/connect1/main.cpp b/examples/sql/overview/connect1/main.cpp new file mode 100644 index 000000000..16f3eeab2 --- /dev/null +++ b/examples/sql/overview/connect1/main.cpp @@ -0,0 +1,31 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include "../connection.h" + + +int main( int argc, char *argv[] ) +{ + 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 ); + + if ( defaultDB->open() ) { + // Database successfully opened; we can now issue SQL commands. + } + + return 0; +} + diff --git a/examples/sql/overview/connection.cpp b/examples/sql/overview/connection.cpp new file mode 100644 index 000000000..0bbc7116c --- /dev/null +++ b/examples/sql/overview/connection.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ +#include <qsqldatabase.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() ); + 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() ); + 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), " + "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), " + "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, " + "pricesid integer, quantity integer, paiddate date)"); + + TQSqlQuery q2(TQString::null, oracle); + q2.exec("create table people (id integer primary key, name char(40))"); + + return TRUE; +} diff --git a/examples/sql/overview/connection.h b/examples/sql/overview/connection.h new file mode 100644 index 000000000..c2c5195b6 --- /dev/null +++ b/examples/sql/overview/connection.h @@ -0,0 +1,24 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +// Enter your connection info here + +#define DB_SALES_DRIVER "TQSQLITE" +#define DB_SALES_DBNAME ":memory:" +#define DB_SALES_USER "" +#define DB_SALES_PASSWD "" +#define DB_SALES_HOST "" + +#define DB_ORDERS_DRIVER "TQSQLITE" +#define DB_ORDERS_DBNAME ":memory:" +#define DB_ORDERS_USER "" +#define DB_ORDERS_PASSWD "" +#define DB_ORDERS_HOST "" + +bool createConnections(); diff --git a/examples/sql/overview/create_connections/README b/examples/sql/overview/create_connections/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/create_connections/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/create_connections/create_connections.pro b/examples/sql/overview/create_connections/create_connections.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/create_connections/create_connections.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/create_connections/main.cpp b/examples/sql/overview/create_connections/main.cpp new file mode 100644 index 000000000..c4b3a9b71 --- /dev/null +++ b/examples/sql/overview/create_connections/main.cpp @@ -0,0 +1,26 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + // Databases successfully opened; get pointers to them: + TQSqlDatabase *oracledb = TQSqlDatabase::database( "ORACLE" ); + // Now we can now issue SQL commands to the oracle connection + // or to the default connection + } + + return 0; +} diff --git a/examples/sql/overview/custom1/custom1.pro b/examples/sql/overview/custom1/custom1.pro new file mode 100644 index 000000000..c490bedd0 --- /dev/null +++ b/examples/sql/overview/custom1/custom1.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = main.h +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/custom1/main.cpp b/examples/sql/overview/custom1/main.cpp new file mode 100644 index 000000000..808444c7c --- /dev/null +++ b/examples/sql/overview/custom1/main.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" + + +CustomEdit::CustomEdit( TQWidget *parent, const char *name ) : + TQLineEdit( parent, name ) +{ + connect( this, SIGNAL(textChanged(const TQString &)), + this, SLOT(changed(const TQString &)) ); +} + + +void CustomEdit::changed( const TQString &line ) +{ + setUpperLine( line ); +} + + +void CustomEdit::setUpperLine( const TQString &line ) +{ + upperLineText = line.upper(); + setText( upperLineText ); +} + + +TQString CustomEdit::upperLine() const +{ + return upperLineText; +} + + +FormDialog::FormDialog() +{ + TQLabel *forenameLabel = new TQLabel( "Forename:", this ); + CustomEdit *forenameEdit = new CustomEdit( this ); + TQLabel *surnameLabel = new TQLabel( "Surname:", this ); + CustomEdit *surnameEdit = new CustomEdit( this ); + TQLabel *salaryLabel = new TQLabel( "Salary:", this ); + TQLineEdit *salaryEdit = new TQLineEdit( this ); + salaryEdit->setAlignment( TQt::AlignRight ); + TQPushButton *saveButton = new TQPushButton( "&Save", this ); + connect( saveButton, SIGNAL(clicked()), this, SLOT(save()) ); + + TQGridLayout *grid = new TQGridLayout( this ); + grid->addWidget( forenameLabel, 0, 0 ); + grid->addWidget( forenameEdit, 0, 1 ); + grid->addWidget( surnameLabel, 1, 0 ); + grid->addWidget( surnameEdit, 1, 1 ); + grid->addWidget( salaryLabel, 2, 0 ); + grid->addWidget( salaryEdit, 2, 1 ); + grid->addWidget( saveButton, 3, 0 ); + grid->activate(); + + staffCursor = new TQSqlCursor( "staff" ); + staffCursor->setTrimmed( "forename", TRUE ); + staffCursor->setTrimmed( "surname", TRUE ); + idIndex = staffCursor->index( "id" ); + staffCursor->select( idIndex ); + staffCursor->first(); + + propMap = new TQSqlPropertyMap; + propMap->insert( forenameEdit->className(), "upperLine" ); + + sqlForm = new TQSqlForm( this ); + sqlForm->setRecord( staffCursor->primeUpdate() ); + sqlForm->installPropertyMap( propMap ); + sqlForm->insert( forenameEdit, "forename" ); + sqlForm->insert( surnameEdit, "surname" ); + sqlForm->insert( salaryEdit, "salary" ); + sqlForm->readFields(); +} + + +FormDialog::~FormDialog() +{ + delete staffCursor; +} + + +void FormDialog::save() +{ + sqlForm->writeFields(); + staffCursor->update(); + staffCursor->select( idIndex ); + staffCursor->first(); +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( ! createConnections() ) + return 1; + + FormDialog *formDialog = new FormDialog(); + formDialog->show(); + app.setMainWidget( formDialog ); + + return app.exec(); +} diff --git a/examples/sql/overview/custom1/main.h b/examples/sql/overview/custom1/main.h new file mode 100644 index 000000000..191672245 --- /dev/null +++ b/examples/sql/overview/custom1/main.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qdialog.h> +#include <qlabel.h> +#include <qlayout.h> +#include <qlineedit.h> +#include <qpushbutton.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqlform.h> +#include <qsqlpropertymap.h> +#include "../connection.h" + +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 ); + public slots: + void changed( const TQString &line ); + private: + TQString upperLineText; +}; + + +class FormDialog : public TQDialog +{ + Q_OBJECT + public: + FormDialog(); + ~FormDialog(); + public slots: + void save(); + private: + TQSqlCursor *staffCursor; + TQSqlForm *sqlForm; + TQSqlPropertyMap *propMap; + TQSqlIndex idIndex; +}; + + diff --git a/examples/sql/overview/delete/README b/examples/sql/overview/delete/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/delete/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/delete/delete.pro b/examples/sql/overview/delete/delete.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/delete/delete.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/delete/main.cpp b/examples/sql/overview/delete/main.cpp new file mode 100644 index 000000000..ebe2108fa --- /dev/null +++ b/examples/sql/overview/delete/main.cpp @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + TQSqlCursor cur( "prices" ); + cur.select( "id=999" ); + if ( cur.next() ) { + cur.primeDelete(); + cur.del(); + } + } + + return 0; +} diff --git a/examples/sql/overview/extract/README b/examples/sql/overview/extract/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/extract/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/extract/extract.pro b/examples/sql/overview/extract/extract.pro new file mode 100644 index 000000000..c517268f4 --- /dev/null +++ b/examples/sql/overview/extract/extract.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH =../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/extract/main.cpp b/examples/sql/overview/extract/main.cpp new file mode 100644 index 000000000..191601c82 --- /dev/null +++ b/examples/sql/overview/extract/main.cpp @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + 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" ); + + 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 ); + } + } + + return 0; +} diff --git a/examples/sql/overview/form1/form1.pro b/examples/sql/overview/form1/form1.pro new file mode 100644 index 000000000..a6cd8cb47 --- /dev/null +++ b/examples/sql/overview/form1/form1.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/form1/main.cpp b/examples/sql/overview/form1/main.cpp new file mode 100644 index 000000000..8f1c05b0e --- /dev/null +++ b/examples/sql/overview/form1/main.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#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 "../connection.h" + +class FormDialog : public TQDialog +{ + public: + FormDialog(); +}; + + +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 ); + + TQGridLayout *grid = new TQGridLayout( this ); + grid->addWidget( forenameLabel, 0, 0 ); + grid->addWidget( forenameDisplay, 0, 1 ); + grid->addWidget( surnameLabel, 1, 0 ); + 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(); +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( ! createConnections() ) return 1; + + FormDialog *formDialog = new FormDialog(); + formDialog->show(); + app.setMainWidget( formDialog ); + + return app.exec(); +} diff --git a/examples/sql/overview/form2/form2.pro b/examples/sql/overview/form2/form2.pro new file mode 100644 index 000000000..c490bedd0 --- /dev/null +++ b/examples/sql/overview/form2/form2.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = main.h +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/form2/main.cpp b/examples/sql/overview/form2/main.cpp new file mode 100644 index 000000000..2d5420bdc --- /dev/null +++ b/examples/sql/overview/form2/main.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" + +FormDialog::FormDialog() + : staffCursor( "staff" ) +{ + staffCursor.setTrimmed( "forename", TRUE ); + staffCursor.setTrimmed( "surname", TRUE ); + + TQLabel *forenameLabel = new TQLabel( "Forename:", this ); + TQLineEdit *forenameEdit = new TQLineEdit( this ); + TQLabel *surnameLabel = new TQLabel( "Surname:", this ); + TQLineEdit *surnameEdit = new TQLineEdit( this ); + TQLabel *salaryLabel = new TQLabel( "Salary:", this ); + TQLineEdit *salaryEdit = new TQLineEdit( this ); + TQPushButton *saveButton = new TQPushButton( "&Save", this ); + connect( saveButton, SIGNAL(clicked()), this, SLOT(save()) ); + + TQGridLayout *grid = new TQGridLayout( this ); + grid->addWidget( forenameLabel, 0, 0 ); + grid->addWidget( forenameEdit, 0, 1 ); + grid->addWidget( surnameLabel, 1, 0 ); + grid->addWidget( surnameEdit, 1, 1 ); + grid->addWidget( salaryLabel, 2, 0 ); + grid->addWidget( salaryEdit, 2, 1 ); + grid->addWidget( saveButton, 3, 0 ); + grid->activate(); + + idIndex = staffCursor.index( "id" ); + staffCursor.select( idIndex ); + staffCursor.first(); + + sqlForm = new TQSqlForm( this ); + sqlForm->setRecord( staffCursor.primeUpdate() ); + sqlForm->insert( forenameEdit, "forename" ); + sqlForm->insert( surnameEdit, "surname" ); + sqlForm->insert( salaryEdit, "salary" ); + sqlForm->readFields(); +} + + +FormDialog::~FormDialog() +{ + +} + + +void FormDialog::save() +{ + sqlForm->writeFields(); + staffCursor.update(); + staffCursor.select( idIndex ); + staffCursor.first(); +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( ! createConnections() ) + return 1; + + FormDialog *formDialog = new FormDialog(); + formDialog->show(); + app.setMainWidget( formDialog ); + + return app.exec(); +} diff --git a/examples/sql/overview/form2/main.h b/examples/sql/overview/form2/main.h new file mode 100644 index 000000000..31a0f66cd --- /dev/null +++ b/examples/sql/overview/form2/main.h @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qdialog.h> +#include <qlabel.h> +#include <qlayout.h> +#include <qlineedit.h> +#include <qpushbutton.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqlform.h> +#include "../connection.h" + +class FormDialog : public TQDialog +{ + Q_OBJECT + public: + FormDialog(); + ~FormDialog(); + public slots: + void save(); + private: + TQSqlCursor staffCursor; + TQSqlForm *sqlForm; + TQSqlIndex idIndex; +}; diff --git a/examples/sql/overview/insert/README b/examples/sql/overview/insert/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/insert/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/insert/insert.pro b/examples/sql/overview/insert/insert.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/insert/insert.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/insert/main.cpp b/examples/sql/overview/insert/main.cpp new file mode 100644 index 000000000..47276819f --- /dev/null +++ b/examples/sql/overview/insert/main.cpp @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + int count = 0; + 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(); + id++; + } + } + + return 0; +} diff --git a/examples/sql/overview/insert2/insert2.pro b/examples/sql/overview/insert2/insert2.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/insert2/insert2.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/insert2/main.cpp b/examples/sql/overview/insert2/main.cpp new file mode 100644 index 000000000..800a94464 --- /dev/null +++ b/examples/sql/overview/insert2/main.cpp @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + TQSqlCursor cur( "prices" ); + TQSqlRecord *buffer = cur.primeInsert(); + buffer->setValue( "id", 53981 ); + buffer->setValue( "name", "Thingy" ); + buffer->setValue( "price", 105.75 ); + cur.insert(); + } + + return 0; +} diff --git a/examples/sql/overview/navigating/README b/examples/sql/overview/navigating/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/navigating/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/navigating/main.cpp b/examples/sql/overview/navigating/main.cpp new file mode 100644 index 000000000..519aa2698 --- /dev/null +++ b/examples/sql/overview/navigating/main.cpp @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + 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 + } + + return 0; +} diff --git a/examples/sql/overview/navigating/navigating.pro b/examples/sql/overview/navigating/navigating.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/navigating/navigating.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/order1/README b/examples/sql/overview/order1/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/order1/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/order1/main.cpp b/examples/sql/overview/order1/main.cpp new file mode 100644 index 000000000..7b185d459 --- /dev/null +++ b/examples/sql/overview/order1/main.cpp @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + TQSqlCursor cur( "staff" ); + TQStringList fields = TQStringList() << "surname" << "forename"; + TQSqlIndex order = cur.index( fields ); + cur.select( order ); + while ( cur.next() ) { + qDebug( cur.value( "id" ).toString() + ": " + + cur.value( "surname" ).toString() + " " + + cur.value( "forename" ).toString() ); + } + } + + return 0; +} diff --git a/examples/sql/overview/order1/order1.pro b/examples/sql/overview/order1/order1.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/order1/order1.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/order2/README b/examples/sql/overview/order2/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/order2/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/order2/main.cpp b/examples/sql/overview/order2/main.cpp new file mode 100644 index 000000000..9165d2a9f --- /dev/null +++ b/examples/sql/overview/order2/main.cpp @@ -0,0 +1,34 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + 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() ) { + qDebug( cur.value( "id" ).toString() + ": " + + cur.value( "surname" ).toString() + " " + + cur.value( "forename" ).toString() ); + } + } + + return 0; +} diff --git a/examples/sql/overview/order2/order2.pro b/examples/sql/overview/order2/order2.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/order2/order2.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/overview.pro b/examples/sql/overview/overview.pro new file mode 100644 index 000000000..59be65100 --- /dev/null +++ b/examples/sql/overview/overview.pro @@ -0,0 +1,33 @@ +TEMPLATE = subdirs + +CONFIG += ordered + +REQUIRES = full-config + +SUBDIRS = basicbrowsing \ + basicbrowsing2 \ + basicdatamanip \ + connect1 \ + create_connections \ + custom1 \ + delete \ + extract \ + form1 \ + form2 \ + insert \ + insert2 \ + navigating \ + order1 \ + order2 \ + retrieve1 \ + retrieve2 \ + subclass1 \ + subclass2 \ + subclass3 \ + subclass4 \ + subclass5 \ + table1 \ + table2 \ + table3 \ + table4 \ + update diff --git a/examples/sql/overview/retrieve1/README b/examples/sql/overview/retrieve1/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/retrieve1/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/retrieve1/main.cpp b/examples/sql/overview/retrieve1/main.cpp new file mode 100644 index 000000000..956f8a17f --- /dev/null +++ b/examples/sql/overview/retrieve1/main.cpp @@ -0,0 +1,30 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + TQSqlQuery query( "SELECT id, surname FROM staff" ); + if ( query.isActive() ) { + while ( query.next() ) { + qDebug( query.value(0).toString() + ": " + + query.value(1).toString() ); + } + } + } + + return 0; +} diff --git a/examples/sql/overview/retrieve1/retrieve1.pro b/examples/sql/overview/retrieve1/retrieve1.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/retrieve1/retrieve1.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/retrieve2/README b/examples/sql/overview/retrieve2/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/retrieve2/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/retrieve2/main.cpp b/examples/sql/overview/retrieve2/main.cpp new file mode 100644 index 000000000..4cb0150be --- /dev/null +++ b/examples/sql/overview/retrieve2/main.cpp @@ -0,0 +1,30 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *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() ); + } + } + + return 0; +} diff --git a/examples/sql/overview/retrieve2/retrieve2.pro b/examples/sql/overview/retrieve2/retrieve2.pro new file mode 100644 index 000000000..c58585ea2 --- /dev/null +++ b/examples/sql/overview/retrieve2/retrieve2.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass1/main.cpp b/examples/sql/overview/subclass1/main.cpp new file mode 100644 index 000000000..476ebd898 --- /dev/null +++ b/examples/sql/overview/subclass1/main.cpp @@ -0,0 +1,38 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qdatatable.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + TQSqlCursor invoiceItemCursor( "invoiceitem" ); + + TQDataTable *invoiceItemTable = new TQDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "pricesid", "PriceID" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass1/subclass1.pro b/examples/sql/overview/subclass1/subclass1.pro new file mode 100644 index 000000000..a6cd8cb47 --- /dev/null +++ b/examples/sql/overview/subclass1/subclass1.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass2/main.cpp b/examples/sql/overview/subclass2/main.cpp new file mode 100644 index 000000000..f7f9bd91f --- /dev/null +++ b/examples/sql/overview/subclass2/main.cpp @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +InvoiceItemCursor::InvoiceItemCursor() : + TQSqlCursor( "invoiceitem" ) +{ + // NOOP +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + InvoiceItemCursor invoiceItemCursor; + + TQDataTable *invoiceItemTable = new TQDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "pricesid", "PriceID" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass2/main.h b/examples/sql/overview/subclass2/main.h new file mode 100644 index 000000000..020ecbff7 --- /dev/null +++ b/examples/sql/overview/subclass2/main.h @@ -0,0 +1,23 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +class TQSqlRecord; + +class InvoiceItemCursor : public TQSqlCursor +{ + public: + InvoiceItemCursor(); +}; + + diff --git a/examples/sql/overview/subclass2/subclass2.pro b/examples/sql/overview/subclass2/subclass2.pro new file mode 100644 index 000000000..a6cd8cb47 --- /dev/null +++ b/examples/sql/overview/subclass2/subclass2.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass3/main.cpp b/examples/sql/overview/subclass3/main.cpp new file mode 100644 index 000000000..db77b855e --- /dev/null +++ b/examples/sql/overview/subclass3/main.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +InvoiceItemCursor::InvoiceItemCursor() : + TQSqlCursor( "invoiceitem" ) +{ + TQSqlFieldInfo productName( "productname", TQVariant::String ); + append( productName ); + setCalculated( productName.name(), TRUE ); +} + + +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 ); + } + + return TQVariant( TQString::null ); +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + InvoiceItemCursor invoiceItemCursor; + + TQDataTable *invoiceItemTable = new TQDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "productname", "Product" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass3/main.h b/examples/sql/overview/subclass3/main.h new file mode 100644 index 000000000..06b0a1734 --- /dev/null +++ b/examples/sql/overview/subclass3/main.h @@ -0,0 +1,25 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +class TQSqlRecord; + +class InvoiceItemCursor : public TQSqlCursor +{ + public: + InvoiceItemCursor(); + protected: + TQVariant calculateField( const TQString & name ); +}; + + diff --git a/examples/sql/overview/subclass3/subclass3.pro b/examples/sql/overview/subclass3/subclass3.pro new file mode 100644 index 000000000..a6cd8cb47 --- /dev/null +++ b/examples/sql/overview/subclass3/subclass3.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass4/main.cpp b/examples/sql/overview/subclass4/main.cpp new file mode 100644 index 000000000..a96870e4d --- /dev/null +++ b/examples/sql/overview/subclass4/main.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +InvoiceItemCursor::InvoiceItemCursor() : + TQSqlCursor( "invoiceitem" ) +{ + TQSqlFieldInfo productName( "productname", TQVariant::String ); + append( productName ); + setCalculated( productName.name(), TRUE ); + + TQSqlFieldInfo productPrice( "price", TQVariant::Double ); + append( productPrice ); + setCalculated( productPrice.name(), TRUE ); + + TQSqlFieldInfo productCost( "cost", TQVariant::Double ); + append( productCost ); + setCalculated( productCost.name(), TRUE ); +} + + +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 ); + } + else if ( name == "price" ) { + 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() ); + } + + return TQVariant( TQString::null ); +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + InvoiceItemCursor invoiceItemCursor; + + TQDataTable *invoiceItemTable = new TQDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "productname", "Product" ); + invoiceItemTable->addColumn( "price", "Price" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "cost", "Cost" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass4/main.h b/examples/sql/overview/subclass4/main.h new file mode 100644 index 000000000..06b0a1734 --- /dev/null +++ b/examples/sql/overview/subclass4/main.h @@ -0,0 +1,25 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +class TQSqlRecord; + +class InvoiceItemCursor : public TQSqlCursor +{ + public: + InvoiceItemCursor(); + protected: + TQVariant calculateField( const TQString & name ); +}; + + diff --git a/examples/sql/overview/subclass4/subclass4.pro b/examples/sql/overview/subclass4/subclass4.pro new file mode 100644 index 000000000..a6cd8cb47 --- /dev/null +++ b/examples/sql/overview/subclass4/subclass4.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass5/main.cpp b/examples/sql/overview/subclass5/main.cpp new file mode 100644 index 000000000..260ddaf2a --- /dev/null +++ b/examples/sql/overview/subclass5/main.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +InvoiceItemCursor::InvoiceItemCursor() : + TQSqlCursor( "invoiceitem" ) +{ + TQSqlFieldInfo productName( "productname", TQVariant::String ); + append( productName ); + setCalculated( productName.name(), TRUE ); + + TQSqlFieldInfo productPrice( "price", TQVariant::Double ); + append( productPrice ); + setCalculated( productPrice.name(), TRUE ); + + TQSqlFieldInfo productCost( "cost", TQVariant::Double ); + append( productCost ); + setCalculated( productCost.name(), TRUE ); +} + + +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 ); + } + else if ( name == "price" ) { + 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() ); + } + + return TQVariant( TQString::null ); +} + + +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 ); + + return buffer; +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + InvoiceItemCursor invoiceItemCursor; + + TQDataTable *invoiceItemTable = new TQDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "productname", "Product" ); + invoiceItemTable->addColumn( "price", "Price" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "cost", "Cost" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass5/main.h b/examples/sql/overview/subclass5/main.h new file mode 100644 index 000000000..6f3a0edca --- /dev/null +++ b/examples/sql/overview/subclass5/main.h @@ -0,0 +1,25 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qdatetime.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +class TQSqlRecord; + +class InvoiceItemCursor : public TQSqlCursor +{ + public: + InvoiceItemCursor(); + TQSqlRecord *primeInsert(); + protected: + TQVariant calculateField( const TQString & name ); +}; diff --git a/examples/sql/overview/subclass5/subclass5.pro b/examples/sql/overview/subclass5/subclass5.pro new file mode 100644 index 000000000..a6cd8cb47 --- /dev/null +++ b/examples/sql/overview/subclass5/subclass5.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/table1/main.cpp b/examples/sql/overview/table1/main.cpp new file mode 100644 index 000000000..c0041a9a2 --- /dev/null +++ b/examples/sql/overview/table1/main.cpp @@ -0,0 +1,31 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qdatatable.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + TQSqlCursor staffCursor( "staff" ); + TQDataTable *staffTable = new TQDataTable( &staffCursor, TRUE ); + app.setMainWidget( staffTable ); + staffTable->refresh(); + staffTable->show(); + + return app.exec(); + } + + return 0; +} diff --git a/examples/sql/overview/table1/table1.pro b/examples/sql/overview/table1/table1.pro new file mode 100644 index 000000000..a6cd8cb47 --- /dev/null +++ b/examples/sql/overview/table1/table1.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/table2/main.cpp b/examples/sql/overview/table2/main.cpp new file mode 100644 index 000000000..79c9993f9 --- /dev/null +++ b/examples/sql/overview/table2/main.cpp @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qdatatable.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + TQSqlCursor staffCursor( "staff" ); + + TQDataTable *staffTable = new TQDataTable( &staffCursor ); + + app.setMainWidget( staffTable ); + + staffTable->addColumn( "forename", "Forename" ); + staffTable->addColumn( "surname", "Surname" ); + staffTable->addColumn( "salary", "Annual Salary" ); + + TQStringList order = TQStringList() << "surname" << "forename"; + staffTable->setSort( order ); + + staffTable->refresh(); + staffTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/table2/table2.pro b/examples/sql/overview/table2/table2.pro new file mode 100644 index 000000000..a6cd8cb47 --- /dev/null +++ b/examples/sql/overview/table2/table2.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/table3/main.cpp b/examples/sql/overview/table3/main.cpp new file mode 100644 index 000000000..4b9f2050e --- /dev/null +++ b/examples/sql/overview/table3/main.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +StatusPicker::StatusPicker( TQWidget *parent, const char *name ) + : TQComboBox( parent, 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(); + i++; + } +} + + +int StatusPicker::statusId() const +{ + return index2id[ currentItem() ]; +} + + +void StatusPicker::setStatusId( int statusid ) +{ + TQMap<int,int>::Iterator it; + for ( it = index2id.begin(); it != index2id.end(); ++it ) { + if ( it.data() == statusid ) { + setCurrentItem( it.key() ); + break; + } + } +} + + + +TQWidget *CustomSqlEditorFactory::createEditor( + TQWidget *parent, const TQSqlField *field ) +{ + if ( field->name() == "statusid" ) { + TQWidget *editor = new StatusPicker( parent ); + return editor; + } + + return TQSqlEditorFactory::createEditor( parent, field ); +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + TQSqlCursor staffCursor( "staff" ); + + TQDataTable *staffTable = new TQDataTable( &staffCursor ); + TQSqlPropertyMap *propMap = new TQSqlPropertyMap(); + CustomSqlEditorFactory *editorFactory = new CustomSqlEditorFactory(); + propMap->insert( "StatusPicker", "statusid" ); + staffTable->installPropertyMap( propMap ); + staffTable->installEditorFactory( editorFactory ); + + app.setMainWidget( staffTable ); + + staffTable->addColumn( "forename", "Forename" ); + staffTable->addColumn( "surname", "Surname" ); + staffTable->addColumn( "salary", "Annual Salary" ); + staffTable->addColumn( "statusid", "Status" ); + + TQStringList order = TQStringList() << "surname" << "forename"; + staffTable->setSort( order ); + + staffTable->refresh(); + staffTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/table3/main.h b/examples/sql/overview/table3/main.h new file mode 100644 index 000000000..dd0d8d095 --- /dev/null +++ b/examples/sql/overview/table3/main.h @@ -0,0 +1,40 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qcombobox.h> +#include <qmap.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqleditorfactory.h> +#include <qsqlpropertymap.h> +#include "../connection.h" + +class StatusPicker : public TQComboBox +{ + Q_OBJECT + Q_PROPERTY( int statusid READ statusId WRITE setStatusId ) + public: + StatusPicker( TQWidget *parent=0, const char *name=0 ); + int statusId() const; + void setStatusId( int id ); + private: + TQMap< int, int > index2id; +}; + + +class CustomSqlEditorFactory : public TQSqlEditorFactory +{ + Q_OBJECT + public: + TQWidget *createEditor( TQWidget *parent, const TQSqlField *field ); +}; + + + diff --git a/examples/sql/overview/table3/table3.pro b/examples/sql/overview/table3/table3.pro new file mode 100644 index 000000000..c490bedd0 --- /dev/null +++ b/examples/sql/overview/table3/table3.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = main.h +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/table4/main.cpp b/examples/sql/overview/table4/main.cpp new file mode 100644 index 000000000..b557b3f1e --- /dev/null +++ b/examples/sql/overview/table4/main.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" + + +StatusPicker::StatusPicker( TQWidget *parent, const char *name ) + : TQComboBox( parent, 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(); + i++; + } +} + + +int StatusPicker::statusId() const +{ + return index2id[ currentItem() ]; +} + + +void StatusPicker::setStatusId( int statusid ) +{ + TQMap<int,int>::Iterator it; + for ( it = index2id.begin(); it != index2id.end(); ++it ) { + if ( it.data() == statusid ) { + setCurrentItem( it.key() ); + break; + } + } +} + + +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(); + } + p->drawText( 2,2, cr.width()-4, cr.height()-4, fieldAlignment( field ), text ); + } + else { + TQDataTable::paintField( p, field, cr, b) ; + } +} + + +TQWidget *CustomSqlEditorFactory::createEditor( + TQWidget *parent, const TQSqlField *field ) +{ + if ( field->name() == "statusid" ) { + TQWidget *editor = new StatusPicker( parent ); + return editor; + } + + return TQSqlEditorFactory::createEditor( parent, field ); +} + + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv ); + + if ( createConnections() ) { + TQSqlCursor staffCursor( "staff" ); + + CustomTable *staffTable = new CustomTable( &staffCursor ); + TQSqlPropertyMap *propMap = new TQSqlPropertyMap(); + CustomSqlEditorFactory *editorFactory = new CustomSqlEditorFactory(); + propMap->insert( "StatusPicker", "statusid" ); + staffTable->installPropertyMap( propMap ); + staffTable->installEditorFactory( editorFactory ); + + app.setMainWidget( staffTable ); + + staffTable->addColumn( "forename", "Forename" ); + staffTable->addColumn( "surname", "Surname" ); + staffTable->addColumn( "salary", "Annual Salary" ); + staffTable->addColumn( "statusid", "Status" ); + + TQStringList order = TQStringList() << "surname" << "forename"; + staffTable->setSort( order ); + + staffTable->refresh(); + staffTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/table4/main.h b/examples/sql/overview/table4/main.h new file mode 100644 index 000000000..59edfc9d5 --- /dev/null +++ b/examples/sql/overview/table4/main.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qcombobox.h> +#include <qmap.h> +#include <qpainter.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqleditorfactory.h> +#include <qsqlpropertymap.h> +#include <qdatatable.h> +#include "../connection.h" + +class StatusPicker : public TQComboBox +{ + Q_OBJECT + Q_PROPERTY( int statusid READ statusId WRITE setStatusId ) +public: + StatusPicker( TQWidget *parent=0, const char *name=0 ); + int statusId() const; + void setStatusId( int id ); +private: + TQMap< int, int > index2id; +}; + + +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 ) {} + void paintField( + TQPainter * p, const TQSqlField* field, const TQRect & cr, bool ); + +}; + + +class CustomSqlEditorFactory : public TQSqlEditorFactory +{ + Q_OBJECT +public: + TQWidget *createEditor( TQWidget *parent, const TQSqlField *field ); +}; diff --git a/examples/sql/overview/table4/table4.pro b/examples/sql/overview/table4/table4.pro new file mode 100644 index 000000000..c490bedd0 --- /dev/null +++ b/examples/sql/overview/table4/table4.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = main.h +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/update/README b/examples/sql/overview/update/README new file mode 100644 index 000000000..eae3cd7f9 --- /dev/null +++ b/examples/sql/overview/update/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/update/main.cpp b/examples/sql/overview/update/main.cpp new file mode 100644 index 000000000..207702c06 --- /dev/null +++ b/examples/sql/overview/update/main.cpp @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + TQApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + 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(); + } + } + + return 0; +} diff --git a/examples/sql/overview/update/update.pro b/examples/sql/overview/update/update.pro new file mode 100644 index 000000000..9e6df96c9 --- /dev/null +++ b/examples/sql/overview/update/update.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/sql.pro b/examples/sql/sql.pro new file mode 100644 index 000000000..35f92914c --- /dev/null +++ b/examples/sql/sql.pro @@ -0,0 +1,9 @@ +TEMPLATE = subdirs + +CONFIG += ordered + +REQUIRES = full-config + +SUBDIRS = overview \ + sqltable \ + blob diff --git a/examples/sql/sqltable/README b/examples/sql/sqltable/README new file mode 100644 index 000000000..701458835 --- /dev/null +++ b/examples/sql/sqltable/README @@ -0,0 +1,18 @@ +This SQL table example retquires a connection to a SQL database. +Modify main.cpp to connect to your specific database. + +This example program expects a table called 'simpletable' to exist in +the database. You can create this table by running the following SQL +script (modify to suit your backend, if necessary): + +drop table simpletable; +create table simpletable +(id number primary key, +name varchar(20), +address varchar(20) ); + +-- optional, some sample data +insert into simpletable (id, name, address) +values (1, 'Trond', 'Oslo'); +insert into simpletable (id, name, address) +values (2, 'Dave', 'Oslo'); diff --git a/examples/sql/sqltable/main.cpp b/examples/sql/sqltable/main.cpp new file mode 100644 index 000000000..68d31389a --- /dev/null +++ b/examples/sql/sqltable/main.cpp @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qdatatable.h> +#include <qsqlcursor.h> +#include <qmessagebox.h> + +/* Modify the following to match your environment */ +#define DRIVER "TQSQLITE" /* see the TQt SQL documentation for a list of available drivers */ +#define DATABASE ":memory:" /* the name of your database */ +#define USER "" /* user name with appropriate rights */ +#define PASSWORD "" /* password for USER */ +#define HOST "" /* host on which the database is running */ + +class SimpleCursor : public TQSqlCursor +{ +public: + SimpleCursor () : TQSqlCursor( "simpletable" ) {} +protected: + TQSqlRecord* primeInsert() + { + /* a real-world application would use sequences, or the like */ + TQSqlRecord* buf = TQSqlCursor::primeInsert(); + TQSqlQuery q( "select max(id)+1 from simpletable" ); + if ( q.next() ) + buf->setValue( "id", q.value(0) ); + return buf; + } +}; + +int main( int argc, char ** argv ) +{ + TQApplication a( argc, argv ); + + TQSqlDatabase * db = TQSqlDatabase::addDatabase( DRIVER ); + db->setDatabaseName( DATABASE ); + db->setUserName( USER ); + db->setPassword( PASSWORD ); + db->setHostName( HOST ); + + if( !db->open() ){ + db->lastError().showMessage( "An error occured. Please read the README file in the sqltable" + "dir for more information.\n\n" ); + return 1; + } + + if (!db->tables().contains("simpletable")) { + TQSqlQuery q("create table simpletable(id int, name varchar(20), address varchar(20))", db); + } + + SimpleCursor cursor; + + TQDataTable table( &cursor ); /* data table uses our cursor */ + table.addColumn( "name", "Name" ); + table.addColumn( "address", "Address" ); + table.setSorting( TRUE ); + + a.setMainWidget( &table ); + table.refresh(); /* load data */ + table.show(); /* show widget */ + + return a.exec(); +} diff --git a/examples/sql/sqltable/sqltable.doc b/examples/sql/sqltable/sqltable.doc new file mode 100644 index 000000000..bfa86f551 --- /dev/null +++ b/examples/sql/sqltable/sqltable.doc @@ -0,0 +1,24 @@ +/* +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ +*/ +/*! \page sqltable-example.html + + \ingroup sql-examples + \title SQL Table + + This example shows how to use a QDataTable to browse data in a SQL database. + + <hr> + + Implementation: + + \include sql/sqltable/main.cpp + +*/ diff --git a/examples/sql/sqltable/sqltable.pro b/examples/sql/sqltable/sqltable.pro new file mode 100644 index 000000000..6a8c216f6 --- /dev/null +++ b/examples/sql/sqltable/sqltable.pro @@ -0,0 +1,10 @@ +TEMPLATE = app +TARGET = sqltable + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp +INTERFACES = |