Yurttas/PL/DBL/oracle/F/03/Company-A/Employee-ins.pc

From ZCubes Wiki
Jump to navigation Jump to search
  1/*
  2REM
  3REM
  4REM Employee-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  char fname[12];
 23  char minit;
 24  char lname[16];
 25  char ssn[10];
 26  char bdate[10];
 27  char address[24];
 28  char sex;
 29  float salary;
 30  char superssn[10];
 31  int dno;
 32} employee;
 33
 34void connect_to_oracle();
 35void sql_error();
 36
 37void main(int argc, char* argv[]) {
 38  FILE *in_file;
 39
 40  char in_filename[32];
 41
 42  if(argc<2) {
 43    printf("Enter the input filename : ");
 44    scanf("%s", in_filename);
 45  }
 46  else strcpy(in_filename, argv[1]);
 47
 48  in_file = fopen(in_filename, "r");
 49
 50  if(in_file==NULL)
 51    printf("File named as %s is not opened for input.\n", in_filename);
 52  else {
 53    connect_to_oracle();
 54
 55    EXEC SQL
 56      WHENEVER SQLERROR DO sql_error("Error in INSERT_table: Employee");
 57
 58    while(fscanf(in_file,"%s %c %s %s %s %22c %c %f %s %d\n",employee.fname,
 59                                                            &employee.minit,
 60                                                             employee.lname,
 61                                                             employee.ssn,
 62                                                             employee.bdate,
 63                                                             employee.address,
 64                                                            &employee.sex,
 65                                                            &employee.salary,
 66                                                             employee.superssn,
 67                                                            &employee.dno)!=EOF){
 68      EXEC SQL
 69        INSERT INTO Employee
 70          VALUES(:employee.fname,
 71                 :employee.minit,
 72                 :employee.lname,
 73                 :employee.ssn,
 74                 :employee.bdate,
 75                 :employee.address,
 76                 :employee.sex,
 77                 :employee.salary,
 78                 :employee.superssn,
 79                 :employee.dno);
 80    }
 81  }
 82
 83  EXEC SQL COMMIT WORK RELEASE;
 84
 85  exit(0);
 86}
 87
 88
 89void sql_error(char* msg) {
 90  char err_msg[128];
 91  int buflen, msglen;
 92
 93  EXEC SQL WHENEVER SQLERROR CONTINUE;
 94
 95  printf("%s \n", msg);
 96  buflen = sizeof(err_msg);
 97  sqlglm(err_msg, &buflen, &msglen);
 98  printf("%.*s \n", msglen, err_msg);
 99  exit(1);
100}
101
102
103void connect_to_oracle() {
104  /* get your username/passwd from "user_pwd.txt" file */
105
106  FILE *in_file;
107  in_file = fopen("user_pwd.txt", "r");
108  fscanf(in_file, "%s", u_name);
109  fscanf(in_file, "%s", pwd);
110
111  strncpy((char *) username.arr, u_name, UNAME_LEN);
112  username.len = strlen((char *) username.arr);
113
114  strncpy((char *) password.arr, pwd, PWD_LEN);
115  password.len = strlen((char *) password.arr);
116
117  EXEC SQL WHENEVER SQLERROR DO sql_error ("ORACLE error-- ");
118  EXEC SQL CONNECT :username IDENTIFIED BY :password;
119
120  printf("connected to oracle - \n");
121}