sqltable-example.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 118 行

HTML
118
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/sql/sqltable/sqltable.doc:11 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>SQL Table</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><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> <p> This example shows how to use a <a href="qdatatable.html">QDataTable</a> to browse data in a SQL database.<p> <hr><p> Implementation:<p> <pre>/******************************************************************************** Copyright (C) 1992-2000 Trolltech AS.  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 &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;#include &lt;<a href="qsqldatabase-h.html">qsqldatabase.h</a>&gt;#include &lt;<a href="qdatatable-h.html">qdatatable.h</a>&gt;#include &lt;<a href="qsqlcursor-h.html">qsqlcursor.h</a>&gt;#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;/* Modify the following to match your environment */#define DRIVER       "QPSQL7"  /* see the Qt SQL documentation for a list of available drivers */#define DATABASE     "simpledb" /* the name of your database */#define USER         "trond"   /* user name with appropriate rights */#define PASSWORD     "trond"   /* password for USER */#define HOST         "silverfish.troll.no" /* host on which the database is running */class SimpleCursor : public <a href="qsqlcursor.html">QSqlCursor</a>{public:    SimpleCursor () : <a href="qsqlcursor.html">QSqlCursor</a>( "simpletable" ) {}protected:    <a href="qsqlrecord.html">QSqlRecord</a>* primeInsert()    {        /* a real-world application would use sequences, or the like */<a name="x2792"></a>        <a href="qsqlrecord.html">QSqlRecord</a>* buf = QSqlCursor::<a href="qsqlcursor.html#primeInsert">primeInsert</a>();        <a href="qsqlquery.html">QSqlQuery</a> q( "select max(id)+1 from simpletable;" );<a name="x2800"></a>        if ( q.<a href="qsqlquery.html#next">next</a>() )<a name="x2802"></a><a name="x2801"></a>               buf-&gt;<a href="qsqlrecord.html#setValue">setValue</a>( "id", q.<a href="qsqlquery.html#value">value</a>(0) );        return buf;    }};int main( int argc, char ** argv ){    <a href="qapplication.html">QApplication</a> a( argc, argv );<a name="x2793"></a>    <a href="qsqldatabase.html">QSqlDatabase</a> * db = QSqlDatabase::<a href="qsqldatabase.html#addDatabase">addDatabase</a>( DRIVER );<a name="x2796"></a>    db-&gt;<a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>( DATABASE );<a name="x2799"></a>    db-&gt;<a href="qsqldatabase.html#setUserName">setUserName</a>( USER );<a name="x2798"></a>    db-&gt;<a href="qsqldatabase.html#setPassword">setPassword</a>( PASSWORD );<a name="x2797"></a>    db-&gt;<a href="qsqldatabase.html#setHostName">setHostName</a>( HOST );<a name="x2795"></a>    if( !db-&gt;<a href="qsqldatabase.html#open">open</a>() ){        QMessageBox::<a href="qmessagebox.html#information">information</a>( 0, "Unable to open database",<a name="x2794"></a>                                  db-&gt;<a href="qsqldatabase.html#lastError">lastError</a>().databaseText() + "\nPlease read the README file in the sqltable directory for more information.");        return 1;    }    SimpleCursor cursor;    <a href="qdatatable.html">QDataTable</a> table( &amp;cursor ); /* data table uses our cursor */<a name="x2789"></a>    table.<a href="qdatatable.html#addColumn">addColumn</a>( "name", "Name" );    table.<a href="qdatatable.html#addColumn">addColumn</a>( "address", "Address" );<a name="x2803"></a>    table.<a href="qtable.html#setSorting">setSorting</a>( TRUE );    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;table );<a name="x2790"></a>    table.<a href="qdatatable.html#refresh">refresh</a>(); /* load data */    table.<a href="qwidget.html#show">show</a>();    /* show widget */    return a.<a href="qapplication.html#exec">exec</a>();}</pre><p> <p>See also <a href="sql-examples.html">Qt SQL Examples</a>.<!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright &copy; 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?