D1 - Relational Query Languages Application Development...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Relational Query Languages Application Development “Programming with SQL”
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS174A: Database Programming 2 SQL in Application Programs ± SQL commands need be called from within a host language (C, C++, JAVA, COBOL, PHP, …) program Must include a statement to CONNECT to the right database JDBC : JAVA variables Embedded SQL : host variables (including special variables used to return status) ± Two main approaches: Embed SQL in the host language (Embedded SQL, SQLJ) Create special API to call SQL commands (JDBC)
Background image of page 2
CS174A: Database Programming 3 SQL and Programming Languages ± Impedance mismatch: SQL relations are (multi-)sets of records, with no a priori bound on the number of records No such data structure in (early) programming languages ± JDBC : JAVA classes ± ESQL : a mechanism called a cursor to handle this
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS174A: Database Programming 4 Embedded SQL ± Approach: Embed SQL in the host language A preprocessor converts the SQL statements into special API calls Then a regular compiler is used to compile the code
Background image of page 4
CS174A: Database Programming 5 Embedded SQL SQL Library Preprocessor Host Language Program + SQL Host Language program + Function Calls Host Language Compiler Executable Program
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS174A: Database Programming 6 Key Language Constructs in ESQL ± Connecting to a database: EXEC SQL CONNECT ± Declaring variables: EXEC SQL BEGIN ( END ) DECLARE SECTION ± Statements: EXEC SQL Statement
Background image of page 6
CS174A: Database Programming 7 Host Variables ± Two special “error” variables: SQLCODE ( LONG ) is negative if an error has occurred SQLSTATE ( CHAR[6] ) contains predefined codes for common errors EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; long c_sid; short c_rating; float c_age; EXEC SQL END DECLARE SECTION
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS174A: Database Programming 8 Host Variables in ESQL-C void printSailorName ( ) { EXEC SQL BEGIN DECLARE SECTION; char c_sname[20]; long c_sid; char SQLSTATE[5]; EXEC SQL END DECLARE SECTION; EXEC SQL SELECT S.sname INTO : c_sname FROM Sailors S WHERE S.sid = : c_sid ; /* check if SQLSTATE has all 0’s and if so, print the value of sname */ } ± Single-row selection statements
Background image of page 8
CS174A: Database Programming 9 ± Can declare a cursor on a relation or a query (which returns a relation) ± Can open a cursor, and repeatedly fetch a tuple then move the cursor, until all tuples have been retrieved Can use a special clause, called ORDER BY , in queries that are accessed through a cursor, to control the order in which tuples are returned ± Fields in ORDER BY clause must also appear in SELECT clause The ORDER BY clause, which orders answer tuples, is only allowed in the context of a cursor ± Can also modify/delete tuple pointed to by a cursor Cursors
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

D1 - Relational Query Languages Application Development...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online