Yurttas/PL/DBL/oracle/F/03/Company-A/q02.pc
Jump to navigation
Jump to search
1/*
2REM
3REM q02.pc
4REM
5REM List the names of employees who have a dependent with
6REM the same first name as themselves.
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 fname[12];
24 char minit;
25 char lname[16];
26} employee;
27
28void connect_to_oracle();
29void sql_error();
30
31void main(int argc, char* argv[]) {
32 connect_to_oracle();
33
34 EXEC SQL DECLARE c CURSOR FOR
35 SELECT DISTINCT fname, minit, lname
36 FROM Employee, Dependent
37 WHERE ssn = essn
38 AND dependentname = fname;
39
40 EXEC SQL OPEN c;
41
42 EXEC SQL WHENEVER NOT FOUND DO BREAK;
43
44 printf("\nfname\tminit\tlname\n");
45 for(;;) {
46 EXEC SQL FETCH c INTO :employee;
47 printf("%s\t%s\t%s\n", employee.fname,
48 employee.minit,
49 employee.lname);
50 }
51
52 EXEC SQL CLOSE c;
53
54 EXEC SQL COMMIT WORK RELEASE;
55
56 exit(0);
57}
58
59
60void sql_error(char* msg) {
61 char err_msg[128];
62 int buflen, msglen;
63
64 EXEC SQL WHENEVER SQLERROR CONTINUE;
65
66 printf("%s \n", msg);
67 buflen = sizeof(err_msg);
68 sqlglm(err_msg, &buflen, &msglen);
69 printf("%.*s \n", msglen, err_msg);
70 exit(1);
71}
72
73
74void connect_to_oracle() {
75 /* get your username/passwd from "user_pwd.txt" file */
76
77 FILE *in_file;
78 in_file = fopen("user_pwd.txt", "r");
79 fscanf(in_file, "%s", u_name);
80 fscanf(in_file, "%s", pwd);
81
82 strncpy((char *) username.arr, u_name, UNAME_LEN);
83 username.len = strlen((char *) username.arr);
84
85 strncpy((char *) password.arr, pwd, PWD_LEN);
86 password.len = strlen((char *) password.arr);
87
88 EXEC SQL WHENEVER SQLERROR DO sql_error ("ORACLE error-- ");
89 EXEC SQL CONNECT :username IDENTIFIED BY :password;
90
91 printf("connected to oracle - \n");
92}