说明,本文主要通过例子看看嵌入式C语言如何访问DB2数据库
环境:DB2 10.5.0.8/LINUX
$ db2 "create db test"
$ db2 "connect to testdb"
$ db2 "create table inst105.sailor(sname char(16), sid SMALLINT, rating SMALLINT, age SMALLINT)"
$ db2 "insert into inst105.sailor values ('yuppy', 22, 1, 20)"
$ db2 "insert into inst105.sailor values ('lubber', 31, 1, 25)"
$ db2 "insert into inst105.sailor values ('guppy', 44, 2, 31)"
$ db2 "insert into inst105.sailor values ('rusty', 58, 3, 47)"
假设db2cprog目录下有一个文件sage.sqc内容如下
/*--------------------------------------------------------------------
EXAMPLE of an embedded SQL C Program for DB2.
Connect to database test for this.
This APP takes one argument on the command line, a sailor's SID. It
then finds the sailor SID's age out of the table INST105.SAILOR (in
database test) and reports it. A dumb and not very interesting
APP, but it shows how things are done.
P. Godfrey NOV 2002
--------------------------------------------------------------------*/
#include
#include
#include
#include
#include
#include
#define EXIT 0
#define NOEXIT 1
/*--------------------------------------------------------------------
Include DB2's SQL error reporting facility.
--------------------------------------------------------------------*/
EXEC SQL INCLUDE SQLCA ;
/*--------------------------------------------------------------------
Declare the SQL interface variables.
--------------------------------------------------------------------*/
EXEC SQL BEGIN DECLARE SECTION ;
short sage;
short sid;
char sname[16];
EXEC SQL END DECLARE SECTION ;
/*--------------------------------------------------------------------
Declare variables to be used in the following C program.
--------------------------------------------------------------------*/
char msg[1025];
int rc;
int errcount;
/*--------------------------------------------------------------------
This macro prints the message in the SQLCA if the return code is 0
and the SQLCODE is not 0.
--------------------------------------------------------------------*/
#define PRINT_MESSAGE() \
{ \
if (rc == 0 && sqlca.sqlcode != 0) \
{ \
sqlaintp(msg, 1024, 0, &sqlca); \
printf("%s\n",msg); \
} \
}
/*--------------------------------------------------------------------
This macro prints out all feilds in the SQLCA.
--------------------------------------------------------------------*/
#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("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"); \
}
/*--------------------------------------------------------------------
This macro prints the message in the SQLCA if one exists. If the
return code is not 0 or the SQLCODE is not expected, an error occurred
and must be recorded.
--------------------------------------------------------------------*/
#define CHECK_SQL(code,text_string,eExit) \
{ \
PRINT_MESSAGE(); \
if (rc != 0 || sqlca.sqlcode != code) { \
printf("%s\n",text_string); \
printf("Expected code = %d\n",code); \
if (rc == 0) { \
DUMP_SQLCA(); \
} \
else printf("RC: %d\n",rc); \
errcount += 1; \
if (eExit == EXIT) goto errorexit; \
} \
}
/*--------------------------------------------------------------------
The PROGRAM.
--------------------------------------------------------------------*/
int main (int argc, char *argv[])
{
/* Grab the first command argument. This is the SID. */
if (argc > 1) {
sid = atoi(argv[1]);
printf("SID requested is %d.\n", sid);
/* If there is no arguement, bail. */
} else {
printf("Which SID?\n");
exit(0);
}
EXEC SQL CONNECT TO TEST;
CHECK_SQL(0, "Connect failed", EXIT);
/* Find the name and age of sailor SID. */
EXEC SQL SELECT SNAME, AGE into :sname, :sage
FROM INST105.SAILOR
WHERE sid = :sid;
CHECK_SQL(0, "The SELECT query failed.", EXIT);
/* Report the age. */
printf("Sailor %s's age is %d.\n", sname, sage);
printf("Executed Successfuly\n") ;
printf("Bye\n") ;
errorexit:
EXEC SQL CONNECT RESET;
}
inst105@node01:~$ cd db2cprog/
inst105@node01:~/db2cprog$ ls
sage.sqc
inst105@node01:~/db2cprog$ db2 precompile sage.sqc bindfile
LINE MESSAGES FOR sage.sqc
------ --------------------------------------------------------------------
SQL0060W The "C" precompiler is in progress.
SQL0091W Precompilation or binding was ended with "0"
errors and "0" warnings.
inst105@node01:~/db2cprog$ ls
sage.bnd sage.c sage.sqc
inst105@node01:~/db2cprog$ db2 bind sage.bnd
LINE MESSAGES FOR sage.bnd
------ ---------------------------------------------- ----------------------
SQL0061W The binder is in progress.
SQL0091N Binding was ended with "0" errors and "0" warnings.
inst105@node01:~/db2cprog$ gcc -I $DB2_HOME/include -c sage.c
inst105@node01:~/db2cprog$ ls
sage.bnd sage.c sage.o sage.sqc
inst105@node01:~/db2cprog$ gcc -o sage.exe sage.o -L $DB2LIB -l db2
inst105@node01:~/db2cprog$ ls
sage.bnd sage.c sage.exe sage.o sage.sqc
inst105@node01:~/db2cprog$ ./sage.exe 44
SID requested is 44.
Sailor guppy 's age is 31.
Executed Successfuly
Bye
inst105@node01:~/db2cprog$ ./sage.exe 22
SID requested is 22.
Sailor yuppy 's age is 20.
Executed Successfuly
Bye
附上一个通过游标循环访问表中所有记录的程序
/*--------------------------------------------------------------------
EXAMPLE of an embedded SQL C Program for DB2.
Connect to database c3421m for this.
This APP fetches and prints each of the tuples from INST105.SAILOR (in
database c3421m).
P. Godfrey NOV 2002
--------------------------------------------------------------------*/
#include
#include
#include
#include
#include
#include
#define EXIT 0
#define NOEXIT 1
/*--------------------------------------------------------------------
Include DB2's SQL error reporting facility.
--------------------------------------------------------------------*/
EXEC SQL INCLUDE SQLCA ;
/*--------------------------------------------------------------------
Declare the SQL interface variables.
--------------------------------------------------------------------*/
EXEC SQL BEGIN DECLARE SECTION ;
short sid;
short sage;
short rating;
char sname[16];
EXEC SQL END DECLARE SECTION ;
/*--------------------------------------------------------------------
Declare variables to be used in the following C program.
--------------------------------------------------------------------*/
char msg[1025];
int rc;
int errcount;
/*--------------------------------------------------------------------
This macro prints the message in the SQLCA if the return code is 0
and the SQLCODE is not 0.
--------------------------------------------------------------------*/
#define PRINT_MESSAGE() \
{ \
if (rc == 0 && sqlca.sqlcode != 0) \
{ \
sqlaintp(msg, 1024, 0, &sqlca); \
printf("%s\n",msg); \
} \
}
/*--------------------------------------------------------------------
This macro prints out all feilds in the SQLCA.
--------------------------------------------------------------------*/
#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"); \
}
/*--------------------------------------------------------------------
This macro prints the message in the SQLCA if one exists. If the
return code is not 0 or the SQLCODE is not expected, an error occurred
and must be recorded.
--------------------------------------------------------------------*/
#define CHECK_SQL(code,text_string,eExit) \
{ \
PRINT_MESSAGE(); \
if (rc != 0 || sqlca.sqlcode != code) { \
printf("%s\n",text_string); \
printf("Expected code = %d\n",code); \
if (rc == 0) { \
DUMP_SQLCA(); \
} \
else printf("RC: %d\n",rc); \
errcount += 1; \
if (eExit == EXIT) goto errorexit; \
} \
}
/*--------------------------------------------------------------------
The PROGRAM.
--------------------------------------------------------------------*/
int main (int argc, char *argv[]) {
EXEC SQL CONNECT TO TEST;
CHECK_SQL(0, "Connect failed", EXIT);
/* Find the name and age of sailor SID. */
EXEC SQL DECLARE sailorC CURSOR FOR
SELECT SID, SNAME, AGE RATING
FROM INST105.SAILOR;
CHECK_SQL(0, "The SELECT query failed.", EXIT);
EXEC SQL OPEN sailorC;
printf("sid sname age rating\n");
while (1) {
EXEC SQL FETCH sailorC INTO :sid, :sname, :sage, :rating;
if (SQLCODE != 0) break;
printf("%3d %-15s %3d %3d\n", sid, sname, sage, rating);
}
EXEC SQL CLOSE sailorC;
errorexit:
EXEC SQL CONNECT RESET;
}
参考资料:
https://www.eecs.yorku.ca/course_archive/2015-16/F/3421/db2notes/embed-c.html