Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 2189

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/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="x2636"></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="x2644"></a>        if ( q.<a href="qsqlquery.html#next">next</a>() )
<a name="x2646"></a><a name="x2645"></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="x2637"></a>    <a href="qsqldatabase.html">QSqlDatabase</a> * db = QSqlDatabase::<a href="qsqldatabase.html#addDatabase">addDatabase</a>( DRIVER );
<a name="x2640"></a>    db-&gt;<a href="qsqldatabase.html#setDatabaseName">setDatabaseName</a>( DATABASE );
<a name="x2643"></a>    db-&gt;<a href="qsqldatabase.html#setUserName">setUserName</a>( USER );
<a name="x2642"></a>    db-&gt;<a href="qsqldatabase.html#setPassword">setPassword</a>( PASSWORD );
<a name="x2641"></a>    db-&gt;<a href="qsqldatabase.html#setHostName">setHostName</a>( HOST );

<a name="x2639"></a>    if( !db-&gt;<a href="qsqldatabase.html#open">open</a>() ){
<a name="x2635"></a>        QMessageBox::<a href="qmessagebox.html#information">information</a>( 0, "Unable to open database",
<a name="x2638"></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="x2633"></a>    table.<a href="qdatatable.html#addColumn">addColumn</a>( "name", "Name" );
    table.<a href="qdatatable.html#addColumn">addColumn</a>( "address", "Address" );
<a name="x2647"></a>    table.<a href="qtable.html#setSorting">setSorting</a>( TRUE );

    a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;table );
<a name="x2634"></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; 2001 
<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.2</div>
</table></div></address></body>
</html>