<ul><li>SQL in higher level languages </li></ul>
 SQL in higher level languages <ul><li>Why do we want to use SQL in a higher level language? </li></ul><ul><li>Read in dat...
Why cont’d <ul><li>Compute results based on result from query e.g.  generate a report </li></ul><ul><ul><li>Query database...
Must have: <ul><ul><li>to do 1)  must read in values into C variables then use those values to insert using SQL  </li></ul...
Must have: <ul><ul><ul><li>  Select dnumber </li></ul></ul></ul><ul><ul><ul><li>From department </li></ul></ul></ul><ul><u...
With what? <ul><li>Can use:  </li></ul><ul><ul><li>embedded SQL </li></ul></ul><ul><li>  Precede each statement with EXEC ...
Host variables  : <ul><li>Referenced by SQL and higher level language </li></ul><ul><li>transmit data between DB manager a...
Parameter markers  ? <ul><li>Can pass query as character string (dynamic SQL) </li></ul><ul><li>  “ update  employee </li>...
Cursors  <ul><li>How to access multiple rows from a query result? </li></ul><ul><li>  Use a cursor  </li></ul><ul><ul><li>...
Using cursors <ul><li>3 steps involved: </li></ul><ul><ul><li>  1)  declare cursor - just a definition of the select </li>...
SQLCA <ul><li>SQL communication area - a structure </li></ul><ul><li>used for communication between DBS monitor and C++ pr...
SQLCA <ul><li>sqlca.sqlcode - testing code part of structure </li></ul><ul><ul><li>sqlcode = 0  successful sql call </li><...
Error messages <ul><li>printing error messages - can extract the error message </li></ul><ul><li>Available through SQLCA <...
Updating <ul><li>Delete </li></ul><ul><ul><li>Delete tuples from base table </li></ul></ul><ul><ul><li>Positioned delete, ...
Execute Immediate <ul><li>Prepares and executes an SQL statement that does not use any host variables </li></ul><ul><li>Ca...
Execute Immediate <ul><li>Execute immediate statement is precompiled </li></ul><ul><li>When it is executed at run time: </...
Prepare and Execute <ul><li>executes previously prepared statement that has parameter markers (host variables) </li></ul><...
Embedded Static vs. dynamic SQL <ul><li>Static SQL  </li></ul><ul><ul><li>Embed SQL statements directly into host level la...
Embedded Static vs. dynamic SQL <ul><li>Dynamic SQL </li></ul><ul><ul><li>Place SQL query into character string </li></ul>...
Embedded SQL work <ul><li>Since it is a C/C++ program, must be able to compile </li></ul><ul><li>Hence, must precompile to...
Precompiler for Oracle <ul><li>start with a source file:  fn.pc </li></ul><ul><li>Precompiler generates file:  fn.c/fn.cpp...
What is needed in .NET to use Oracle?  <ul><li>Must set up the environment </li></ul><ul><ul><li>Add path for oracle execu...
Connect <ul><li>must connect to DBMS </li></ul><ul><li>Include the following in C/C++ program </li></ul><ul><li>EXEC SQL c...
OLE, JDBC <ul><li>OLE methods are available in Oracle to do most of the above </li></ul><ul><li>Can also use JDBC, indepen...
Upcoming SlideShare
Loading in …5
×

SQL in Higher Level Languages

