summaryrefslogtreecommitdiffstats
path: root/examples/sql/sqltable
diff options
context:
space:
mode:
Diffstat (limited to 'examples/sql/sqltable')
-rw-r--r--examples/sql/sqltable/README18
-rw-r--r--examples/sql/sqltable/main.cpp71
-rw-r--r--examples/sql/sqltable/sqltable.doc24
-rw-r--r--examples/sql/sqltable/sqltable.pro10
4 files changed, 123 insertions, 0 deletions
diff --git a/examples/sql/sqltable/README b/examples/sql/sqltable/README
new file mode 100644
index 0000000..4fad463
--- /dev/null
+++ b/examples/sql/sqltable/README
@@ -0,0 +1,18 @@
+This SQL table example requires 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 0000000..9c068aa
--- /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 Qt. 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 "QSQLITE" /* see the Qt 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 QSqlCursor
+{
+public:
+ SimpleCursor () : QSqlCursor( "simpletable" ) {}
+protected:
+ QSqlRecord* primeInsert()
+ {
+ /* a real-world application would use sequences, or the like */
+ QSqlRecord* buf = QSqlCursor::primeInsert();
+ QSqlQuery q( "select max(id)+1 from simpletable" );
+ if ( q.next() )
+ buf->setValue( "id", q.value(0) );
+ return buf;
+ }
+};
+
+int main( int argc, char ** argv )
+{
+ QApplication a( argc, argv );
+
+ QSqlDatabase * db = QSqlDatabase::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")) {
+ QSqlQuery q("create table simpletable(id int, name varchar(20), address varchar(20))", db);
+ }
+
+ SimpleCursor cursor;
+
+ QDataTable 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 0000000..bfa86f5
--- /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 0000000..6a8c216
--- /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 =