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