Docsity

CLI-Introduction to Database Management-Lecture 12 Slides-Computer Science, Slides of Introduction to Database Management Systems

Partial preview of the text

Download CLI-Introduction to Database Management-Lecture 12 Slides-Computer Science and more Slides Introduction to Database Management Systems in PDF only on Docsity!

CLI/ODBC

David Toman

School of Computer Science University of Waterloo

Introduction to Databases CS

Call Level Interface/ODBC

An interface built on a library calls:

• Applications are developed without access to the DB (and without

additional tools: no precompilation)

• incorporates ODBC (MS) and X/Open standards

• but it is harder to use and doesn’t allow preprocessing (e.g., no

checking of your SQL code and data types)

Three fundamental objects in an ODBC program:

• Environments

• Connections

• Statements

Errors

• SQLxxx functions return error codes

) similar to libc functions

) we should check them after every SQLxxx call

• the actual return codes:

• SQL_SUCCESS

• SQL_ERROR

• use the SQLError function to get sensible messages

SQL Statements

... and what we can do with them:

• SQLAllocStmt (allocates object)

• SQLExecDirect (execute)

• SQLPrepare (compile statement)

• SQLExecute (execute compiled statement)

• SQLSetParam (initialize a procedure parameter)

• SQLNumResultCols (number of result columns)

• SQLBindCol (ā€œhost variablesā€ in ODBC)

• SQLGetData (obtaining values of result columns)

• SQLFetch (cursor access in ODBC)

• SQLError (obtains diagnostics)

• SQLRowCount (number of affected rows)

• SQLFreeStmt (frees object)

Parameter Markers

The parameters are bound to buffers

SQLBindParameter(stmt-handle,

param-nr,

inp/out,

c-type,

db-type,

db-prec,

db-scale,

val-ptr,val-len,

val-NULL-ptr)

) it substitutes a value pointed to by val-ptr (with length val-len,

indicator variable val-NULL-ptr, and C data type c-type)

) for the param-nr-th parameter of stmt-handle

(using database type db-type).

Example

SQLCHAR stmt[] = "UPDATE author SET url =? WHERE aid = ?";

SQLINTEGER aid; SQLCHAR s[70]; SQLINTEGER ind;

rc = SQLAllocStmt(hdbc, &hstmt);

rc = SQLPrepare(hstmt, stmt, SQL_NTS);

printf"(Enter Author ID: "); scanf("%ld",&aid); printf"(Enter Author URL: "); scanf("%s", s);

rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, s, 70, &ind);

rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &aid, 0, NULL);

rc = SQLExecute(hstmt);

A Query with SQLBindCol

SQLCHAR sqlstmt[] = "SELECT pubid, title FROM publication";

SQLINTEGER rows; struct { SQLINTEGER ind; SQLCHAR s[70]; } pubid, title;

rc = SQLAllocStmt(hdbc, &hstmt);

rc = SQLExecDirect(hstmt, sqlstmt, SQL_NTS);

rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER)pubid.s, 8, &pubid.ind); rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, (SQLPOINTER)title.s, 70, &title.ind);

while ((rc = SQLFetch(hstmt)) == SQL_SUCCESS) printf("%-8.8s %-70.70s\n", pubid.s, title.s);

rc = SQLRowCount(hstmt, &rows); printf(" %d rows selected\n", rows);

rc = SQLFreeStmt(hstmt, SQL_DROP);

A Query with SQLGetData

SQLCHAR sqlstmt[] = "SELECT pubid, title FROM publication";

SQLINTEGER rows; struct { SQLINTEGER ind; SQLCHAR s[70]; } pubid, title;

rc = SQLAllocStmt(hdbc, &hstmt);

rc = SQLExecDirect(hstmt, sqlstmt, SQL_NTS);

while ((rc = SQLFetch(hstmt)) == SQL_SUCCESS) { rc = SQLGetData(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) pubid.s, 8, &(pubid.ind)); rc = SQLGetData(hstmt, 2, SQL_C_CHAR, (SQLPOINTER) title.s, 70, &(title.ind)); printf("%-8.8s %-70.70s \n", pubid.s, title.s); }

rc = SQLRowCount(hstmt, &rows); printf(" %d rows selected\n", rows);

rc = SQLFreeStmt(hstmt, SQL_DROP);

Transactions

• transaction start:

) implicitly using one of

SQLPrepare,

SQLExecute,

SQLExecDirect, etc.

functions.

• transaction end:

SQLTransact(henv, hdbc, what)

where

what = SQL_COMMIT, or

what = SQL_ROLLBACK

Summary

• CLI/ODBC can do everything Embedded SQL can.

• However, all statements are dynamic

) no precompilation

) explicit binding of parameters (user has to make types match!)

• An almost standard (ODBC, X/Open)

) independence on DBMS

) but: the standard has 100’s of functions