EXEC SQL CONNECT ...;
호스트 프로그램과 DBS 간 데이터 할당을 위한 변수 선언
프로그램에서 사용시 반드시 접두사에 콜론(:) 사용
EXEC SQL BEGIN DECLARE SECTION
...
EXEC SQL END DECLARE SECTION;
EXEC SQL <statement>;
두 언어 간에 자료처리 방식 차이로 인한 불일치
일반 프로그램 언어는 집합(or 멀티셋) 타입 지원 X
→ 해결 : DBS가 cursor 기능 제공
declare
: cursor 선언 → open
시 DBS에 해당 질의 결과를 임시 테이블에 저장 실제 질의문이 수행되는 시점 → fetch
: 임시 테이블에서 tuple 하나씩 추출 후 호스트 프로그램에 tuple 단위로 값 전달 → close
EXEC SQL declare myCursor cursor for
select sID, name from student where total > :credit;
EXEC SQL open myCursor;
EXEC SQL fetch myCursor into :si, :sn;
EXEC SQL close myCursor;
int main() {
EXEC SQL BEGIN DECLARE SECTION;
char fname[15];
char lname[15];
EXEC SQL END DECLARE SECTION;
EXEC SQL connect to 'myServer';
EXEC SQL DECLARE demoCursor cursor for
select firstName, lastName
from customer
where :lname < 'C';
EXEC SQL open demoCursor;
for(;;) {
EXEC SQL fetch demoCursor into :fname, :lname;
if (strncmp(SQLSTATE, "00000", 5) != 0) break;
printf("%s %s\n" , fname, lname);
}
EXEC SQL close demoCursor;
EXEC SQL disconnect current;
}
Declare myCursorUpdate cursor for
select*
from professor
for update; //갱신 커서임을 명시
Update professor
set salary = salary + 100
where current of myCursorUpdate;
SQL 문장에 대한 compile 단계
? : 실제 값으로 대치되는 placeholder(변수)
→ execute 단계
stcopy("select name from professor where salary > ?", myText);
//placeholder 가능
//사용자로부터 SQL 문장 직접 받기
EXEC SQL prepare my1 from :myTEXT;
mySalary = 1000;
EXEC SQL execute my1 using :mySalary;
‘?’ 기호 : my1 prepare 단계에서 실제 값으로 대치되는 placeholder
stcopy("select name from professor where pID='234', myText2);
EXEC SQL execute immediate from myText2;
→ 간단하게 훑고 skip한 파트
Open Database Connectivity)
C, C++, C#, Visual Basic
동적 SQL 접근 방식 제공
SQLExecDirect() //SQL 문장 수행
SQLBindCol() //결과 속성값을 로컬 변수와 연결
SQLFetch() //터플 단위로 데이터 받아옴
전처리 과정 필요
전처리 과정에서 구문검사, 권한 검사, 실행 계획 수립 등 가능
DB 연산 신속하게 수행 가능
단점
실행 코드 작성 시 두 단계를 거쳐야 하는 번거로움
flexible, powerful, harder to use
단점
slower
→ 요즘엔 SQL API 방식이 널리 사용되는 경향이 있음.
Web server
application server
database server
→ 2층 구조에 비해 프로세스 구동 및 연동간 오버헤드 다소 존재
<html>
<body>
<table border="1">
<tr> <th>ID</th> <th>Name</th> <th>Department</th> </tr>
<tr> <td>123</td> <td>Lee</td> <td>CS</td> </tr>
...
</table>
<form action="myQuery" method="get">
Search for:
<select name="personType">
<option value="student" selected>Student</option>
<option value="professor">Professor</option> </select> <br>
Name: <input type=text size=20 name="name">
<input type=submit value="Find">
</form>
</body>
</html>