服务器之家

服务器之家 > 正文

C语言嵌入informix基础入门示例讲解

时间:2021-01-10 12:15     来源/作者:C语言教程网

复制代码 代码如下:


#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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部