Sample introduction to embedded informix in C language
- 2020-04-02 01:59:09
- OfStack
#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=%dn",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=%dn",SQLCODE);
$declare vcursor cursor for select name, card, age into $dbps.name, $dbps.card, $dbps.age from person;
printf("declare vcursor result: SQLCODE=%dn",SQLCODE);
$open vcursor;
printf("open vcursor result: SQLCODE=%dn",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=%dn",SQLCODE);
break;
}
}
}
$close vcursor;
$free vcursor;
}
$disconnect current;
return 0;
}
Program for simple C embedded informix database, source file for. Ec file, compiler for esql, header file directory: $(INFORMIXDIR)/include, administrative tool dbaccess, usage: dbaccess [dbname], dbschema, usage: dbschema [-t tabname] dbname [filename]
1. Generate. C file according to. Ec file by preprocessor
2. The compiler specified by the system compiles. C files into obj files
3, by the system connection program will be obj file and static library file connection, generate executable files
So you must specify an appropriate compiler, GCC or g++, for $CC
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 variable: EXEC SQL define MAXLEN 64;
Define variables:
EXEC SQL BEGIN DECLARE SECTION;
Char Fname [MAXLEN + 1];
EXEC SQL END DECLARE SECTION;
Open database:
EXEC SQL connect to 'dbname';
Close the database:
The EXEC SQL disconnect current;
Define the cursor
EXEC SQL declare cursorname cursor for select...
Open the cursor
The EXEC SQL open cursorname;
Read the data
The EXEC SQL fetch cursorname;
Determine if the operation was successful
SQLCODE = = 0
Determine if the data is complete
If (SQLCODE = = 100);
Or in the following way:
$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
Here's how to write the makefile. Be sure to link library functions, or you'll get an error
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