Yurttas/PL/DBL/oracle/F/03/PC/q10.pc
Jump to navigation
Jump to search
1/*
2REM
3REM q10.pc
4REM
5REM Find the maker of the printer with the fastest processor
6REM among all those PC's that have the smallest amount of RAM.
7REM
8*/
9
10#include <stdio.h>
11#include <sqlca.h>
12
13#define UNAME_LEN 32
14#define PWD_LEN 16
15
16VARCHAR username[UNAME_LEN]; /* VARCHAR is an Oracle-supplied struct */
17VARCHAR password[PWD_LEN]; /* varchar can be in lower case also. */
18
19char u_name[UNAME_LEN];
20char pwd[PWD_LEN];
21
22struct {
23 char maker[6];
24} smallram;
25
26void connect_to_oracle();
27void sql_error();
28
29void main(int argc, char* argv[]) {
30 connect_to_oracle();
31
32 EXEC SQL
33 CREATE VIEW SmallRam(model,ram)
34 AS (SELECT model, ram
35 FROM PC
36 WHERE ram IN (SELECT MIN(ram)
37 FROM PC));
38
39 EXEC SQL DECLARE c CURSOR FOR
40 SELECT P.maker
41 FROM Product P, PC C, SmallRam S
42 WHERE P.model=C.model
43 AND C.model=S.model
44 AND C.speed IN (SELECT MAX(speed)
45 FROM PC, SmallRam S
46 WHERE PC.model= S.model);
47
48 EXEC SQL OPEN c;
49
50 EXEC SQL WHENEVER NOT FOUND DO BREAK;
51
52 printf("\nmaker\n");
53 for(;;) {
54 EXEC SQL FETCH c INTO :smallram;
55 printf("%s\n", smallram.maker);
56 }
57
58 EXEC SQL CLOSE c;
59
60 EXEC SQL COMMIT WORK RELEASE;
61
62 exit(0);
63}
64
65
66void sql_error(char* msg) {
67 char err_msg[128];
68 int buflen, msglen;
69
70 EXEC SQL WHENEVER SQLERROR CONTINUE;
71
72 printf("%s \n", msg);
73 buflen = sizeof(err_msg);
74 sqlglm(err_msg, &buflen, &msglen);
75 printf("%.*s \n", msglen, err_msg);
76 exit(1);
77}
78
79
80void connect_to_oracle() {
81 /* get your username/passwd from "user_pwd.txt" file */
82
83 FILE *in_file;
84 in_file = fopen("user_pwd.txt", "r");
85 fscanf(in_file, "%s", u_name);
86 fscanf(in_file, "%s", pwd);
87
88 strncpy((char *) username.arr, u_name, UNAME_LEN);
89 username.len = strlen((char *) username.arr);
90
91 strncpy((char *) password.arr, pwd, PWD_LEN);
92 password.len = strlen((char *) password.arr);
93
94 EXEC SQL WHENEVER SQLERROR DO sql_error ("ORACLE error-- ");
95 EXEC SQL CONNECT :username IDENTIFIED BY :password;
96
97 printf("connected to oracle - \n");
98}