summaryrefslogtreecommitdiffstats
path: root/examples/sql/sqltable/main.cpp
blob: 29cebe51bed0512f8c952ae582bdaa4f2f653339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <tqapplication.h>
#include <tqsqldatabase.h>
#include <tqdatatable.h>
#include <tqsqlcursor.h>
#include <tqmessagebox.h>

/* Modify the following to match your environment */
#define DRIVER       "TQSQLITE"  /* see the TQt 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 TQSqlCursor
{
public:
    SimpleCursor () : TQSqlCursor( "simpletable" ) {}
protected:
    TQSqlRecord* primeInsert()
    {
	/* a real-world application would use sequences, or the like */
	TQSqlRecord* buf = TQSqlCursor::primeInsert();
	TQSqlQuery q( "select max(id)+1 from simpletable" );
	if ( q.next() )
	       buf->setValue( "id", q.value(0) );
	return buf;
    }
};

int main( int argc, char ** argv )
{
    TQApplication a( argc, argv );

    TQSqlDatabase * db = TQSqlDatabase::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")) {
	TQSqlQuery q("create table simpletable(id int, name varchar(20), address varchar(20))", db);
    }

    SimpleCursor cursor;

    TQDataTable 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();
}