File: S003_multi.cpp

package info (click to toggle)
xbsql 0.11-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,524 kB
  • ctags: 709
  • sloc: sh: 9,259; cpp: 5,356; yacc: 668; xml: 186; makefile: 49; perl: 33
file content (126 lines) | stat: -rw-r--r-- 3,313 bytes parent folder | download | duplicates (3)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/***************************************************************************
    file                 : S003_multi.cpp
    begin                : Wed 07-Mar-01
    copyright            : (C) 2001 by Mike Richardson
    email                : mike@quaking.demon.co.uk
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; you may use the current    *
 *   version as of 07-Mar-2001, or at your discretion, any later version.  *
 *                                                                         *
 ***************************************************************************/

#include	<stdio.h>

#include	"xbsql.h"

int	main
	(	int	,
		char	*[]
	)
{

	XBaseSQL xb (".") ;

	if (!xb.execCommand ("create table T003_multi (i int(10) unique index, v char(80))"))
	{
		fprintf	(stderr, "S003_multi: create: %s\n", xb.lastError()) ;
		exit	(1) ;
	}

	XBSQLSelect	*select	;
	XBSQLInsert	*insert	;
	XBSQLUpdate	*update	;
	XBSQLDelete	*delrec	;

	select	= xb.openSelect ("select * from T003_multi") ;
	insert	= xb.openInsert ("insert into T003_multi values (?, ?)") ;
	update	= xb.openUpdate ("update T003_multi set v = ? where i = ?") ;
	delrec	= xb.openDelete	("delete from T003_multi where i = ?") ;

	XBSQLValue	v[2]	;

	if (!select->execute (0, 0))
	{
		fprintf	(stderr, "S003_multi: select: %s\n", xb.lastError()) ;
		exit	(1) ;
	}
	fprintf	(stdout, "select: %d rows\n", select->getNumRows()) ;

	for (int r = 0 ; r < 4 ; r += 1)
	{
		char	b[32]	;

		sprintf	(b, "text_%03d", r) ;
		v[0]	= r	;
		v[1]	= b	;

		if (!insert->execute (2, v))
		{
			fprintf	(stderr, "S003_multi: insert: %s\n", xb.lastError()) ;
			exit	(1) ;
		}
		fprintf	(stdout, "insert: %d rows\n", insert->getNumRows()) ;
	}

	if (!select->execute (0, 0))
	{
		fprintf	(stderr, "S003_multi: select: %s\n", xb.lastError()) ;
		exit	(1) ;
	}
	fprintf	(stdout, "select: %d rows\n", select->getNumRows()) ;

	v[0] = "one" ;
	v[1] = 1     ;

	if (!update->execute (2, v))
	{
		fprintf	(stderr, "S003_multi: update: %s\n", xb.lastError()) ;
		exit	(1) ;
	}
	fprintf	(stdout, "update: %d rows\n", update->getNumRows()) ;

	v[0] = 2 ;

	if (!delrec->execute (1, v))
	{
		fprintf	(stderr, "S003_multi: delete: %s\n", xb.lastError()) ;
		exit	(1) ;
	}
	fprintf	(stdout, "detete: %d rows\n", delrec->getNumRows()) ;

	if (!select->execute (0, 0))
	{
		fprintf	(stderr, "S003_multi: select: %s\n", xb.lastError()) ;
		exit	(1) ;
	}
	fprintf	(stdout, "select: %d rows\n", select->getNumRows()) ;

	for (uint r = 0 ; r < select->getNumRows() ; r += 1)
	{
		for (uint c = 0 ; c < select->getNumFields() ; c += 1)
		{
			if (c > 0) fprintf (stdout, ",") ;
			fprintf	(stdout, "\"%s\"", select->getField(r, c).getText()) ;
		}
		fprintf	(stdout, "\n") ;
	}

	if (!xb.execCommand ("drop table T003_multi"))
	{
		fprintf	(stderr, "S003_multi: drop  : %s\n", xb.lastError()) ;
		exit	(1) ;
	}

	delete	select	;
	delete	insert	;
	delete	update	;
	delete	delrec	;

	return	0 ;
}