C language embedded in Informix Basic introductory example to explain _c language

Source: Internet
Author: User
Tags informix stdin strlen

Copy Code code as follows:

#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <sqlca.h>

$struct _db_person
{
Char name[30+1];
Char card[12+1];
int age;
};

Char *trim (char *str)
{
Char *p, *buffer;
int Len;

if (NULL!=STR)
{
len = strlen (str);
if (len > 0)
{
Buffer= (char *) malloc (sizeof (char) * (len+1));
if (NULL!= buffer)
{
Memmove (buffer, str, len);
buffer[len]=0;

p = buffer + len-1;
while ((P!= buffer) && ((*p > 0x00) && (*p <= '))
* (p--) = 0;
p = buffer;
while ((*p > 0x00) && (*p <= '))
p++;
strcpy (str, p);
Free (buffer);
}
}
}
return str;

}

int GetData (struct _db_person *pps)
{
Char strage[20];

memset (PPS, 0, sizeof (struct _db_person));
printf ("Enter name<press enter only to Exit>:");
Fgets (Pps->name,sizeof (pps->name), stdin);
Trim (pps->name);
if (strlen (pps->name) = = 0)
return-1;
printf ("Enter card no<press enter only to Exit>:");
Fgets (Pps->card,sizeof (Pps->card), stdin);
Trim (Pps->card);
if (strlen (pps->card) = = 0)
Return-2;
printf ("Enter age<press enter only to Exit>:");
Fgets (Strage,sizeof (strage), stdin);
Trim (strage);
if (strlen (strage) = = 0)
return-3;
Pps->age = Atoi (strage);

return 0;
}

int main (void)
{
$struct _db_person dbps;

$database exec01;
if (SQLCODE!= 0)
{
printf ("Open demo1 failure,sqlcode=%d\n", SQLCODE);
return-1;
}
while (1)
{
if (GetData (&dbps) <0)
Break
$insert into person (name, card, age) VALUES ($dbps. Name, $dbps. Card, $dbps. Age);
printf ("Insert Data result:sqlcode=%d\n", SQLCODE);
$declare vcursor cursor for select name, card, age into $dbps. Name, $dbps.
printf ("Declare vcursor result:sqlcode=%d\n", SQLCODE);
$open Vcursor;
printf ("Open vcursor result:sqlcode=%d\n", SQLCODE);
if (0==sqlcode)
{
while (1)
{
$fetch Vcursor;
if (0==sqlcode)
{
printf ("name=[%s],card=[%s],age=[%d]\n", dbps.name,dbps.card,dbps.age);
}
Else
{
if (sqlcode==100)
printf ("Fetch end!\n");
Else
printf ("Fetch failure! Sqlcode=%d\n ", SQLCODE);
Break
}
}
}
$close Vcursor;
$free Vcursor;
}
$disconnect current;

return 0;
}

The program embeds the Informix database in simple C, the source file is. ec file, the compiler is esql, header file directory: $ (informixdir)/include, admin tool dbaccess, usage: dbaccess [dbname], Dbschema, usage: dbschema [-t tabname]–d dbname [filename]

1. c file generation by preprocessor according to the. ec file

2, the system specified by the compiler to compile the. c file into obj file

3, by the system connector to the obj file and static library file connection, generate executable file

So you must specify the appropriate compiler for $CC, GCC or g++

Add environment variables after installing Informix under Linux

Ld_library_path= $INFORMIXDIR/lib:$/informixdir/lib/esql: $LD _library_path;

Export Ld_library_path;

Programming

Include header file: EXEC SQL include "dbdef.h";

Predefined variables: EXEC SQL define MaxLen 64;

To define a variable:

EXEC SQL BEGIN DECLARE section;

Char fname[maxlen+1];

EXEC SQL end DECLARE section;

To open a database:

EXEC SQL connect to ' dbname ';

To close the database:

EXEC SQL Disconnect current;

Define Cursor

EXEC SQL declare cursorname cursor FOR SELECT ...

Open cursor

EXEC SQL Open cursorname;

Reading data

EXEC SQL fetch cursorname;

Determine if the operation was successful

Sqlcode==0

Determine if the data is over

if (sqlcode==100);

Or in the following way:

Copy Code code as follows:

$include "Appdef.h";

$define MaxLen 64;

$char fname[maxlen+1];

$database dccdb;

$declare cursorname cursor for SELECT ...;

$open Cursorname;

$fetch Cursorname;

$close Cursorname;

$free Cursorname;

$disconnect Current


The following is the writing of makefile, be sure to link library functions, or there will be errors

Copy Code code as follows:

cc=gcc
exec01:exec01.o
Esql-o exec01-l$ (informixdir)/lib EXEC01.O
EXEC01.O:
Esql-c-i$ (informixdir)/incl/esql exec01.ec
Clean
Rm-f exec01 *.o exec01.c exec01

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.