Yurttas/PL/DBL/oracle/F/03/Company-A/DeptLocation-q-all.pc

From ZCubes Wiki
Jump to navigation Jump to search
 1/*  
 2REM
 3REM Deptlocation-q-all.pc
 4REM
 5REM Query DeptLocation table.
 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  int dnumber;
23  char dlocation[24];
24} deptlocation;
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 *
34    FROM DeptLocation;
35
36  EXEC SQL OPEN c;
37
38  EXEC SQL WHENEVER NOT FOUND DO BREAK;
39
40  int dnumber;
41  char dlocation[24];
42  printf("\ndnumber\tdloaction\n");
43  for(;;) {
44    EXEC SQL FETCH c INTO :deptlocation;
45    printf("%d\t%s\n", deptlocation.dnumber,
46                       deptlocation.dlocation);
47  }
48
49  EXEC SQL CLOSE c;
50
51  EXEC SQL COMMIT WORK RELEASE;
52
53  exit(0);
54}
55
56
57void sql_error(char* msg) {
58  char err_msg[128];
59  int buflen, msglen;
60
61  EXEC SQL WHENEVER SQLERROR CONTINUE;
62
63  printf("%s \n", msg);
64  buflen = sizeof(err_msg);
65  sqlglm(err_msg, &buflen, &msglen);
66  printf("%.*s \n", msglen, err_msg);
67  exit(1);
68}
69
70
71void connect_to_oracle() {
72  /* get your username/passwd from "user_pwd.txt" file */
73
74  FILE *in_file;
75  in_file = fopen("user_pwd.txt", "r");
76  fscanf(in_file, "%s", u_name);
77  fscanf(in_file, "%s", pwd);
78
79  strncpy((char *) username.arr, u_name, UNAME_LEN);
80  username.len = strlen((char *) username.arr);
81
82  strncpy((char *) password.arr, pwd, PWD_LEN);
83  password.len = strlen((char *) password.arr);
84
85  EXEC SQL WHENEVER SQLERROR DO sql_error ("ORACLE error-- ");
86  EXEC SQL CONNECT :username IDENTIFIED BY :password;
87
88  printf("connected to oracle - \n");
89}