pro c 访问 MySQL,pro*C访问DB2数据库

这篇博客展示了如何使用Pro*C预编译器进行C语言编程来连接并查询DB2数据库。代码中包含了连接数据库、执行SQL语句、错误检查和SQLCA结构的详细打印。此外,还提供了Makefile文件用于编译和绑定过程,展示了一个完整的DB2数据库操作流程。
摘要由CSDN通过智能技术生成

pro*C访问DB2数据库的例子

test.sqc

$ cat test.sqc

#include

#include

EXEC SQL INCLUDE SQLCA ;

EXEC SQL BEGIN DECLARE SECTION ;

char hvalue[16];

EXEC SQL END DECLARE SECTION ;

char sqlcamsg[1025];

#define DUMP_SQLCA() \

{ \

printf("****************** DUMP OF SQLCA ******************\n"); \

printf("SQLCAID: %s\n", sqlca.sqlcaid); \

printf("SQLCABC: %d\n", sqlca.sqlcabc); \

printf("SQLCODE: %d\n", sqlca.sqlcode); \

printf("SQLERRML: %d\n", sqlca.sqlerrml); \

printf("SQLERRMC: %s\n", sqlca.sqlerrmc); \

printf("SQLERRP: %s\n", sqlca.sqlerrp); \

printf("SQLERRD[0]: %d\n", sqlca.sqlerrd[0]); \

printf("SQLERRD[1]: %d\n", sqlca.sqlerrd[1]); \

printf("SQLERRD[2]: %d\n", sqlca.sqlerrd[2]); \

printf("SQLERRD[3]: %d\n", sqlca.sqlerrd[3]); \

printf("SQLERRD[4]: %d\n", sqlca.sqlerrd[4]); \

printf("SQLERRD[5]: %d\n", sqlca.sqlerrd[5]); \

printf("SQLWARN: %s\n", sqlca.sqlwarn); \

printf("SQLSTATE: %s\n", sqlca.sqlstate); \

printf("***************** END OF SQLCA DUMP **************\n"); \

}

#define CHECK_SQL(failure_string) \

{ \

if (sqlca.sqlcode != 0) { \

printf("!!!ERROR: %s\n", failure_string); \

sqlaintp(sqlcamsg, 1024, 0, &sqlca); \

printf("%s\n", sqlcamsg); \

DUMP_SQLCA(); \

goto done; \

} \

}

main (int argc, char *argv[])

{

EXEC SQL CONNECT TO USER USING ;

CHECK_SQL("Connect failed");

EXEC SQL SELECT 'ABCD' into :hvalue FROM SYSIBM.SYSDUMMY1;

CHECK_SQL("Select failed");

printf("Select Result: [%s]\n", hvalue);

done:

EXEC SQL CONNECT RESET;

}

makefile

$ cat makefile

TARGET=test

DB2DIR?=~/sqllib

$(TARGET): $(TARGET).sqc

db2 connect to user using

db2 prep $(TARGET).sqc bindfile

db2 bind $(TARGET).bnd

#db2 list packages

#db2 drop package package-name

gcc -o $(TARGET) -I$(DB2DIR)/include $(TARGET).c -L$(DB2DIR)/lib64 -ldb2

clean:

rm -f $(TARGET) $(TARGET).bnd $(TARGET).c

run

$ make

db2 connect to user using

Database Connection Information

Database server =

SQL authorization ID =

Local database alias =

db2 prep test.sqc bindfile

LINE MESSAGES FOR test.sqc

------ --------------------------------------------------------------------

SQL0060W The "C" precompiler is in progress.

SQL0091W Precompilation or binding was ended with "0"

errors and "0" warnings.

db2 bind test.bnd

LINE MESSAGES FOR test.bnd

------ --------------------------------------------------------------------

SQL0061W The binder is in progress.

SQL0091N Binding was ended with "0" errors and "0" warnings.

#db2 list packages

#db2 drop package package-name

gcc -o test -I${DB2DIR}/include test.c -L${DB2DIR}/lib64 -ldb2

$

$ ./test

Select Result: [ABCD]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值