
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Static Embedded SQL, Database Applications, How does Client/server Work?, Embedded SQL, Application Structure, Variables, Errors, Exception Handling, Dummy Application, Real SQL Statements, Indicator Variables, Impedance Mismatch, Application with a Cursor, Updates, Queries
Typology: Slides
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















School of Computer Science University of Waterloo
#include <stdio.h> #include "util.h"
int main(int argc, char (^) *argv[]) { EXEC SQL BEGIN DECLARE SECTION; char db[6] = "cs448"; EXEC SQL END DECLARE SECTION; printf("Sample C program: CONNECT\n" ); EXEC SQL WHENEVER SQLERROR GO TO error; EXEC SQL CONNECT TO :db; printf("Connected to DB2\n"); // do your stuff here EXEC SQL COMMIT; EXEC SQL CONNECT reset; exit(0); error: check_error("My error",&sqlca); EXEC SQL WHENEVER SQLERROR CONTINUE; EXEC SQL ROLLBACK; EXEC SQL CONNECT reset; exit(1); }
#include <stdio.h>
int main(int argc, char (^) *argv[]) { EXEC SQL BEGIN DECLARE SECTION; char user[6] = "cs448"; char pwd[10]; EXEC SQL END DECLARE SECTION; printf("Sample C program: CONNECT\n" ); strncpy(pwd,getpass("Password: "),10); EXEC SQL WHENEVER SQLERROR GO TO error; EXEC SQL CONNECT :user IDENTIFIED BY :pwd; printf("Connected to Oracle\n"); // do your stuff here EXEC SQL COMMIT RELEASE; exit(0); error: sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] =’\0’; printf("MyError %s\n", sqlca.sqlerrm.sqlerrmc); EXEC SQL WHENEVER SQLERROR CONTINUE; EXEC SQL ROLLBACK RELEASE; exit(1); }
bash$ make NAME=sample db2 connect to cs
Database server = DB2/SUN 6.1. SQL authorization ID = DAVID Local database alias = CS
db2 prep sample1.sqc bindfile LINE MESSAGES FOR sample1.sqc
SQL0060W The "C" precompiler is in progress. SQL0091W Precompilation or binding was ended with "0" errors and "0" warnings. db2 bind sample1.bnd LINE MESSAGES FOR sample1.bnd
SQL0061W The binder is in progress. SQL0091N Binding was ended with "0" errors and "0" warnings. db2 connect reset DB20000I The SQL command completed successfully. cc -I/usr/db2/include -c sample1.c cc -I/usr/db2/include -o sample1 sample1.o util.o -L/usr/db2/lib -R/usr/db2/lib -ldb
main(int argc, char (^) *argv[]) { ... printf("Connected to DB2\n"); for (i=1; i<argc; i++) { strncpy(pubid,argv[i],8);
EXEC SQL WHENEVER NOT FOUND GO TO nope;
EXEC SQL SELECT title INTO :title FROM publication WHERE pubid = :pubid;
printf("%10s: %s\n",pubid,title); continue; nope: printf("%10s: (^) *** not found (^) *** \n",pubid); }; ... }
main(int argc, char (^) *argv[]) { ... strncpy(apat,argv[1],8);
EXEC SQL DECLARE author CURSOR FOR SELECT name, title FROM author , wrote, publication WHERE name LIKE :apat AND aid=author AND pubid=publication;
EXEC SQL OPEN author; EXEC SQL WHENEVER NOT FOUND GO TO end; for (;;) { EXEC SQL FETCH author INTO :name, title; printf("%10s -> %20s: %s\n",apat,name,title); }; end: ... }