diff options
Diffstat (limited to 'examples/demo/sql')
-rw-r--r-- | examples/demo/sql/book.sql | 42 | ||||
-rw-r--r-- | examples/demo/sql/connect.ui | 244 | ||||
-rw-r--r-- | examples/demo/sql/connect.ui.h | 20 | ||||
-rw-r--r-- | examples/demo/sql/sqlex.ui | 309 | ||||
-rw-r--r-- | examples/demo/sql/sqlex.ui.h | 126 |
5 files changed, 741 insertions, 0 deletions
diff --git a/examples/demo/sql/book.sql b/examples/demo/sql/book.sql new file mode 100644 index 0000000..417ef43 --- /dev/null +++ b/examples/demo/sql/book.sql @@ -0,0 +1,42 @@ +-- The following SQL generates the database +-- used by the 'book' example programs +-- 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. + +DROP TABLE author; +DROP TABLE book; +DROP TABLE sequence; + +CREATE TABLE author +( id integer primary key, +forename varchar(40), +surname varchar(40) ); + +CREATE TABLE book +( id integer primary key, +title varchar(40), +price numeric(10,2), +authorid integer, +notes varchar(255) ); + +create index book_authorid_idx on book( authorid ); + +CREATE TABLE sequence +( tablename varchar(10), +sequence numeric); + +INSERT INTO author VALUES ( 0, 'Philip K', 'Dick' ); +INSERT INTO author VALUES ( 1, 'Robert', 'Heinlein' ); +INSERT INTO author VALUES ( 2, 'Sarah', 'Paretsky' ); + +INSERT INTO book VALUES ( 0, 'The Man Who Japed', 6.99, 0, 'A good book' ); +INSERT INTO book VALUES ( 1, 'The Man in the High Castle', 9.99, 0, 'Worth reading' ); +INSERT INTO book VALUES ( 2, 'The Number of the Beast', 8.99, 1, 'Get this!' ); +INSERT INTO book VALUES ( 3, 'Indemnity Only', 9.99, 2, 'Cool' ); +INSERT INTO book VALUES ( 4, 'Burn Marks', 9.99, 2, 'Need to make notes' ); +INSERT INTO book VALUES ( 5, 'Deadlock', 9.99, 2, 'Hmmm..' ); + +INSERT INTO sequence VALUES ( 'author', 2 ); +INSERT INTO sequence VALUES ( 'book', 5 ); diff --git a/examples/demo/sql/connect.ui b/examples/demo/sql/connect.ui new file mode 100644 index 0000000..8031fd0 --- /dev/null +++ b/examples/demo/sql/connect.ui @@ -0,0 +1,244 @@ +<!DOCTYPE UI><UI version="3.0" stdsetdef="1"> +<class>ConnectDialog</class> +<widget class="QDialog"> + <property name="name"> + <cstring>ConnectDialog</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>303</width> + <height>247</height> + </rect> + </property> + <property name="caption"> + <string>Connect...</string> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>11</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="QGroupBox"> + <property name="name"> + <cstring>GroupBox3</cstring> + </property> + <property name="title"> + <string>Connection settings</string> + </property> + <grid> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>11</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="QLineEdit" row="1" column="1"> + <property name="name"> + <cstring>editDatabase</cstring> + </property> + </widget> + <widget class="QLabel" row="1" column="0"> + <property name="name"> + <cstring>TextLabel3</cstring> + </property> + <property name="text"> + <string>Database Name:</string> + </property> + <property name="buddy" stdset="0"> + <cstring>comboDatabase</cstring> + </property> + </widget> + <widget class="QLineEdit" row="3" column="1"> + <property name="name"> + <cstring>editPassword</cstring> + </property> + <property name="echoMode"> + <enum>Password</enum> + </property> + </widget> + <widget class="QLabel" row="2" column="0"> + <property name="name"> + <cstring>TextLabel4</cstring> + </property> + <property name="text"> + <string>&Username:</string> + </property> + <property name="buddy" stdset="0"> + <cstring>editUsername</cstring> + </property> + </widget> + <widget class="QLabel" row="3" column="0"> + <property name="name"> + <cstring>TextLabel4_2</cstring> + </property> + <property name="text"> + <string>&Password:</string> + </property> + <property name="buddy" stdset="0"> + <cstring>editPassword</cstring> + </property> + </widget> + <widget class="QComboBox" row="0" column="1"> + <property name="name"> + <cstring>comboDriver</cstring> + </property> + </widget> + <widget class="QLineEdit" row="2" column="1"> + <property name="name"> + <cstring>editUsername</cstring> + </property> + </widget> + <widget class="QLineEdit" row="4" column="1"> + <property name="name"> + <cstring>editHostname</cstring> + </property> + </widget> + <widget class="QLabel" row="4" column="0"> + <property name="name"> + <cstring>TextLabel5</cstring> + </property> + <property name="text"> + <string>&Hostname:</string> + </property> + <property name="buddy" stdset="0"> + <cstring>editHostname</cstring> + </property> + </widget> + <widget class="QLabel" row="5" column="0"> + <property name="name"> + <cstring>TextLabel5_2</cstring> + </property> + <property name="text"> + <string>P&ort:</string> + </property> + <property name="buddy" stdset="0"> + <cstring>portSpinBox</cstring> + </property> + </widget> + <widget class="QSpinBox" row="5" column="1"> + <property name="name"> + <cstring>portSpinBox</cstring> + </property> + <property name="specialValueText"> + <string>Default</string> + </property> + <property name="maxValue"> + <number>65535</number> + </property> + <property name="minValue"> + <number>-1</number> + </property> + <property name="value"> + <number>-1</number> + </property> + </widget> + <widget class="QLabel" row="0" column="0"> + <property name="name"> + <cstring>TextLabel2</cstring> + </property> + <property name="text"> + <string>D&river</string> + </property> + <property name="buddy" stdset="0"> + <cstring>comboDriver</cstring> + </property> + </widget> + </grid> + </widget> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>Layout25</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <spacer> + <property name="name"> + <cstring>Spacer1</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="sizeHint"> + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + <widget class="QPushButton"> + <property name="name"> + <cstring>PushButton1</cstring> + </property> + <property name="text"> + <string>&OK</string> + </property> + <property name="default"> + <bool>true</bool> + </property> + </widget> + <widget class="QPushButton"> + <property name="name"> + <cstring>PushButton2</cstring> + </property> + <property name="text"> + <string>&Cancel</string> + </property> + </widget> + </hbox> + </widget> + </vbox> +</widget> +<connections> + <connection> + <sender>PushButton1</sender> + <signal>clicked()</signal> + <receiver>ConnectDialog</receiver> + <slot>accept()</slot> + </connection> + <connection> + <sender>PushButton2</sender> + <signal>clicked()</signal> + <receiver>ConnectDialog</receiver> + <slot>reject()</slot> + </connection> +</connections> +<tabstops> + <tabstop>comboDriver</tabstop> + <tabstop>editDatabase</tabstop> + <tabstop>editUsername</tabstop> + <tabstop>editPassword</tabstop> + <tabstop>editHostname</tabstop> + <tabstop>portSpinBox</tabstop> + <tabstop>PushButton1</tabstop> + <tabstop>PushButton2</tabstop> +</tabstops> +<includes> + <include location="local" impldecl="in implementation">connect.ui.h</include> +</includes> +<slots> + <slot>init()</slot> + <slot>destroy()</slot> +</slots> +<layoutdefaults spacing="6" margin="11"/> +</UI> diff --git a/examples/demo/sql/connect.ui.h b/examples/demo/sql/connect.ui.h new file mode 100644 index 0000000..da1c78f --- /dev/null +++ b/examples/demo/sql/connect.ui.h @@ -0,0 +1,20 @@ +/**************************************************************************** +** ui.h extension file, included from the uic-generated form implementation. +** +** If you wish to add, delete or rename slots use Qt Designer which will +** update this file, preserving your code. Create an init() slot in place of +** a constructor, and a destroy() slot in place of a destructor. +*****************************************************************************/ + +#include <qsqldatabase.h> + +void ConnectDialog::init() +{ + comboDriver->clear(); + comboDriver->insertStringList( QSqlDatabase::drivers() ); +} + +void ConnectDialog::destroy() +{ + +} diff --git a/examples/demo/sql/sqlex.ui b/examples/demo/sql/sqlex.ui new file mode 100644 index 0000000..48a3605 --- /dev/null +++ b/examples/demo/sql/sqlex.ui @@ -0,0 +1,309 @@ +<!DOCTYPE UI><UI version="3.1" stdsetdef="1"> +<class>SqlEx</class> +<widget class="QWidget"> + <property name="name"> + <cstring>SqlEx</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>654</width> + <height>438</height> + </rect> + </property> + <property name="caption"> + <string>Form1</string> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>11</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="QSplitter"> + <property name="name"> + <cstring>vsplit</cstring> + </property> + <property name="orientation"> + <enum>Vertical</enum> + </property> + <widget class="QSplitter"> + <property name="name"> + <cstring>hsplit</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <widget class="QListView"> + <column> + <property name="text"> + <string>Tables</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <column> + <property name="text"> + <string>Type</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <column> + <property name="text"> + <string>Required</string> + </property> + <property name="clickable"> + <bool>true</bool> + </property> + <property name="resizable"> + <bool>true</bool> + </property> + </column> + <property name="name"> + <cstring>lv</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <property name="allColumnsShowFocus"> + <bool>true</bool> + </property> + <property name="rootIsDecorated"> + <bool>true</bool> + </property> + <property name="resizeMode"> + <enum>LastColumn</enum> + </property> + </widget> + <widget class="QDataTable"> + <property name="name"> + <cstring>dt</cstring> + </property> + <property name="frameworkCode" stdset="0"> + <bool>false</bool> + </property> + </widget> + </widget> + <widget class="QGroupBox"> + <property name="name"> + <cstring>gb</cstring> + </property> + <property name="title"> + <string>SQL Query</string> + </property> + <grid> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>11</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="QLayoutWidget" row="0" column="0"> + <property name="name"> + <cstring>Layout22</cstring> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="QTextEdit"> + <property name="name"> + <cstring>te</cstring> + </property> + </widget> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>Layout21</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <spacer> + <property name="name"> + <cstring>Spacer3</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="sizeHint"> + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + <widget class="QPushButton"> + <property name="name"> + <cstring>submitBtn</cstring> + </property> + <property name="sizePolicy"> + <sizepolicy> + <hsizetype>5</hsizetype> + <vsizetype>0</vsizetype> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="minimumSize"> + <size> + <width>80</width> + <height>0</height> + </size> + </property> + <property name="text"> + <string>&Submit</string> + </property> + </widget> + <widget class="QPushButton"> + <property name="name"> + <cstring>clearBtn</cstring> + </property> + <property name="sizePolicy"> + <sizepolicy> + <hsizetype>5</hsizetype> + <vsizetype>0</vsizetype> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="minimumSize"> + <size> + <width>80</width> + <height>0</height> + </size> + </property> + <property name="text"> + <string>C&lear</string> + </property> + </widget> + </hbox> + </widget> + </vbox> + </widget> + </grid> + </widget> + </widget> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>Layout23</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <widget class="QLabel"> + <property name="name"> + <cstring>lbl</cstring> + </property> + <property name="text"> + <string>Press "Connect" to open a database</string> + </property> + </widget> + <widget class="QPushButton"> + <property name="name"> + <cstring>conBtn</cstring> + </property> + <property name="sizePolicy"> + <sizepolicy> + <hsizetype>0</hsizetype> + <vsizetype>0</vsizetype> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="minimumSize"> + <size> + <width>80</width> + <height>0</height> + </size> + </property> + <property name="text"> + <string>&Connect...</string> + </property> + </widget> + </hbox> + </widget> + </vbox> +</widget> +<connections> + <connection> + <sender>clearBtn</sender> + <signal>clicked()</signal> + <receiver>te</receiver> + <slot>clear()</slot> + </connection> + <connection> + <sender>conBtn</sender> + <signal>clicked()</signal> + <receiver>SqlEx</receiver> + <slot>dbConnect()</slot> + </connection> + <connection> + <sender>lv</sender> + <signal>returnPressed(QListViewItem*)</signal> + <receiver>SqlEx</receiver> + <slot>showTable(QListViewItem*)</slot> + </connection> + <connection> + <sender>lv</sender> + <signal>doubleClicked(QListViewItem*)</signal> + <receiver>SqlEx</receiver> + <slot>showTable(QListViewItem*)</slot> + </connection> + <connection> + <sender>submitBtn</sender> + <signal>clicked()</signal> + <receiver>SqlEx</receiver> + <slot>execQuery()</slot> + </connection> +</connections> +<includes> + <include location="local" impldecl="in implementation">sqlex.ui.h</include> +</includes> +<slots> + <slot>init()</slot> + <slot>dbConnect()</slot> + <slot>execQuery()</slot> + <slot>showTable( QListViewItem * item )</slot> +</slots> +<layoutdefaults spacing="6" margin="11"/> +</UI> diff --git a/examples/demo/sql/sqlex.ui.h b/examples/demo/sql/sqlex.ui.h new file mode 100644 index 0000000..bad4059 --- /dev/null +++ b/examples/demo/sql/sqlex.ui.h @@ -0,0 +1,126 @@ +/**************************************************************************** +** ui.h extension file, included from the uic-generated form implementation. +** +** If you wish to add, delete or rename slots use Qt Designer which will +** update this file, preserving your code. Create an init() slot in place of +** a constructor, and a destroy() slot in place of a destructor. +*****************************************************************************/ +#include <qsqldriver.h> +#include <qmessagebox.h> +#include <qsqldatabase.h> +#include <qlineedit.h> +#include <qcombobox.h> +#include <qspinbox.h> +#include <qsqlerror.h> +#include <qsqlcursor.h> +#include <qsqlselectcursor.h> +#include <qdatatable.h> +#include "connect.h" + +static void showError( const QSqlError& err, QWidget* parent = 0 ) +{ + QString errStr ( "The database reported an error\n" ); + if ( !err.databaseText().isEmpty() ) + errStr += err.databaseText(); + if ( !err.driverText().isEmpty() ) + errStr += err.driverText(); + QMessageBox::warning( parent, "Error", errStr ); +} + +ConnectDialog* conDiag = 0; + +void SqlEx::init() +{ + hsplit->setResizeMode( lv, QSplitter::KeepSize ); + vsplit->setResizeMode( gb, QSplitter::KeepSize ); + submitBtn->setEnabled( FALSE ); + conDiag = new ConnectDialog( this, "Connection Dialog", TRUE ); +} + +void SqlEx::dbConnect() +{ + if ( conDiag->exec() != QDialog::Accepted ) + return; + if ( dt->sqlCursor() ) { + dt->setSqlCursor( 0 ); + } + // close old connection (if any) + if ( QSqlDatabase::contains( "SqlEx" ) ) { + QSqlDatabase* oldDb = QSqlDatabase::database( "SqlEx" ); + oldDb->close(); + QSqlDatabase::removeDatabase( "SqlEx" ); + } + // open the new connection + QSqlDatabase* db = QSqlDatabase::addDatabase( conDiag->comboDriver->currentText(), "SqlEx" ); + if ( !db ) { + QMessageBox::warning( this, "Error", "Could not open database" ); + return; + } + db->setHostName( conDiag->editHostname->text() ); + db->setDatabaseName( conDiag->editDatabase->text() ); + db->setPort( conDiag->portSpinBox->value() ); + if ( !db->open( conDiag->editUsername->text(), conDiag->editPassword->text() ) ) { + showError( db->lastError(), this ); + return; + } + lbl->setText( "Double-Click on a table-name to view the contents" ); + lv->clear(); + + QStringList tables = db->tables(); + for ( QStringList::Iterator it = tables.begin(); it != tables.end(); ++it ) { + QListViewItem* lvi = new QListViewItem( lv, *it ); + QSqlRecordInfo ri = db->recordInfo ( *it ); + for ( QSqlRecordInfo::Iterator it = ri.begin(); it != ri.end(); ++it ) { + QString req; + if ( (*it).isRequired() > 0 ) { + req = "Yes"; + } else if ( (*it).isRequired() == 0 ) { + req = "No"; + } else { + req = "?"; + } + QListViewItem* fi = new QListViewItem( lvi, (*it).name(), + QVariant::typeToName( (*it).type() ), req ); + lvi->insertItem( fi ); + } + lv->insertItem( lvi ); + } + submitBtn->setEnabled( TRUE ); +} + +void SqlEx::execQuery() +{ + // use a custom cursor to populate the data table + QSqlSelectCursor* cursor = new QSqlSelectCursor( te->text(), QSqlDatabase::database( "SqlEx", TRUE ) ); + if ( cursor->isSelect() ) { + dt->setSqlCursor( cursor, TRUE, TRUE ); + dt->setSort( QStringList() ); + dt->refresh( QDataTable::RefreshAll ); + QString txt( "Query OK" ); + if ( cursor->size() >= 0 ) + txt += ", returned rows: " + QString::number( cursor->size() ); + lbl->setText( txt ); + } else { + // an error occured if the cursor is not active + if ( !cursor->isActive() ) { + showError( cursor->lastError(), this ); + } else { + lbl->setText( QString("Query OK, affected rows: %1").arg( cursor->numRowsAffected() ) ); + } + } +} + +void SqlEx::showTable( QListViewItem * item ) +{ + // get the table name + QListViewItem* i = item->parent(); + if ( !i ) { + i = item; + } + + // populate the data table + QSqlCursor* cursor = new QSqlCursor( i->text( 0 ), TRUE, QSqlDatabase::database( "SqlEx", TRUE ) ); + dt->setSqlCursor( cursor, TRUE, TRUE ); + dt->setSort( cursor->primaryIndex() ); + dt->refresh( QDataTable::RefreshAll ); + lbl->setText( "Displaying table " + i->text( 0 ) ); +} |