๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š ์ „๊ณต ๊ณต๋ถ€/DB๊ธฐ์ดˆ

[DB] SQL ProgrammingโŒจ๏ธ (1)

728x90

Database Programming

  • ๋ชฉํ‘œ : ๋Œ€ํ™”ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์•„๋‹Œ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์—์„œ DB์— ์ ‘๊ทผ
  • ์™œ? : ํŽธ๋ฆฌํ•˜์ง€๋งŒ ๋ถˆ์ถฉ๋ถ„ํ•˜๊ณ , ๋Œ€๋ถ€๋ถ„์ด ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋งŒ๋“ค์–ด์ง

DB programming Approaches (์ ‘๊ทผ๋ฐฉ๋ฒ•)

  • ์ž„๋ฒ ๋””๋“œ
  • ํ•จ์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ (API)
  • ์ƒˆ ์–ธ์–ด : DBPL (Database Programming Language) → ๋ถˆ์ผ์น˜๋ฅผ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ์Œ

Impedance Mismatch : ์ž„ํ”ผ๋˜์Šค ๋ถˆ์ผ์น˜

  • ํ˜ธ์ŠคํŠธ ์–ธ์–ด์™€ DB๋ชจ๋ธ ์‚ฌ์ด์˜ ๋น„ํ˜ธํ™˜์„ฑ

Typical sequence of interaction in DB Programming

  1. ํด๋ผ์ด์–ธํŠธ ํ”„๋กœ๊ทธ๋žจ์ด DB์„œ๋ฒ„์™€์˜ ์—ฐ๊ฒฐ์„ open
  2. ํด๋ผ์ด์–ธํŠธ ํ”„๋กœ๊ทธ๋žจ์ด ์งˆ์˜์–ด๋ฅผ ๋ณด๋ƒ„
  3. DB์ ‘๊ทผ์ด ํ•„์š”์—†์–ด์ง€๋ฉด ์ ‘์† ์ข…๋ฃŒ

Embedded SQL

  • COBOL, C, JAVA
  • ํ˜ธ์ŠคํŠธ ์–ธ์–ด์™€ ์ฐจ์ด๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด EXEC SQL … END_EXEC ๋ฅผ ์‚ฌ์šฉํ•จ
  • ๊ณต์œ ๋ณ€์ˆ˜ : SQL๋ฌธ์—์„œ ๋ณ€์ˆ˜ ์•ž์— : (์ฝœ๋ก )์„ ๋ถ™์—ฌ์•ผํ•จ → ex) into :fname

SQLCODE : ์—๋Ÿฌ ๋ณ€์ˆ˜, SQLSTATE : ์˜ˆ์™ธ ๋ณ€์ˆ˜ (ํ†ต์‹ ์— ์‚ฌ์šฉ๋˜๋Š” ์ง€์ •๋œ ๋ณ€์ˆ˜์ž„)

Connecting to the DB

  • connection : CONNECT TO
  • change : SET CONNECTION -
  • disconnection : DISCONNECT -

communicating between program and dbms

  • SQLCODE = 0 : ์„ฑ๊ณต์ 
  • SQLCODE > 0 or =100 : ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์—†์Œ
  • SQLCODE < 0 : ์—๋Ÿฌ
  • ORACLE : SQLCA ์‚ฌ์šฉ : record structure
    • EXEC SQL include SQLCA;
  • SQLSTATE = ‘00000’ : no error or exception

๊ฒ€์ƒ‰๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ํŠœํ”Œ์ผ๋•Œ Multiple Tuples with Embedded SQL Using Cursors

  • cursor (iterator)
  • EXEC SQL DECLARE EMP CURSOR FOR : EMP๋ผ๋Š” ์ปค์„œ ์„ ์–ธ
  • CURSOR OPEN : ์ปค์„œ ์‹คํ–‰ (EXEC SQL OPEN EMP;)
  • FETCH : ๋‹ค์Œ ํŠœํ”Œ๋กœ ์ด๋™์‹œํ‚ด (EXEC SQL FETCH from EMP into ~)
  • CLOSE EMP : EMP์ปค์„œ๋ฅผ ์ œ๊ฑฐ (EXEC SQL CLOSE EMP;)

