程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C說話嵌入informix基本入門示例講授

C說話嵌入informix基本入門示例講授

編輯:關於C++

C說話嵌入informix基本入門示例講授。本站提示廣大學習愛好者:(C說話嵌入informix基本入門示例講授)文章只能為提供參考,不一定能成為您想要的結果。以下是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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved