偶然使用db2,编译时遇到问题经过测试,解决后记到这里备查。
一、代码:test_db2.SQC【
[code]
/************************************************************
函数名称:
功能说明: 输入参数:
输出参数:
返 值:
0: 成功
-1: 失败
************************************************************/
EXEC SQL INCLUDE SQLCA;
int main(int argc, char **argv)
{
int iRet=0;
EXEC SQL BEGIN DECLARE SECTION;
char Uname[8+1]="test";
char Pwd[8+1]="test";
long TotCnt = 0;
char MsgIdNo[35+1];
EXEC SQL END DECLARE SECTION;
EXEC SQL CONNECT to EEPS USER :Uname USING :Pwd;
memset( MsgIdNo, 0x00, sizeof(MsgIdNo) );
EXEC SQL SELECT count(*) INTO :TotCnt FROM ChkTotRcv;
if( SQLCODE ) {
printf( "SQLCODE=[%d]/n",SQLCODE );
return(SQLCODE);
}
else {
printf( "Cnt=[%d]/n", TotCnt);
printf( "SQLCODE=[%d]/n",SQLCODE );
}
return 0;
}
[/code]
】
二、编译命令:
这个限于用户环境和DB2的安装环境,这里使用的是AIX.
[
[code]
[:/app/zgl_tmp] cat mk.log
cc -E test_db2.SQC > test_db2.sqc
db2 perp test_db2.sqc BINDFILE USING test_db2.bnd blocking all qualifier csp;
cc -E test_db2.SQC > test_db2.sqc
db2 connect to $DBNAME USER $DBUSER USING $DBPASS;
db2 prep test_db2.sqc BINDFILE USING test_db2.bnd blocking all qualifier csp;
db2 bind test_db2.bnd datetime iso isolation cs ;
cc -o test_db2.o -c -DSCOUNIX -DAIX_UNIX -DDB_DB2 -I/home/db2inst3/sqllib/include -g -DDEBUG test_db2.c
cc -o testdb2 test_db2.o -L/home/db2inst3/sqllib/lib -ldb2 -lc -lm
ls -l
[:/app/zgl_tmp]./testdb2
Cnt=[0]
SQLCODE=[0]
cd
[:/app/zgl_tmp]
[/code]
]
三、整理的makefile
[
[code]
#
include $(APPDIR)/inc/makefile.h
EEPSINC=$(APPDIR)/eeps/inc
CFLAGS = -DSCOUNIX -DAIX_UNIX -DDB_DB2 -I/home/db2inst3/sqllib/include -g -DDEBUG
MFLAGS = -L/home/db2inst3/sqllib/lib -ldb2 -lc -lm
MAINSRC = test_db2.o
#all: test_db2
test_db2:$(MAINSRC)
$(CC) -o $@ $(MAINSRC) $(MFLAGS)
# $(RM) -f *.o
.SUFFIXES:.c .SQC
.SQC.o:
$(CC) -E $*.SQC > $*.sqc
db2 connect to $(DBNAME) USER $(DBUSER) USING $(DBPASS);
db2 prep $*.sqc BINDFILE USING $(BNDDIR)$*.bnd blocking all qualifier csp;
db2 bind $(BNDDIR)$*.bnd datetime iso isolation cs ;
$(CC) -o $*.o -c $(CFLAGS) $*.c
# $(RM) -f $*.c $*.sqc
#.c.o:
# $(CC) -c $(CFLAGS) $<
clear:
$(RM) *.bnd *.o *.sqc test_db2.c
[/code]
]