Docsity

CS 180: Winter 2002 - Lecture Notes on Embedded SQL, Cursors, and Call-Level Interfaces, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
Winter 2002 Arthur Keller – CS 180 11–1
Schedule
•Today: Feb. 7 (TH)
◆PL/SQL, Embedded SQL, CLI, JDBC.
◆Read Sections 8.1, 8.3-8.5.
•Feb. 12 (T) Advising Day. No class.
•Reminder: Midterm is Feb. 14 (TH)
◆Covers material through Feb. 7 (TH) lecture and readings
(Chapters 1-3, 5-7, 8.1-8.5).
•Feb. 19 (T)
◆Object-Relational Systems.
◆Read Sections 4.5, 9.4-9.5. Assignment 5 due.
•Feb. 21 (TH)
◆Transactions, Authorization.
◆Read Sections 8.6-8.7. Project Part 5 due.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download CS 180: Winter 2002 - Lecture Notes on Embedded SQL, Cursors, and Call-Level Interfaces and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

Winter 2002

Arthur Keller – CS 180

Schedule

•^ Today: Feb. 7 (TH)

◆^ PL/SQL, Embedded SQL, CLI, JDBC. ◆^ Read Sections 8.1, 8.3-8.5.

•^ Feb. 12 (T) Advising Day. No class.•^ Reminder: Midterm is Feb. 14 (TH)

◆^ Covers material through Feb. 7 (TH) lecture and readings(Chapters 1-3, 5-7, 8.1-8.5).

•^ Feb. 19 (T)

◆^ Object-Relational Systems. ◆^ Read Sections 4.5, 9.4-9.5. Assignment 5 due.

•^ Feb. 21 (TH)

◆^ Transactions, Authorization. ◆^ Read Sections 8.6-8.7. Project Part 5 due.

Winter 2002

Arthur Keller – CS 180

Embedded SQL

Add to a conventional programming language(C in our examples) certain statements thatrepresent SQL operations.• Each embedded SQL statement introducedwith

EXEC

SQL

• Preprocessor converts C + SQL to pure C.

◆^ SQL statements become procedure calls.

Winter 2002

Arthur Keller – CS 180

Example

Find the price for a given beer at a given bar.

Sells(bar, beer, price)EXEC SQL BEGIN DECLARE SECTION;char

theBar[21],

theBeer[21];

float

thePrice;

EXEC SQL END DECLARE SECTION;

/* assign to

theBar and

theBeer

EXEC SQL SELECT priceINTO :thePriceFROM SellsWHERE beer = :theBeer

AND

bar = :theBar;

Winter 2002

Arthur Keller – CS 180 Cursors

Similar to PL/SQL cursors, with some syntactic differences.ExamplePrint Joe’s menu.

Sells(bar, beer, price)EXEC SQL BEGIN DECLARE SECTION;

char

theBeer[21]; float

thePrice;

EXEC SQL END DECLARE SECTION;EXEC SQL DECLARE c CURSOR FOR

SELECT beer, priceFROM SellsWHERE bar =

'Joe''s

Bar';

EXEC SQL OPEN CURSOR c;while(1) {

EXEC SQL FETCH c

INTO :theBeer, :thePrice; if(NOT FOUND) break; /* format and print beer and price */} EXEC SQL CLOSE CURSOR c;

Winter 2002

Arthur Keller – CS 180

Dynamic SQL

Motivation:• Embedded SQL is fine for fixed applications,e.g., a program that is used by a sales clerk tobook an airline seat.• It fails if you try to write a program like^ sqlplus

, because you have compiled the code

for

sqlplus

before you see the SQL statements

typed in response to the

SQL>

prompt.

• Two special statements of embedded SQL:

◆^ PREPARE

turns a character string into an SQL query.

◆^ EXECUTE

executes that query.

Winter 2002

Arthur Keller – CS 180

Example: Sqlplus Sketch EXEC^

SQL

BEGIN

DECLARE

SECTION;

char

query[MAX_QUERY_LENGTH];

EXEC

SQL

END

DECLARE

SECTION;

/*^

issue

SQL>

prompt

/*^

read

user's

text

into

array

query

EXEC

SQL

PREPARE

q^

FROM

:query;

EXEC

SQL

EXECUTE

q;

/*^

go^

back

to

reissue

prompt

-^ Once prepared, a query can be executed many times.

◆^ “Prepare” = optimize the query,

e.g ., find a way to execute it using

few disk-page I/O’s.

-^ Alternatively,

PREPARE

and

EXECUTE

can be combined

into:

EXEC

SQL

EXECUTE

IMMEDIATE

:query;

Winter 2002

Arthur Keller – CS 180

CLI

-^ In C, library calls let you create a

statement handle

= struct in

which you can place an SQL statement.^ ◆^

See text. See also Monjian book for PostgreSQL.

-^ Use

SQLPrepare(myHandle,

to make

myHandle

represent the SQL statement in the second argument.

-^ Use

SQLExecute(myHandle)

to execute that statement.

Example

SQLPrepare(handle1,~"SELECT~beer,~price

FROM

Sells WHERE

bar

=^

'Joe''s

Bar'");

SQLExecute(handle1);

Winter 2002

Arthur Keller – CS 180

Fetching Data

To obtain the data returned by an executed query, we:1.^

Bind variables to the component numbers of the returned query. ◆^ SQLBindCol

applies to a handle, column number, and variable, plus

other arguments (see text).

2.^

Fetch, using the handle of the query’s statement. ◆^ SQLFetch

applies to a handle.

Example

SQLBindCol(handle1,

SQL_CHAR,

&theBar,...)

SQLBindCol(handle1,

SQL_REAL,

&thePrice,...)

SQLExecute(handle1);

while(SQLFetch(handle1)

SQL_NO_DATA)

Winter 2002

Arthur Keller – CS 180

Executing Statements

-^ JDBC distinguishes queries (statements that return data) from^ updates

(statements that only affect the database).

-^ Methods

executeQuery

() and

executeUpdate

() are used to

execute these two kinds of SQL statements.^ ◆^

They must have an argument if applied to a

Statement

, never if applied

to a^

PreparedStatement

-^ When a query is executed, it returns an object of class^ ResultSet

Example

stat1.executeUpdate("INSERT

INTO

Sells"

"VALUES('Brass

Rail',

'Bud',

);ResultSet

Menu

=^

stat2.executeQuery();

Winter 2002

Arthur Keller – CS 180

Getting the Tuples of a

ResultSet

-^ Method

Next

() applies to a

ResultSet

and moves a

“cursor” to the next tuple in that set.^ ◆^

Apply

Next

() once to get to the first tuple.

◆^ Next

() returns

FALSE

if there are no more tuples.

-^ While a given tuple is the current of the cursor, you canget its

i th component by applying to a

ResultSet

a method

of the form

get

X

(i),

where

X^

is the name for the

type of that component.Example^ while(Menu.Next())

theBeer

=^

Menu.getString(1);

thePrice

=^

Menu.getFloat(2); ...

}