Yurttas/PL/DBL/oracle/F/03/Company-A/DeptLocation-ins.pc
Jump to navigation
Jump to search
1/*
2REM
3REM
4REM DeptLocation-ins.pc
5REM
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 FILE *in_file;
31
32 char in_filename[32];
33
34 if(argc<2) {
35 printf("Enter the input filename : ");
36 scanf("%s", in_filename);
37 }
38 else strcpy(in_filename, argv[1]);
39
40 in_file = fopen(in_filename, "r");
41
42 if(in_file==NULL)
43 printf("File named as %s is not opened for input.\n", in_filename);
44 else {
45 connect_to_oracle();
46
47 EXEC SQL
48 WHENEVER SQLERROR DO sql_error("Error in INSERT_table: DeptLocation");
49
50 while(fscanf(in_file,"%d %10c\n",&deptlocation.dnumber,
51 deptlocation.dlocation)!=EOF){
52 EXEC SQL
53 INSERT INTO DeptLocation
54 VALUES(:deptlocation.dnumber,
55 :deptlocation.dlocation);
56 }
57 }
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}