Yurttas/PL/DBL/oracle/F/03/Movie/Movie-ins.pc

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