歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

informix的嵌入SQL語句的一個實例

//*****************************dbconn.ec
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

EXEC SQL include "sqlca.h";
EXEC SQL include "sqlda.h";
EXEC SQL include "decimal.h";
EXEC SQL include "sqltypes.h";

EXEC SQL include "sqlhdr.h";

int disconnectDB()
{
EXEC SQL WHENEVER SQLERROR GOTO error;
EXEC SQL disconnect current;
puts("disconnece current!");
return 0;
error:
printf(" s %s l %d: error:sqlcode=%d\n", __FILE__, __LINE__, sqlca.sqlcode);
return -1;
}

int connectDB()
{
EXEC SQL begin declare section;
char *dbname;
EXEC SQL end declare section;

EXEC SQL WHENEVER SQLERROR GOTO error;

dbname = getenv("WORKDBS"); //環境變量WORKDBS是數據庫名,一般在setenv裡配置
EXEC SQL database :dbname;
printf("connect to INFORMIX[%s] successful!\n", dbname);

return 0;
error:
printf("s %s l %d: error:sqlcode=%d\n", __FILE__, __LINE__, sqlca.sqlcode);
return -1;
}

int selectDB()
{
EXEC SQL WHENEVER SQLERROR GOTO error;

EXEC SQL select * from TEMP_TABLE where column1 = 'test'; //數據庫裡的表查詢
printf("code == %d\n", sqlca.sqlcode);

return 0;

error:
printf("s %s l %d: error:sqlcode=%d\n", __FILE__, __LINE__, sqlca.sqlcode);
return -1;
}

//******************test.c

#include <stdio.h>
int connectDB();
int disconnectDB();
int selectDB();

int main()
{
connectDB();
selectDB();

disconnectDB();
return 0;
}

//***********************makefile

#include ${WORKPATH}/src/etc/Make.defines
#MIDDYDIR = ${YQHLDIR}/middleware/middy
#ZLIBDIR = ${YQHLDIR}/middleware/zlib-1.2.3
#CFLAGS = -g -w -I$(MIDDYDIR)/include -I$(INFORMIXDIR) -I$(INCLDIR) -I$(ZLIBDIR)
#INFOLIBS = -L$(INFORMIXDIR)/lib -lifasf -lasf -lifcss -lthasf \
# -L$(INFORMIXDIR)/lib/esql -lifsql -lsql -lthsql -lgen -lgls \
# -lglx -lifgen -lifgls -lifglx -lifos -lifxa -los -lthgen -lthos -lthxa -ltli -lm


INFORMIXDIR = /infx64
CFLAGS = -g -w
INFOLIBS = -L$(INFORMIXDIR)/lib -lifasf \
-L$(INFORMIXDIR)/lib/esql -lifsql -lgen -lgls -lifgen -lthgen -los -ltli -lm

all:dbconn.o test.o
cc $(CFLAGS) test.o dbconn.o -o $@ $(INFOLIBS)
test.o:test.c
cc -c test.c
dbconn.o:dbconn.ec
esql -c dbconn.ec
clean:
rm -f *.o all dbconn.c

//*************************************在 AIX 上把連接函數編譯成 動態庫 libconnect.so 和 libconnect.a

include ${WORKPATH}/src/etc/Make.defines
MIDDYDIR = ${YQHLDIR}/middleware/middy
ZLIBDIR = ${YQHLDIR}/middleware/zlib-1.2.3
CFLAGS = -g -w -I$(MIDDYDIR)/include -I$(INFORMIXDIR) -I$(INCLDIR) -I$(ZLIBDIR)
INFOLIBS = -L$(INFORMIXDIR)/lib -lifasf -lasf -lifcss -lthasf \
-L$(INFORMIXDIR)/lib/esql -lifsql -lsql -lthsql -lgen -lgls \
-lglx -lifgen -lifgls -lifglx -lifos -lifxa -los -lthgen -lthos -lthxa -ltli -lm


#INFORMIXDIR = /infx64
#CFLAGS = -g -w
#INFOLIBS = -L$(INFORMIXDIR)/lib -lifasf \
# -L$(INFORMIXDIR)/lib/esql -lifsql -lgen -lgls -lifgen -lthgen -los -ltli -lm

all:libdbconn.a test.o
cc $(CFLAGS) test.o -o $@ $(INFOLIBS) -L./ -ldbconn

test.o:
cc -c test.c
libdbconn.a:libdbconn.so
ar crv $@ libdbconn.so

libdbconn.so::dbconn.o
/usr/bin/nm -B -BCpg dbconn.o |awk -f $(YQHLDIR)/awk.f|sort -u >libdbconn.so.exp
$(CC) -bnoentry -bM:SRE -bE:libdbconn.so.exp -o libdbconn.so dbconn.o -L$(ZLIBDIR) -lz -L$(L
IB) -lgh $(INFOLIBS)

dbconn.o:dbconn.ec
esql -c dbconn.ec

clean:
rm -f *.o all dbconn.c libdbconn.a libdbconn.so libdbconn.so.exp
 

Copyright © Linux教程網 All Rights Reserved