Access INFORMIX database in C Language

Source: Internet
Author: User
Tags informix
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;
}
...
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.