| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | 
|---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/sql/sqltable/sqltable.doc:11 --> | 
|---|
| 3 | <html> | 
|---|
| 4 | <head> | 
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
| 6 | <title>SQL Table</title> | 
|---|
| 7 | <style type="text/css"><!-- | 
|---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; } | 
|---|
| 9 | a:link { color: #004faf; text-decoration: none } | 
|---|
| 10 | a:visited { color: #672967; text-decoration: none } | 
|---|
| 11 | body { background: #ffffff; color: black; } | 
|---|
| 12 | --></style> | 
|---|
| 13 | </head> | 
|---|
| 14 | <body> | 
|---|
| 15 |  | 
|---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%"> | 
|---|
| 17 | <tr bgcolor="#E5E5E5"> | 
|---|
| 18 | <td valign=center> | 
|---|
| 19 | <a href="index.html"> | 
|---|
| 20 | <font color="#004faf">Home</font></a> | 
|---|
| 21 | | <a href="classes.html"> | 
|---|
| 22 | <font color="#004faf">All Classes</font></a> | 
|---|
| 23 | | <a href="mainclasses.html"> | 
|---|
| 24 | <font color="#004faf">Main Classes</font></a> | 
|---|
| 25 | | <a href="annotated.html"> | 
|---|
| 26 | <font color="#004faf">Annotated</font></a> | 
|---|
| 27 | | <a href="groups.html"> | 
|---|
| 28 | <font color="#004faf">Grouped Classes</font></a> | 
|---|
| 29 | | <a href="functions.html"> | 
|---|
| 30 | <font color="#004faf">Functions</font></a> | 
|---|
| 31 | </td> | 
|---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>SQL Table</h1> | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | <p> | 
|---|
| 36 | This example shows how to use a <a href="qdatatable.html">QDataTable</a> to browse data in a SQL database. | 
|---|
| 37 | <p> <hr> | 
|---|
| 38 | <p> Implementation: | 
|---|
| 39 | <p> <pre>/**************************************************************************** | 
|---|
| 40 | ** | 
|---|
| 41 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 42 | ** | 
|---|
| 43 | ** This file is part of an example program for Qt.  This example | 
|---|
| 44 | ** program may be used, distributed and modified without limitation. | 
|---|
| 45 | ** | 
|---|
| 46 | *****************************************************************************/ | 
|---|
| 47 |  | 
|---|
| 48 | #include <<a href="qapplication-h.html">qapplication.h</a>> | 
|---|
| 49 | #include <<a href="qsqldatabase-h.html">qsqldatabase.h</a>> | 
|---|
| 50 | #include <<a href="qdatatable-h.html">qdatatable.h</a>> | 
|---|
| 51 | #include <<a href="qsqlcursor-h.html">qsqlcursor.h</a>> | 
|---|
| 52 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>> | 
|---|
| 53 |  | 
|---|
| 54 | /* Modify the following to match your environment */ | 
|---|
| 55 | #define DRIVER       "QSQLITE"  /* see the Qt SQL documentation for a list of available drivers */ | 
|---|
| 56 | #define DATABASE     ":memory:" /* the name of your database */ | 
|---|
| 57 | #define USER         ""   /* user name with appropriate rights */ | 
|---|
| 58 | #define PASSWORD     ""   /* password for USER */ | 
|---|
| 59 | #define HOST         "" /* host on which the database is running */ | 
|---|
| 60 |  | 
|---|
| 61 | class SimpleCursor : public <a href="qsqlcursor.html">QSqlCursor</a> | 
|---|
| 62 | { | 
|---|
| 63 | public: | 
|---|
| 64 | SimpleCursor () : <a href="qsqlcursor.html">QSqlCursor</a>( "simpletable" ) {} | 
|---|
| 65 | protected: | 
|---|
| 66 | <a href="qsqlrecord.html">QSqlRecord</a>* primeInsert() | 
|---|
| 67 | { | 
|---|
| 68 | /* a real-world application would use sequences, or the like */ | 
|---|
| 69 | <a name="x2453"></a>        <a href="qsqlrecord.html">QSqlRecord</a>* buf = QSqlCursor::<a href="qsqlcursor.html#primeInsert">primeInsert</a>(); | 
|---|
| 70 | <a href="qsqlquery.html">QSqlQuery</a> q( "select max(id)+1 from simpletable" ); | 
|---|
| 71 | if ( q.<a href="qsqlquery.html#next">next</a>() ) | 
|---|
| 72 | <a name="x2464"></a>               buf-><a href="qsqlrecord.html#setValue">setValue</a>( "id", q.<a href="qsqlquery.html#value">value</a>(0) ); | 
|---|
| 73 | return buf; | 
|---|
| 74 | } | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | int main( int argc, char ** argv ) | 
|---|
| 78 | { | 
|---|
| 79 | <a href="qapplication.html">QApplication</a> a( argc, argv ); | 
|---|
| 80 |  | 
|---|
| 81 | <a name="x2454"></a>    <a href="qsqldatabase.html">QSqlDatabase</a> * db = QSqlDatabase::<a href="qsqldatabase.html#addDatabase">addDatabase</a>( DRIVER ); | 
|---|
| 82 | <a name="x2457"></a>    db-><a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>( DATABASE ); | 
|---|
| 83 | <a name="x2460"></a>    db-><a href="qsqldatabase.html#setUserName">setUserName</a>( USER ); | 
|---|
| 84 | <a name="x2459"></a>    db-><a href="qsqldatabase.html#setPassword">setPassword</a>( PASSWORD ); | 
|---|
| 85 | <a name="x2458"></a>    db-><a href="qsqldatabase.html#setHostName">setHostName</a>( HOST ); | 
|---|
| 86 |  | 
|---|
| 87 | <a name="x2456"></a>    if( !db-><a href="qsqldatabase.html#open">open</a>() ){ | 
|---|
| 88 | <a name="x2455"></a>        db-><a href="qsqldatabase.html#lastError">lastError</a>().showMessage( "An error occured. Please read the README file in the sqltable" | 
|---|
| 89 | "dir for more information.\n\n" ); | 
|---|
| 90 | return 1; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | <a name="x2461"></a>    if (!db-><a href="qsqldatabase.html#tables">tables</a>().contains("simpletable")) { | 
|---|
| 94 | <a href="qsqlquery.html">QSqlQuery</a> q("create table simpletable(id int, name varchar(20), address varchar(20))", db); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | SimpleCursor cursor; | 
|---|
| 98 |  | 
|---|
| 99 | <a href="qdatatable.html">QDataTable</a> table( &cursor ); /* data table uses our cursor */ | 
|---|
| 100 | <a name="x2451"></a>    table.<a href="qdatatable.html#addColumn">addColumn</a>( "name", "Name" ); | 
|---|
| 101 | table.<a href="qdatatable.html#addColumn">addColumn</a>( "address", "Address" ); | 
|---|
| 102 | <a name="x2465"></a>    table.<a href="qtable.html#setSorting">setSorting</a>( TRUE ); | 
|---|
| 103 |  | 
|---|
| 104 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &table ); | 
|---|
| 105 | <a name="x2452"></a>    table.<a href="qdatatable.html#refresh">refresh</a>(); /* load data */ | 
|---|
| 106 | table.<a href="qwidget.html#show">show</a>();    /* show widget */ | 
|---|
| 107 |  | 
|---|
| 108 | return a.<a href="qapplication.html#exec">exec</a>(); | 
|---|
| 109 | } | 
|---|
| 110 | </pre> | 
|---|
| 111 |  | 
|---|
| 112 | <p> <p>See also <a href="sql-examples.html">Qt SQL Examples</a>. | 
|---|
| 113 |  | 
|---|
| 114 | <!-- eof --> | 
|---|
| 115 | <p><address><hr><div align=center> | 
|---|
| 116 | <table width=100% cellspacing=0 border=0><tr> | 
|---|
| 117 | <td>Copyright © 2007 | 
|---|
| 118 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> | 
|---|
| 119 | <td align=right><div align=right>Qt 3.3.8</div> | 
|---|
| 120 | </table></div></address></body> | 
|---|
| 121 | </html> | 
|---|