Yurttas/PL/DBL/oracle/F/03/SPJ/q06.pc
Jump to navigation
Jump to search
1/*
2REM
3REM q06.pc
4REM
5REM Get the total quantity of part 'P01' supplied by
6REM supplier 'S01'.
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 int totalquantity;
24} shipments;
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 SUM(quantity)
34 FROM Shipments
35 WHERE pn = 'P01'
36 AND sn = 'S01';
37
38 EXEC SQL OPEN c;
39
40 EXEC SQL WHENEVER NOT FOUND DO BREAK;
41
42 printf("\nquantity\n");
43 for(;;) {
44 EXEC SQL FETCH c INTO :shipments;
45 printf("%d\n", shipments.quantity);
46 }
47
48 EXEC SQL CLOSE c;
49
50 EXEC SQL COMMIT WORK RELEASE;
51
52 exit(0);
53}
54
55
56void sql_error(char* msg) {
57 char err_msg[128];
58 int buflen, msglen;
59
60 EXEC SQL WHENEVER SQLERROR CONTINUE;
61
62 printf("%s \n", msg);
63 buflen = sizeof(err_msg);
64 sqlglm(err_msg, &buflen, &msglen);
65 printf("%.*s \n", msglen, err_msg);
66 exit(1);
67}
68
69
70void connect_to_oracle() {
71 /* get your username/passwd from "user_pwd.txt" file */
72
73 FILE *in_file;
74 in_file = fopen("user_pwd.txt", "r");
75 fscanf(in_file, "%s", u_name);
76 fscanf(in_file, "%s", pwd);
77
78 strncpy((char *) username.arr, u_name, UNAME_LEN);
79 username.len = strlen((char *) username.arr);
80
81 strncpy((char *) password.arr, pwd, PWD_LEN);
82 password.len = strlen((char *) password.arr);
83
84 EXEC SQL WHENEVER SQLERROR DO sql_error ("ORACLE error-- ");
85 EXEC SQL CONNECT :username IDENTIFIED BY :password;
86
87 printf("connected to oracle - \n");
88}