一、頭文件
引入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.2cto.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;
}
...
}