1/*
2REM
3REM q05.pc
4REM
5REM Find all employees who live in the same City and
6REM on the same Street as their Manager.
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 employeename[16];
24} employee;
25
26void connect_to_oracle();
27void sql_error();
28
29void main(int argc, char* argv[]) {
30 connect_to_oracle();
31
32 EXEC SQL DECLARE c CURSOR FOR
33 SELECT E1.employeename
34 FROM Employee E1, Manages M, Employee E2
35 WHERE E1.employeename = M.employeename
36 AND M.managername = E2.employeename
37 AND E1.Street = E2.Street
38 AND E1.City = E2.City;
39
40 EXEC SQL OPEN c;
41
42 EXEC SQL WHENEVER NOT FOUND DO BREAK;
43
44 printf("\nemployeename\n");
45 for(;;) {
46 EXEC SQL FETCH c INTO :employee;
47 printf("%s\n", employee.employeename);
48 }
49
50 EXEC SQL CLOSE c;
51
52 EXEC SQL COMMIT WORK RELEASE;
53
54 exit(0);
55}
56
57
58void sql_error(char* msg) {
59 char err_msg[128];
60 int buflen, msglen;
61
62 EXEC SQL WHENEVER SQLERROR CONTINUE;
63
64 printf("%s \n", msg);
65 buflen = sizeof(err_msg);
66 sqlglm(err_msg, &buflen, &msglen);
67 printf("%.*s \n", msglen, err_msg);
68 exit(1);
69}
70
71
72void connect_to_oracle() {
73 /* get your username/passwd from "user_pwd.txt" file */
74
75 FILE *in_file;
76 in_file = fopen("user_pwd.txt", "r");
77 fscanf(in_file, "%s", u_name);
78 fscanf(in_file, "%s", pwd);
79
80 strncpy((char *) username.arr, u_name, UNAME_LEN);
81 username.len = strlen((char *) username.arr);
82
83 strncpy((char *) password.arr, pwd, PWD_LEN);
84 password.len = strlen((char *) password.arr);
85
86 EXEC SQL WHENEVER SQLERROR DO sql_error ("ORACLE error-- ");
87 EXEC SQL CONNECT :username IDENTIFIED BY :password;
88
89 printf("connected to oracle - \n");
90}