C语言访问INFORMIX数据库 — ESQL语法知识
一、头文件
引入informix数据库的头文件时,可使用如下的方式:
[cpp]
EXEC SQL INCLUDE sqlca;
EXEC SQL INCLUDE sqlda;
EXEC SQL INCLUDE sqlhdr;
EXEC SQL INCLUDE decimal;
EXEC SQL INCLUDE locator;
EXEC SQL INCLUDE varchar;
EXEC SQL INCLUDE datetime;
EXEC SQL INCLUDE sqlstype;
EXEC SQL INCLUDE sqltypes;
EXEC SQL INCLUDE sqlstype;
二、宏定义
宏在宿主变量使用时,此宏定义可使用如下方式:
[cpp]
EXEC SQL DEFINE SQL_USRNAME_MAX_LEN 64; /* User name max len */
EXEC SQL DEFINE SQL_PASSWD_MAX_LEN 64; /* Password max len */
EXEC SQL DEFINE SQL_SVRNAME_MAX_LEN 64; /* Database name max len */
EXEC SQL DEFINE SQL_CNNAME_MAX_LEN 64; /* Connect name max len */
EXEC SQL DEFINE SQL_STMT_MAX_LEN 1024; /* SQL statement max len */
三、结构体定义
当SQL语句中使用了结构体变量时,那么此变量的类型结构体必须通过如下方式定义:
[cpp]
EXEC SQL BEGIN DECLARE SECTION;
typedef struct
{
int id;
char name[32];
char gender;
int age;
char brf[256];
}DBStudent_t;
EXEC SQL END DECLARE SECTION;
如:
[cpp]
int db_ifx_update(...)
{
EXEC SQL BEGIN DECLARE SECTION;
DBStudent_t dbstudent;
EXEC SQL END DECLARE SECTION;
...
EXEC SQL UPDATE SET *=(:dbstudent) WHERE id='10001';
}
四、变量定义
宿主变量的必须通过如下方式进行定义:
[cpp]
EXEC SQL BEGIN DECLARE SECTION;
char svrname[SQL_SVRNAME_MAX_LEN], /* Server name */
usrname[SQL_USRNAME_MAX_LEN], /* User name */
passwd[SQL_PASSWD_MAX_LEN], /* Passwd */
cnname[SQL_CNNAME_MAX_LEN]; /* Connect name */
EXEC SQL END DECLARE SECTION;
如:
[cpp]
int db_ifx_open(...)
{ www.zzzyk.com
EXEC SQL BEGIN DECLARE SECTION;
char svrname[SQL_SVRNAME_MAX_LEN], /* Server name */
usrname[SQL_USRNAME_MAX_LEN], /* User name */
passwd[SQL_PASSWD_MAX_LEN], /* Passwd */
cnname[SQL_CNNAME_MAX_LEN]; /* Connect name */
EXEC SQL END DECLARE SECTION;
...
EXEC SQL CONNECT TO :svrname AS :cnname USER :usrname USING :passwd;
...
}
五、错误信息
每执行一次SQL语句后,错误码存放在全局变量sqlca.sqlcode中,可通过rgetmsg()获取错误信息。如:
[cpp]
int db_ifx_commit(...)
{
EXEC SQL COMMIT WORK;
if(sqlca.sqlcode < 0)
{
rgetmsg(sqlca.sqlcode, errmsg, sizeof(errmsg));
fprintf(stdout, "%s", errmsg);
return -1;
}
...
}
补充:软件开发 , C语言 ,