#define ERRLEN 256 /* maximum length of error message */ int errlength = ERRLEN; /* size of buffer */ int errsize; /* to contain actual message length */ char errbuf[ERRLEN]; /* buffer to receive message */
sqlglm(errbuf, &errlength, &errsize); /* get the error message for Oracle DB */ printf("%.*s\n", errsize, errbuf);
1.4.3. 连接到数据库
SQL 99
1 2 3
EXEC SQL CONNECT TO target-server [AS connect-name] [USER username]; EXEC SQL CONNECT TO DEFAULT;
target-server:数据库名称
connect-name:连接的Session名称
username:用户名
Oracle
1 2
EXEC SQL CONNECT TO :user_name IDENTIFIED BY :user_pwd ;
准备、执行和使用:PREPARE handle FROM :stmt_string;,EXECUTE handle USING :host_var;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<stdio.h> exec sql include sqlca; exec sql begin declare section; char cust_id[5], sqltext[256]; exec sql end declare section; intmain() { strcpy(sqltext, "delete from customers where cid = ?"); exec sql whenever sqlerror goto report_error; exec sql connect to testdb; exec sql prepare delcust from :sqltext; while (1) { /* input customer’s id to cust_id */ exec sql execute delcust using :cust_id; exec sql commit work; } ...... }
2.3. 动态选择
动态选择:The Describe Statement and the SQLDA
2.4. 前沿编程概念
2.4.1. Scrollable Cursors 可滚动游标
1 2 3 4 5 6 7 8 9 10 11 12 13
//指针循环 EXEC SQL DECLARE cursor_name [ INSENSITIVE ] [ SCROLL ] CURSOR [ WITH HOLD ] FOR subquery { UNION subquery } [ ORDER BY ...... ] [ FOR READ ONLY | FOR UPDATE OF columnname ...... ]; //执行指针 EXEC SQL FETCH [ { NEXT | PRIOR | FIRST | LAST | { ABSOLUTE | RELATIVE } value_spec } FROM ] cursor_name INTO ......;
评论