From bd0f3345a938b35ce6a12f6150373b0955b8dd12 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Sun, 10 Jul 2011 15:24:15 -0500 Subject: Add Qt3 development HEAD version --- doc/html/sqltable-example.html | 121 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 doc/html/sqltable-example.html (limited to 'doc/html/sqltable-example.html') diff --git a/doc/html/sqltable-example.html b/doc/html/sqltable-example.html new file mode 100644 index 0000000..d298361 --- /dev/null +++ b/doc/html/sqltable-example.html @@ -0,0 +1,121 @@ + + + + + +SQL Table + + + + + + + +
+ +Home + | +All Classes + | +Main Classes + | +Annotated + | +Grouped Classes + | +Functions +

SQL Table

+ + +

+This example shows how to use a QDataTable to browse data in a SQL database. +


+

Implementation: +

/****************************************************************************
+**
+** Copyright (C) 1992-2007 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();
+}
+
+ +

See also Qt SQL Examples. + + +


+ +
Copyright © 2007 +TrolltechTrademarks +
Qt 3.3.8
+
+ -- cgit v1.2.3