SQL in Higher Level Languages

24
1 SQL in higher level languages

description

 

Transcript of SQL in Higher Level Languages

Page 1: SQL in Higher Level Languages

1

 

SQL in higher level languages

Page 2: SQL in Higher Level Languages

2

 SQL in higher level languages

Why do we want to use SQL in a higher level language?

1) Read in data from file, insert into relation

Loop until the EOF read values from file – higher level

code manipulate values with higher level

code code insert into relation values (SQL)

End loop

Page 3: SQL in Higher Level Languages

3

Why cont’d

2) Compute results based on result from query e.g. generate a report

Query databaseCompute results from queryPrint results

3) Provide a user interface for SQL if the current

one is lackingPrompt user for querySend query to DBMSReceive resultsDisplay results to user

Page 4: SQL in Higher Level Languages

4

Must have:

to do 1) must read in values into C variables then use those values to insert using SQL

still need SQL statement to insert, select tuples

to do 2) must be able to manipulate results from SQL query, but mismatch between C and SQL

sets versus one record at a time

to do 3) need to accept queries from user - create SQL queries

Page 5: SQL in Higher Level Languages

5

Must have:

Select dnumber From department Where mgrssn = 987654321

What is needed?– Variables in which to place result (Host variables)– Processing of result table (cursors)– Data structure for communicating with DBS in case of

errors (SQLCA)– What if we want to process any query typed in?

(Dynamic SQL)

Page 6: SQL in Higher Level Languages

6

With what?

• Can use: 1. embedded SQL

Precede each statement with EXEC SQL

2. Platform specific classes, interfaces

Oracle’s OLE

3. Platform independent classes, interfaces

JDBC

Page 7: SQL in Higher Level Languages

7

Host variables :

• Referenced by SQL and higher level language• transmit data between DB manager and application• Used in static SQL

select ssn from employee where salary < :min

insert into project values (:projectName, :projectNumber, :projectLocation, :departmentNumber)

Page 8: SQL in Higher Level Languages

8

Parameter markers ?

Can pass query as character string (dynamic SQL) “update employee

set salary = salary*1.1 where dno = ? and sex = ?”

• We need to identify the variable that we will obtain later

• Depending on system, use ? or : as a parameter marker

• An SQL statement can contain more than one parameter marker

Page 9: SQL in Higher Level Languages

9

Cursors 

• How to access multiple rows from a query result?

•   Use a cursor – A cursor points to 1 row– Can move forward, backwards, etc.

Page 10: SQL in Higher Level Languages

10

Using cursors

• 3 steps involved:–   1) declare cursor - just a definition of the

select– 2) open cursor - executes select, builds

result table• Declare/open can be as one

– 3) fetch results - to navigate through the results

Page 11: SQL in Higher Level Languages

11

SQLCA

• SQL communication area - a structure• used for communication between DBS

monitor and C++ program• allocates program space for errors and

starts communication by DBS monitor• after each SQL statement executed, a new

value is placed in SQLCA• indicates if successful, EOF, etc.• error or warning conditions

Page 12: SQL in Higher Level Languages

12

SQLCA

• sqlca.sqlcode - testing code part of structure– sqlcode = 0 successful sql call– < 0 error– > 0 warning - call successful but some

condition existed

e.g. EOF is 100 (DB2, Ingres, but not ORACLE)

• sqlerrd[2] - indicates number of row affected by insert, update or delete (used for referential integrity)

Page 13: SQL in Higher Level Languages

13

Error messages

• printing error messages - can extract the error message

• Available through SQLCA

 

Page 14: SQL in Higher Level Languages

14

Updating

• Delete– Delete tuples from base table– Positioned delete, use cursor

• Updates– Makes changes to base table– Positioned delete, use cursor

• Insert– No need for cursor, can't specify position of new row

• DDL– Can create tables, etc.

Page 15: SQL in Higher Level Languages

15

Execute Immediate

• Prepares and executes an SQL statement that does not use any host variables

• Can NOT be used for any select statements

• Can use it for update, drop, create index, create table, create view, etc.

Page 16: SQL in Higher Level Languages

16

Execute Immediate

• Execute immediate statement is precompiled• When it is executed at run time:

1.executable SQL stmt constructed from SQL char string text

2.executable form of SQL statement is processed

3.executable form of SQL statement is destroyed

• Recompiles a new SQL statement each time execute this statement

Page 17: SQL in Higher Level Languages

17

Prepare and Execute

• executes previously prepared statement that has parameter markers (host variables)

• can execute a statement more than once• this statement only makes sense for updates,

delete, etc. because are not using the 'into'• a select statement here makes no sense, cannot

print results• Prepare and execute gives better performance

than execute immediate if repeatedly execute the same statement

Page 18: SQL in Higher Level Languages

18

Embedded Static vs. dynamic SQL

• Static SQL – Embed SQL statements directly into host level

languageEXEC SQL select lname, salary

into :lname, :sal

From employee

Where ssn=123456789;

Page 19: SQL in Higher Level Languages

19

Embedded Static vs. dynamic SQL

• Dynamic SQL– Place SQL query into character string

char st[80] = " "; // in Declare Section strcpy (st, "Delete From employee where"); cin >> field; strappend (st, field); strappend (st, "> :val");

//Resulting query is:// “Delete From employee where salary > :val” EXEC SQL Execute immediate st;

Sample program

Page 20: SQL in Higher Level Languages

20

Embedded SQL work

• Since it is a C/C++ program, must be able to compile

• Hence, must precompile to identify SQL statements

• SQL statements are replaced with calls to SQLLIB routines (API calls to data manager that are recognized by C/C++)

Page 21: SQL in Higher Level Languages

21

Precompiler for Oracle

• start with a source file: fn.pc• Precompiler generates file: fn.c/fn.cpp

– (internal representation of SQL statements that were replaced by calls to SQLLIB routines- orasql9.lib)

– if examine fn.c/fn.cpp can see all the SQLLib calls

• Then you compile your program using C/C++

Page 22: SQL in Higher Level Languages

22

What is needed in .NET to use Oracle? • Must set up the environment

– Add path for oracle executable files• C:\Program Files\Oracle\Ora90\bin

– Add path for Oracle include files• C:\Program Files\Oracle\Ora90\precomp\public

– Add path for Oracle library files• C:\Program Files\Oracle\Ora90\precomp\lib\msvc

– Add orasql9.lib for Linker

Page 23: SQL in Higher Level Languages

23

Connect

• must connect to DBMS

• Include the following in C/C++ program EXEC SQL connect :user_name identified by :user_pwd

using :host_string;

EXEC SQL disconnect;

How to set up .NET in our lab to use Pro C

Page 24: SQL in Higher Level Languages

24

OLE, JDBC

• OLE methods are available in Oracle to do most of the above

• Can also use JDBC, independent of platform