C语言嵌入informix基础入门示例讲解
#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.card, $dbps.age from person;
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;
}
程序为简单的C中嵌入informix数据库,源文件为.ec文件,编译器为esql,头文件目录:$(INFORMIXDIR)/include,管理工具dbaccess,用法:dbaccess [dbname],dbschema,用法:dbschema [-t tabname] –d dbname [filename]
1、由预处理程序根据.ec文件生成.c文件
2、由系统指定的编译器将.c文件编译为obj文件
3、由系统连接程序将obj文件和静态库文件连接,生成可执行文件
所以必须为$CC指定合适的编译器,gcc或g++
linux下安装好informix后添加环境变量
LD_LIBRARY_PATH=$INFORMIXDIR/lib:$/INFORMIXDIR/lib/esql:$LD_LIBRARY_PATH;
export LD_LIBRARY_PATH;
编程
包含头文件:EXEC SQL include “dbdef.h”;
预定义变量:EXEC SQL define MAXLEN 64;
定义变量:
EXEC SQL BEGIN DECLARE SECTION;
char Fname[MAXLEN+1];
EXEC SQL END DECLARE SECTION;
打开数据库:
EXEC SQL connect to ‘dbname';
关闭数据库:
EXEC SQL disconnect current;
定义cursor
EXEC SQL declare cursorname cursor for select……
打开cursor
EXEC SQL open cursorname;
读取数据
EXEC SQL fetch cursorname;
判断操作是否成功
SQLCODE==0
判断数据是否已经结束
if( SQLCODE==100);
或者用下面的变成方式:
$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
下面是makefile的编写,一定要链接库函数,不然会出错
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
相关文章
C++实现LeetCode(80.有序数组中去除重复项之二)
这篇文章主要介绍了C++实现LeetCode(80.有序数组中去除重复项之二),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-07-07notepad介绍及插件cmake编译过程(替代notepad++)
这篇文章主要介绍了notepad介绍及插件cmake编译过程(替代notepad++),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-03-03Qt图形图像开发之Qt曲线图美化QChart QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开
这篇文章主要介绍了Qt图形图像开发之Qt曲线图美化QChart QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开数值消失效果实例,需要的朋友可以参考下2020-03-03
最新评论