Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Similar presentations


Presentation on theme: "SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006."— Presentation transcript:

1 SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006

2 Lecture 22© CIS 4301 - Spring 20062 SQL in a Program You're starting by using "direct" (or "interactive") SQL In reality SQL is rarely used in this way, instead it's specified within programs Host language (e.g., ADA, C, Cobol, Fortran, M, Pascal) Some of the steps are actually SQL statements Entire program sent to preprocessor, changes the embedded SQL into something that makes sense in host language Pre-processed program is then compiled in usual manner DBMS vendor normally provides library that supplies necessary function definitions Call-level interface (CLI)

3 Lecture 22© CIS 4301 - Spring 20063 Processing Programs with SQL Statements Host Language + Embedded SQL Preprocessor Host Language + Function Calls Host-language compiler Object-code program SQL library

4 Lecture 22© CIS 4301 - Spring 20064 Impedance Mismatch Why not use a single language SQL? Conventional language? Many things SQL cannot do… Example? Also, conventional progr. language not suitable for letting humans write database operations that can be executed efficiently… Why not? Need both, but must deal with disconnect between the data models used Rel. model uses sets, not naturally supported by programming languages Programming lang. support pointers, loops, branches, not available in SQL Passing data back and forth between SQL and other prog. languages is not straightforward

5 Lecture 22© CIS 4301 - Spring 20065 SQL/Host Language Interface How are values passed from the program into SQL commands? How are results of SQL commands returned into program variables? How do we deal with set-valued (i.e., relation-valued) results? Shared variables Variables of the host languages DECLARE Section Identify in program by preceding variable with colon Special variable SQLSTATE connects the host language with the SQL execution system ‘00000’  no error ‘02000’  no tuple found

6 Lecture 22© CIS 4301 - Spring 20066 The Declare Section EXEC SQL BEGIN DECLARE SECTION; char studioName[50], studioAddr[256]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

7 Lecture 22© CIS 4301 - Spring 20067 Embedded SQL SQL statements that do not return a result can be embedded directly into a host language using EXEC SQL … syntax EXEC SQL INSERT INTO Studio (name, address) VALUES (:studioName, :studioAddress);

8 Lecture 22© CIS 4301 - Spring 20068 Sample C Function void getStudio() { EXEC SQL BEGIN DECLARE SECTION; char studioName[50], studioAddr[256]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* code for handling user input… */ EXEC SQL INSERT INTO STUDIO(name, address) VALUES (:studioName, :studioAddr); }

9 Lecture 22© CIS 4301 - Spring 20069 Embedded SQL So far, statements return no results No impedance mismatch – EASY SFW queries must use one of two mechanisms Single-row select using shared variables to hold components of single tuple being returned Queries producing more than one tuple must declare cursor

10 Lecture 22© CIS 4301 - Spring 200610 Single-Row Select Statement Void printNetWorth() { EXEC SQL BEGIN DECLARE SECTION; char studioName[50]; int presNetWorth; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* print request that studio name be entered, read response into studioName */ EXEC SQL SELECT netWorth INTO :presNetWorth FROM Studio, MovieExec WHERE presC# = cert# AND Studio.name = :studioName; /* print value of presNetWorth */ } Declaration of shared vars

11 Lecture 22© CIS 4301 - Spring 200611 Cursors For set-valued results: associate handle (cursor) with query, get tuples in result one at a time through handle Example: Print names and GPAs of all students who applied to Santa Cruz Step 1: declare cursor EXEC BEGIN DECLARE SECTION; char n[30]; float g; EXEC END DECLARE SECTION; EXEC DECLARE Scinfo CURSOR FOR (SELECT DISTINCT name, GPA FROM Student, Apply WHERE Student.ID = Apply.ID AND Apply.location = "Santa Cruz");

12 Lecture 22© CIS 4301 - Spring 200612 General Cursor Declaration EXEC SQL DECLARE CURSOR FOR

13 Lecture 22© CIS 4301 - Spring 200613 Using a Cursor Step 2: run query using iteration EXEC SQL OPEN SCinfo;  initializes cursor EXEC SQL FETCH FROM SCinfo INTO :n, :g; WHILE (MORE_TUPLES) Print(:n, :g); EXEC SQL FETCH SCinfo INTO :n,:g; ENDWHILE; EXEC SQL CLOSE SCinfo;  closes cursor #define MORE_TUPLES !(strcmp(SQLSTATE,”02000”))

14 Lecture 22© CIS 4301 - Spring 200614 General Fetch Statement EXEC SQL FETCH FROM INTO

15 Lecture 22© CIS 4301 - Spring 200615 Other Uses of Cursor Cursor can be used to modify or delete tuples See example in FCDBS

16 Lecture 22© CIS 4301 - Spring 200616 Scrolling Cursors Default movement through cursor is forward, one tuple at a time Option to move in different directions When declaring cursor, declare cursor as scrollable Use FETCH in conjunction with direction keyword NEXT or PRIOR FIRST or LAST RELATIVE ABSOLUTE

17 Lecture 22© CIS 4301 - Spring 200617 Dynamic SQL So far, model was to embed specific SQL queries and commands within host-language program Alternatively, can have host language compute and assemble SQL statements Since not known at compile time, cannot be handled by SQL preprocessor or host-language compiler Dynamic SQL Example? Host-language program must instruct SQL system to take char string, turn it into executable SQL statement, and finally to execute that statement

18 Lecture 22© CIS 4301 - Spring 200618 A very simple sqlplus Void vssqlplus () { EXEC SQL BEGIN DECLARE SECTION; char *query; EXEC SQL END DECLARE SECTION; /* prompt user for a query, allocate space and make shared variable :query point to first char of query */ EXEC SQL PREPARE SQLquery FROM :query; EXEC SQL EXECUTE SQLquery; /* code for displaying result to user */ } EXEC SQL EXECUTE IMMEDIATE :query;


Download ppt "SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006."

Similar presentations


Ads by Google