Dynamic SQL : ๋Ÿฐํƒ€์ž„ ์ค‘์— SQL, ๋ณต์žกํ•  ์ˆ˜ ์žˆ์Œ

EXEC SQL PREPARE - FROM - ; → ์ปดํŒŒ์ผ ๋ช…๋ น

EXEC SQL EXECUTE - ; → ์‹คํ–‰ ๋ช…๋ น

์ปดํŒŒ์ผ+์‹คํ–‰ ํ•œ๋ฒˆ์— ๋ช…๋ นํ•˜๊ธฐ : EXEC SQL EXECUTE IMMEDIATE - ;

SQLJ : sql in java, import java.sql ํ•„์š”

Embedded SQL in java

  • try { #sql { }; }
  • catch (SQLException e) { } : ์˜ˆ์™ธ/์—๋Ÿฌ ๊ฒ€์ถœ

Multiple Tuples in SQLJ

  • 2 types of iterators
    • named iterator : ๋ณ€์ˆ˜ ์ด๋ฆ„์œผ๋กœ
    • positional iterator : ํƒ€์ž…๋งŒ ๋‚˜์—ดํ•˜๊ณ  ์ˆœ์„œ๋Œ€๋กœ → next() ๋Œ€์‹  !endFetch() ์‚ฌ์šฉ : ์›์†Œ๊ฐ€ ๋‚จ์•„์žˆ์„๋•Œ false, ์ข…๋ฃŒ์‹œ true์ด๋ฏ€๋กœ
  • next() ์‚ฌ์šฉ
  • ์ปค์„œ ์„ ์–ธ ํ›„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  (Emp e = null), ์ปค์„œ์— sql๊ฒฐ๊ณผ๋ฅผ ๋Œ€์ž…ํ•˜์—ฌ next()๋กœ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๊ธฐ

DB programming with functional calls : SQL/CLI and JDBC

  • embedded sql prpovides static DB programming
  • API : dynamic DB programming with a library of functions
    • ์žฅ์  : no preprocessor need(๋” ์œ ์—ฐํ•จ)
    • ๋‹จ์  : SQL syntax checks to be done at runtime

“select - from - where Ssn = ?”, SQL_NTS

SQL Call level interface

  • a part of the SQL standard
    • ODBC (Open database connectivitiy)
  • easy access to several db
  • certain libraries
  • SQL statements are dynamically created and passed as string parameters in the calls
    • the information is kept in 4 types of records (struct in C)

Components of SQL/CLI

  1. Environment record : ์—ฐ๊ฒฐ+ํ™˜๊ฒฝ์ •๋ณด ๊ด€๋ฆฌ
  2. Connection record : ํŠน์ • ์—ฐ๊ฒฐ์„ ์œ„ํ•œ ์ •๋ณด ๊ด€๋ฆฌ
  3. Statement record : SQL ๋ฌธ์žฅ ์ •๋ณด ๊ด€๋ฆฌ
  4. Description record : ํŠœํ”Œ, ๋ณ€์ˆ˜ ์ •๋ณด ๊ด€๋ฆฌ

Steps in C

  1. load SQL/CLI libraries
  2. handle ๋ณ€์ˆ˜ ์„ ์–ธ(c pointers) : SQLHSTMT(statement), SQLHDBC(db connection), SQLHENV(environment), SQLHDESC(description)
  3. set up records using SQLAllocHandle( handle_type , handle_1(์ปจํ…Œ์ด๋„ˆ) , handle_2(์ƒˆ type์˜ ํฌ์ธํ„ฐ) )
  4. statement ์ค€๋น„์‹œํ‚ค๊ธฐ : SQLPrepare
  5. ํ”„๋กœ๊ทธ๋žจ ๋ณ€์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ
  6. SQLExecute๋กœ ์‹คํ–‰
  7. ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ C๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉ : SQLBindCol
  8. SQLFetch ๋กœ ๊ฐ’์„ C๋ณ€์ˆ˜๋กœ ๊ฒ€์ƒ‰

// program CLI

๋ฉ€ํ‹ฐํŠœํ”Œ ๋ฐ˜ํ™˜ ์‹œ while(!ret2) { - } ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ

728x90