INFORMIX database is a product of IBM, which is a relational database and currently plays an important role in the financial industry. The only way to access the INFORMIX Database Using C is through
INFORMIX database is a product of IBM, which is a relational database and currently plays an important role in the financial industry. The only way to access the INFORMIX Database Using C is through
I. header files
When introducing the header file of the informix database, you can use the following method:
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;
Ii. macro definition
When a macro is used as a host variable, the macro definition can be as follows:
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 */
Iii. struct Definition
When a struct variable is used in an SQL statement, the struct type of this variable must be defined as follows:
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;
For example:
Int db_ifx_update (...)
{
Exec SQL BEGIN DECLARE SECTION;
DBStudent_t dbstudent;
Exec SQL END DECLARE SECTION;
...
Exec SQL UPDATE SET * = (: dbstudent) WHERE;
}
Iv. variable definition
The host variable must be defined as follows:
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;
For example:
Int db_ifx_open (...)
{
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;
...
}
V. error message
After each SQL statement is executed, the error code is stored in the global variable sqlca. sqlcode. You can use rgetmsg () to obtain the error information. For example:
Int db_ifx_commit (...)
{
Exec SQL COMMIT WORK;
If (sqlca. sqlcode <0)
{
Rgetmsg (sqlca. sqlcode, errmsg, sizeof (errmsg ));
Fprintf (stdout, "% s", errmsg );
Return-1;
}
...
}