Yurttas/PL/DBL/oracle/F/03/PC/Laptop-ins.pc
Revision as of 00:02, 5 November 2013 by MassBot1 (talk | contribs) (Created page with "<syntaxhighlight lang="text" line start="1" enclose="div"> →REM REM REM Laptop-ins.pc REM REM: #include <stdio.h> #include <sqlca.h> #define UNAME_LEN 32 #define PWD_LEN...")
1/*
2REM
3REM
4REM Laptop-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 model[6];
23 int speed;
24 int ram;
25 float hd;
26 float screen;
27 int price;
28} laptop;
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: Laptop");
53
54 while(fscanf(in_file,"%s%d%d%f%f%d\n",laptop.model,
55 &laptop.speed,
56 &laptop.ram,
57 &laptop.hd,
58 &laptop.screen,
59 &laptop.price)!=EOF){
60 EXEC SQL
61 INSERT INTO Laptop
62 VALUES(:laptop.model,
63 :laptop.speed,
64 :laptop.ram,
65 :laptop.hd,
66 :laptop.screen,
67 :laptop.price);
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("application is done\n");
109}