C語(yǔ)言嵌入informix基礎(chǔ)入門(mén)示例講解
#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;
}
程序?yàn)楹?jiǎn)單的C中嵌入informix數(shù)據(jù)庫(kù),源文件為.ec文件,編譯器為esql,頭文件目錄:$(INFORMIXDIR)/include,管理工具dbaccess,用法:dbaccess [dbname],dbschema,用法:dbschema [-t tabname] –d dbname [filename]
1、由預(yù)處理程序根據(jù).ec文件生成.c文件
2、由系統(tǒng)指定的編譯器將.c文件編譯為obj文件
3、由系統(tǒng)連接程序?qū)bj文件和靜態(tài)庫(kù)文件連接,生成可執(zhí)行文件
所以必須為$CC指定合適的編譯器,gcc或g++
linux下安裝好informix后添加環(huán)境變量
LD_LIBRARY_PATH=$INFORMIXDIR/lib:$/INFORMIXDIR/lib/esql:$LD_LIBRARY_PATH;
export LD_LIBRARY_PATH;
編程
包含頭文件:EXEC SQL include “dbdef.h”;
預(yù)定義變量:EXEC SQL define MAXLEN 64;
定義變量:
EXEC SQL BEGIN DECLARE SECTION;
char Fname[MAXLEN+1];
EXEC SQL END DECLARE SECTION;
打開(kāi)數(shù)據(jù)庫(kù):
EXEC SQL connect to ‘dbname';
關(guān)閉數(shù)據(jù)庫(kù):
EXEC SQL disconnect current;
定義cursor
EXEC SQL declare cursorname cursor for select……
打開(kāi)cursor
EXEC SQL open cursorname;
讀取數(shù)據(jù)
EXEC SQL fetch cursorname;
判斷操作是否成功
SQLCODE==0
判斷數(shù)據(jù)是否已經(jīng)結(jié)束
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的編寫(xiě),一定要鏈接庫(kù)函數(shù),不然會(huì)出錯(cuò)
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
相關(guān)文章
vscode不同項(xiàng)目使用不同的插件的實(shí)現(xiàn)
本文主要介紹了vscode不同項(xiàng)目使用不同的插件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
C/C++多參數(shù)函數(shù)參數(shù)的計(jì)算順序與壓棧順序的示例代碼
這篇文章主要介紹了C/C++多參數(shù)函數(shù)參數(shù)的計(jì)算順序與壓棧順序,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
C語(yǔ)言實(shí)現(xiàn)txt數(shù)據(jù)讀入內(nèi)存/CPU緩存實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)txt數(shù)據(jù)讀入內(nèi)存/CPU緩存實(shí)例詳解的相關(guān)資料,這里對(duì)實(shí)現(xiàn)該函數(shù)進(jìn)行了代碼實(shí)現(xiàn),需要的朋友可以參考下2017-01-01
C++實(shí)現(xiàn)獲取系統(tǒng)時(shí)間的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了使用C++實(shí)現(xiàn)獲取系統(tǒng)時(shí)間的一些常用方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以了解下2024-03-03
C語(yǔ)言實(shí)現(xiàn)兩個(gè)變量值交換的三種方式
兩個(gè)變量值的交換是編程中最常見(jiàn)的問(wèn)題之一,以下將介紹三種變量的交換方式,其中第一種方式是最常用也是最實(shí)用的,后兩種方式一般只在特殊限制下使用,需要的朋友可以參考下2025-03-03

