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

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