source: liacs/db/test/main.pc@ 23

Last change on this file since 23 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

  • Property svn:executable set to *
File size: 6.1 KB
RevLine 
[2]1// Rick van der Zwet
2// Databases opdracht 3
3// SN: 0433373
4
5
6//c libaries
7#include <cstdio>
8#include <string>
9
10//oracle sql libaries
11#include <sqlcpr.h>
12#include <sqlca.h>
13
14
15
16
17// Declare error handling function.-
18void sql_error(char *msg)
19{
20 char err_msg[128];
21 size_t buf_len, msg_len;
22
23 EXEC SQL WHENEVER SQLERROR CONTINUE;
24
25 // Print the error message:
26 cout << msg << endl;
27 buf_len = sizeof (err_msg);
28 sqlglm(err_msg, &buf_len, &msg_len);
29 cout << msg_len << "*" << err_msg << endl;
30
31 // Make sure the database is consistent:
32 EXEC SQL ROLLBACK RELEASE;
33
34 // Stop the program.
35 exit(EXIT_FAILURE);
36} //end sql_error
37
38
39
40//
41// clear input field
42void digestline(void) {
43 scanf("%*[^\n]"); /* Skip to the End of the Line */
44 scanf("%*1[\n]"); /* Skip One Newline */
45} //end digestline
46
47
48
49
50void opdr1() {
51 printf("Uitvoer opdracht 1...\n");
52 EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
53 EXEC SQL BEGIN DECLARE SECTION;
54 struct
55 {
56 char Sname[20];
57 } result;
58 EXEC SQL END DECLARE SECTION;
59
60 EXEC SQL DECLARE names_1 CURSOR FOR
61 SELECT Sname
62 FROM Sportsman S, Coaches C
63 WHERE S.Scountry = 'Norway' AND S.Sage > 25 AND S.Sid = C.Sid;
64
65 EXEC SQL OPEN names_1;
66 EXEC SQL WHENEVER NOT FOUND DO break;
67
68 printf("NAAM\n");
69 printf("----\n");
70 for (;;)
71 {
72 EXEC SQL FETCH names_1
73 INTO :result;
74 printf("%s\n", result.Sname);
75 }
76 EXEC SQL CLOSE names_1;
77}
78
79
80
81void opdr2() {
82 printf("Uitvoer opdracht 2...\n");
83 EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
84 EXEC SQL BEGIN DECLARE SECTION;
85 struct
86 {
87 char Cname[20];
88 } result;
89 EXEC SQL END DECLARE SECTION;
90
91 EXEC SQL DECLARE names_2 CURSOR FOR
92 SELECT Cname
93 FROM Coach C, Coaches Cs, Sportsman S, (SELECT MIN(C2.Cage) AS minimum
94 FROM Coach C2 ) Age
95 WHERE C.Cage != Age.minimum AND C.Ccountry = S.Scountry AND C.Cid = Cs.Cid AND Cs.Sid = S.Sid;
96
97 EXEC SQL OPEN names_2;
98 EXEC SQL WHENEVER NOT FOUND DO break;
99
100 printf("NAAM\n");
101 printf("----\n");
102 for (;;)
103 {
104 EXEC SQL FETCH names_2
105 INTO :result;
106 printf("%s\n", result.Cname);
107 }
108
109 EXEC SQL CLOSE names_2;
110}
111
112void opdr3() {
113 printf("Uitvoer opdracht 3...\n");
114 EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
115 EXEC SQL BEGIN DECLARE SECTION;
116 struct
117 {
118 char Sname[20];
119 } result;
120 EXEC SQL END DECLARE SECTION;
121
122 EXEC SQL DECLARE names_3 CURSOR FOR
123 SELECT Sname
124 FROM Sportsman S
125 WHERE RTRIM(S.Sname) LIKE 'K%t%e';
126
127 EXEC SQL OPEN names_3;
128 EXEC SQL WHENEVER NOT FOUND DO break;
129
130 printf("NAAM\n");
131 printf("----\n");
132 for (;;)
133 {
134 EXEC SQL FETCH names_3
135 INTO :result;
136 printf("%s\n", result.Sname);
137 }
138
139 EXEC SQL CLOSE names_3;
140}
141
142
143void opdr4() {
144 printf("Uitvoer opdracht 4...\n");
145 EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
146 EXEC SQL BEGIN DECLARE SECTION;
147 struct
148 {
149 char Sname[20];
150 char Cname[20];
151 } result;
152 EXEC SQL END DECLARE SECTION;
153
154 EXEC SQL DECLARE names_4 CURSOR FOR
155 SELECT S.Sname, C.Cname
156 FROM Sportsman S, Coaches Cs, Coach C
157 WHERE Cs.Cid = C.Cid AND Cs.Sid = S.Sid AND ABS(S.Sage - C.Cage) < 4;
158
159 EXEC SQL OPEN names_4;
160 EXEC SQL WHENEVER NOT FOUND DO break;
161
162 printf("SPORTSMAN , COACH\n");
163 printf("-----------------\n");
164 for (;;)
165 {
166 EXEC SQL FETCH names_4
167 INTO :result;
168 printf("%s , %s\n", result.Sname, result.Cname);
169 }
170}
171
172
173void opdr5() {
174 printf("Uitvoer opdracht 5...\n");
175 EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
176 EXEC SQL BEGIN DECLARE SECTION;
177 struct
178 {
179 char Cname[20];
180 } result;
181 EXEC SQL END DECLARE SECTION;
182
183 EXEC SQL DECLARE names_5 CURSOR FOR
184 SELECT C.Cname
185 FROM Coach C
186 WHERE C.Ccountry = 'Kazakhstan' AND C.Cid = (SELECT T.Cid
187 FROM Coaches T
188 WHERE T.Cid = C.Cid
189 GROUP BY T.Cid
190 HAVING COUNT(*) > 3 );
191
192 EXEC SQL OPEN names_5;
193 EXEC SQL WHENEVER NOT FOUND DO break;
194
195 printf("COACH\n");
196 printf("-----\n");
197 for (;;)
198 {
199 EXEC SQL FETCH names_5
200 INTO :result;
201 printf("%s\n", result.Cname);
202 }
203}
204
205
206// Main module
207//
208int main() {
209
210 char username[10] = "";
211 char password[10] = "";
212 char connect[20] = "";
213 int choice, args = 0;
214
215 EXEC SQL BEGIN DECLARE SECTION;
216 VARCHAR connect_str[20];
217 struct
218 {
219 char tname[20];
220 } tables;
221 EXEC SQL END DECLARE SECTION;
222
223 printf("Please enter username: ");
224 scanf("%s",username);
225 printf ("Please enter password: ");
226 scanf("%s",password);
227 strcpy(connect, username);
228 strcat(connect, "/");
229 strcat(connect, password);
230 strcat(connect, "@ONW");
231 printf("Using %s", connect);
232
233 strcpy((char *) connect_str.arr, connect);
234 connect_str.len = (unsigned short) strlen((char *) connect_str.arr);
235
236 EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
237
238 EXEC SQL CONNECT :connect_str;
239
240 do
241 {
242 printf("1: Names of Sportsmen, Norway, older 25, at least one coach\n");
243 printf("2: Names of coaches, sportsman own country, not youngest coach\n");
244 printf("3: Names of all sportsman, name start with 'K', includes 't', ends with 'e'\n");
245 printf("4: Names of sportsman and coach, 'team', age differences 4 year\n");
246 printf("5: Names of coaches, from Kazakhstan, coach over 3 persons\n");
247 printf("\n");
248 printf("9: quit\n");
249 printf("Please enter your choice: ");
250 if ( scanf("%d", &choice) > 0 ) {
251 switch( choice ) {
252 case 1:
253 opdr1();
254 break;
255 case 2:
256 opdr2();
257 break;
258 case 3:
259 opdr3();
260 break;
261 case 4:
262 opdr4();
263 break;
264 case 5:
265 opdr5();
266 break;
267 case 9:
268 printf("Goodbye..\n");
269 break;
270 default:
271 printf("Sorry %d ain't a good number\n", choice);
272 }
273 }
274 else
275 {
276 printf("Sorry input not valid\n");
277 choice = 0;
278 }
279 digestline();
280 } while ( choice != 9);
281
282 //close connection
283 EXEC SQL COMMIT WORK RELEASE;
284 exit(0);
285}
Note: See TracBrowser for help on using the repository browser.