#include #include exec sql include sqlca; EXEC SQL DECLARE video TABLE (video_no i4 not null, video_name c20 not null); struct video_ { int video_no; char video_name[21]; } video; EXEC SQL DECLARE customer TABLE (customer_no i4 not null, (customer_no i4 not null, customer_name c20 not null); struct customer_ { int customer_no; char customer_name[21]; } customer; EXEC SQL DECLARE hire TABLE (video_no i4 not null, customer_no i4 not null); struct hire_ { int video_no; int customer_no; } hire; exec sql begin declare section ; exec sql end declare section ; main() { printf("processing hire table \n") ; initdb() ; process_hire() ; enddb() ; printf("successfully completed application\n") ; } initdb() { char name[30], pass[30]; struct termios t; printf("Username: "); scanf(" %s", name); printf("Password: "); tcgetattr(0, &t); t.c_lflag&=~ECHO; tcsetattr(0, TCSANOW, &t); scanf(" %s", pass); tcgetattr(0, &t); t.c_lflag|=ECHO; tcsetattr(0, TCSANOW, &t); printf("\n"); exec sql whenever sqlerror do close_down() ; exec sql connect :name identified by :pass using :"cse2316"; printf("Have connected to the database \n") ; } enddb() { exec sql commit ; /*exec sql disconnect ;*/ printf("have disconnected and committed\n ") ; } process_hire() { exec sql begin declare section ; struct hire_details_ { int video_no; int customer_no; char video_name[21]; char customer_name[21]; } hire_details; exec sql end declare section ; exec sql declare hire_csr cursor for select hire.video_no, hire.customer_no, video.video_name, customer.customer_name from hire, video, customer where hire.video_no = video.video_no and hire.customer_no = customer.customer_no; exec sql whenever sqlerror do close_down() ; exec sql whenever not found goto close_ordcsr ; exec sql open hire_csr ; printf("\n"); printf("Videos out on hire\n"); printf("==================\n"); printf("\n"); while (sqlca.sqlcode == 0) { exec sql fetch hire_csr into :hire_details ; printf("video %d %20s customer %d %s\n", hire_details.video_no, hire_details.video_name, hire_details.customer_no, hire_details.customer_name); } /* end while */ close_ordcsr: printf("\n"); exec sql whenever not found continue ; exec sql close hire_csr ; } /* end process_hire */ close_down() { exec sql begin declare section ; char errbuf[101] ; size_t errsz, bufsz=100; exec sql end declare section ; exec sql whenever sqlerror continue ; sqlglm(errbuf, &bufsz, &errsz) ; errbuf[errsz]=0; printf("closing down because of database error:\n") ; printf("%s\n",errbuf) ; exec sql rollback ; /*exec sql disconnect ;*/ exit(-1) ; }