247
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
247
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SQL in Higher Level Languages

  1. 1.   <ul><li>SQL in higher level languages </li></ul>
  2. 2.  SQL in higher level languages <ul><li>Why do we want to use SQL in a higher level language? </li></ul><ul><li>Read in data from file, insert into relation </li></ul><ul><li>Loop until the EOF </li></ul><ul><li> read values from file – higher level code </li></ul><ul><li> manipulate values with higher level code code </li></ul><ul><li>insert into relation values (SQL) </li></ul><ul><li>End loop </li></ul>
  3. 3. Why cont’d <ul><li>Compute results based on result from query e.g. generate a report </li></ul><ul><ul><li>Query database </li></ul></ul><ul><ul><li>Compute results from query </li></ul></ul><ul><ul><li>Print results </li></ul></ul><ul><li>Provide a user interface for SQL if the current one is lacking </li></ul><ul><ul><li>Prompt user for query </li></ul></ul><ul><ul><li>Send query to DBMS </li></ul></ul><ul><ul><li>Receive results </li></ul></ul><ul><ul><li>Display results to user </li></ul></ul>
  4. 4. Must have: <ul><ul><li>to do 1) must read in values into C variables then use those values to insert using SQL </li></ul></ul><ul><ul><li>still need SQL statement to insert, select tuples </li></ul></ul><ul><ul><li>to do 2) must be able to manipulate results from SQL query, but mismatch between C and SQL </li></ul></ul><ul><ul><li> sets versus one record at a time </li></ul></ul><ul><ul><li>to do 3) need to accept queries from user - create SQL queries </li></ul></ul>
  5. 5. Must have: <ul><ul><ul><li> Select dnumber </li></ul></ul></ul><ul><ul><ul><li>From department </li></ul></ul></ul><ul><ul><ul><li>Where mgrssn = 987654321 </li></ul></ul></ul><ul><li>What is needed? </li></ul><ul><ul><li>Variables in which to place result (Host variables) </li></ul></ul><ul><ul><li>Processing of result table (cursors) </li></ul></ul><ul><ul><li>Data structure for communicating with DBS in case of errors (SQLCA) </li></ul></ul><ul><ul><li>What if we want to process any query typed in? (Dynamic SQL) </li></ul></ul>
  6. 6. With what? <ul><li>Can use: </li></ul><ul><ul><li>embedded SQL </li></ul></ul><ul><li> Precede each statement with EXEC SQL </li></ul><ul><li>2. Platform specific classes, interfaces </li></ul><ul><li> Oracle’s OLE </li></ul><ul><li>3. Platform independent classes, interfaces </li></ul><ul><li> JDBC </li></ul>
  7. 7. Host variables : <ul><li>Referenced by SQL and higher level language </li></ul><ul><li>transmit data between DB manager and application </li></ul><ul><li>Used in static SQL </li></ul><ul><ul><li> select ssn </li></ul></ul><ul><ul><li> from employee </li></ul></ul><ul><ul><li> where salary < :min </li></ul></ul><ul><ul><li> insert into project values (:projectName, :projectNumber, :projectLocation, :departmentNumber) </li></ul></ul>
  8. 8. Parameter markers ? <ul><li>Can pass query as character string (dynamic SQL) </li></ul><ul><li> “ update employee </li></ul><ul><li>set salary = salary*1.1 </li></ul><ul><li>where dno = ? and sex = ?” </li></ul><ul><li>We need to identify the variable that we will obtain later </li></ul><ul><li>Depending on system, use ? or : as a parameter marker </li></ul><ul><li>An SQL statement can contain more than one parameter marker </li></ul>
  9. 9. Cursors  <ul><li>How to access multiple rows from a query result? </li></ul><ul><li>  Use a cursor  </li></ul><ul><ul><li>A cursor points to 1 row </li></ul></ul><ul><ul><li>Can move forward, backwards, etc. </li></ul></ul>
  10. 10. Using cursors <ul><li>3 steps involved: </li></ul><ul><ul><li>  1) declare cursor - just a definition of the select </li></ul></ul><ul><ul><li>2) open cursor - executes select, builds result table </li></ul></ul><ul><ul><ul><li>Declare/open can be as one </li></ul></ul></ul><ul><ul><li>3) fetch results - to navigate through the results </li></ul></ul>
  11. 11. SQLCA <ul><li>SQL communication area - a structure </li></ul><ul><li>used for communication between DBS monitor and C++ program </li></ul><ul><li>allocates program space for errors and starts communication by DBS monitor </li></ul><ul><li>after each SQL statement executed, a new value is placed in SQLCA </li></ul><ul><li>indicates if successful, EOF, etc. </li></ul><ul><li>error or warning conditions </li></ul>
  12. 12. SQLCA <ul><li>sqlca.sqlcode - testing code part of structure </li></ul><ul><ul><li>sqlcode = 0 successful sql call </li></ul></ul><ul><ul><li>< 0 error </li></ul></ul><ul><ul><li>> 0 warning - call successful but some condition existed </li></ul></ul><ul><li>e.g. EOF is 100 (DB2, Ingres, but not ORACLE) </li></ul><ul><li>sqlerrd[2] - indicates number of row affected by insert, update or delete (used for referential integrity) </li></ul>
  13. 13. Error messages <ul><li>printing error messages - can extract the error message </li></ul><ul><li>Available through SQLCA </li></ul><ul><li>  </li></ul>
  14. 14. Updating <ul><li>Delete </li></ul><ul><ul><li>Delete tuples from base table </li></ul></ul><ul><ul><li>Positioned delete, use cursor </li></ul></ul><ul><li>Updates </li></ul><ul><ul><li>Makes changes to base table </li></ul></ul><ul><ul><li>Positioned delete, use cursor </li></ul></ul><ul><li>Insert </li></ul><ul><ul><li>No need for cursor, can't specify position of new row </li></ul></ul><ul><li>DDL </li></ul><ul><ul><li>Can create tables, etc. </li></ul></ul>
  15. 15. Execute Immediate <ul><li>Prepares and executes an SQL statement that does not use any host variables </li></ul><ul><li>Can NOT be used for any select statements </li></ul><ul><li>Can use it for update, drop, create index, create table, create view, etc. </li></ul>
  16. 16. Execute Immediate <ul><li>Execute immediate statement is precompiled </li></ul><ul><li>When it is executed at run time: </li></ul><ul><ul><li>executable SQL stmt constructed from SQL char string text </li></ul></ul><ul><ul><li>executable form of SQL statement is processed </li></ul></ul><ul><ul><li>executable form of SQL statement is destroyed </li></ul></ul><ul><li>Recompiles a new SQL statement each time execute this statement </li></ul>
  17. 17. Prepare and Execute <ul><li>executes previously prepared statement that has parameter markers (host variables) </li></ul><ul><li>can execute a statement more than once </li></ul><ul><li>this statement only makes sense for updates, delete, etc. because are not using the 'into' </li></ul><ul><li>a select statement here makes no sense, cannot print results </li></ul><ul><li>Prepare and execute gives better performance than execute immediate if repeatedly execute the same statement </li></ul>
  18. 18. Embedded Static vs. dynamic SQL <ul><li>Static SQL </li></ul><ul><ul><li>Embed SQL statements directly into host level language </li></ul></ul><ul><ul><li>EXEC SQL select lname, salary </li></ul></ul><ul><ul><li>into :lname, :sal </li></ul></ul><ul><ul><li>From employee </li></ul></ul><ul><ul><li>Where ssn=123456789; </li></ul></ul>
  19. 19. Embedded Static vs. dynamic SQL <ul><li>Dynamic SQL </li></ul><ul><ul><li>Place SQL query into character string </li></ul></ul><ul><li>char st[80] = &quot; &quot;; // in Declare Section </li></ul><ul><li>strcpy (st, &quot;Delete From employee where&quot;); </li></ul><ul><li>cin >> field; </li></ul><ul><li>strappend (st, field); </li></ul><ul><li>strappend (st, &quot;> :val&quot;); </li></ul><ul><li>//Resulting query is: </li></ul><ul><li>// “Delete From employee where salary > :val” </li></ul><ul><li> EXEC SQL Execute immediate st; </li></ul><ul><li>Sample program </li></ul>
  20. 20. Embedded SQL work <ul><li>Since it is a C/C++ program, must be able to compile </li></ul><ul><li>Hence, must precompile to identify SQL statements </li></ul><ul><li>SQL statements are replaced with calls to SQLLIB routines (API calls to data manager that are recognized by C/C++) </li></ul>
  21. 21. Precompiler for Oracle <ul><li>start with a source file: fn.pc </li></ul><ul><li>Precompiler generates file: fn.c/fn.cpp </li></ul><ul><ul><li>(internal representation of SQL statements that were replaced by calls to SQLLIB routines- orasql9.lib) </li></ul></ul><ul><ul><li>if examine fn.c/fn.cpp can see all the SQLLib calls </li></ul></ul><ul><li>Then you compile your program using C/C++ </li></ul>
  22. 22. What is needed in .NET to use Oracle? <ul><li>Must set up the environment </li></ul><ul><ul><li>Add path for oracle executable files </li></ul></ul><ul><ul><ul><li>C:Program FilesOracleOra90in </li></ul></ul></ul><ul><ul><li>Add path for Oracle include files </li></ul></ul><ul><ul><ul><li>C:Program FilesOracleOra90precomppublic </li></ul></ul></ul><ul><ul><li>Add path for Oracle library files </li></ul></ul><ul><ul><ul><li>C:Program FilesOracleOra90precomplibmsvc </li></ul></ul></ul><ul><ul><li>Add orasql9.lib for Linker </li></ul></ul>
  23. 23. Connect <ul><li>must connect to DBMS </li></ul><ul><li>Include the following in C/C++ program </li></ul><ul><li>EXEC SQL connect :user_name identified by :user_pwd using :host_string; </li></ul><ul><li>EXEC SQL disconnect; </li></ul><ul><li>How to set up .NET in our lab to use Pro C </li></ul>
  24. 24. OLE, JDBC <ul><li>OLE methods are available in Oracle to do most of the above </li></ul><ul><li>Can also use JDBC, independent of platform </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×