DB2嵌入式編程

1.代碼test1.sqchtml

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlca.h>
EXEC SQL INCLUDE SQLCA;
int main()
{
 EXEC SQL BEGIN DECLARE SECTION;
 char firstname[130];
 char userid[9];
 char passwd[19];
 EXEC SQL END DECLARE SECTION;

 EXEC SQL CONNECT TO sample;
 EXEC SQL SELECT FIRSTNME INTO :firstname
 FROM EMPLOYEE fetch first 1 row only;

 printf( "First name = %s\n", firstname );
 EXEC SQL CONNECT RESET;
 return 0;
}

其中樣例數據庫sample可用DB2自帶的實用程序db2sampl來建立。sql

2.鏈接數據庫:數據庫

     db2 connect to sample user DBUSER using DBPWD
3.預編譯:fetch

     db2 prep test1.sqc bindfile,會生成test1.c和test1.bnd文件
4.綁定bnd文件ui

    db2 bind test1.bnd (GRANT PUBLIC),同時指定訪問權限(GRANT PUBLIC)-----可選.net

5.復位數據庫鏈接:3d

db2 connect reset
DB20000I  SQL 命令成功完成。
db2 terminate
DB20000I  TERMINATE 命令成功完成。

後面的步驟就沒必要和數據庫創建鏈接,故復位。code

6.編譯鏈接、運行server

gcc -c test1.c  -g -I/home/db2inst1/sqllib/include
gcc -o test1  -g -I/home/db2inst1/sqllib/include test1.o -L/home/db2inst1/sqllib/lib -Wl,-rpath,/home/db2inst1/sqllib/lib -ldb2
$ ./test1 
First name = CHRISTINE

7.以上步驟較多,寫成Makefilehtm

DB2PATH = /home/$(DB2INSTANCE)/sqllib

# Use the gnu c compiler
CC= gcc

# The required compiler flags
CFLAGS= $(EXTRA_CFLAGS) -g -I$(DB2PATH)/include

# The required libraries 
LIBS= -L$(DB2PATH)/lib -Wl,-rpath,$(DB2PATH)/lib -ldb2

# To connect to a remote SAMPLE database cataloged on the client machine
# with another name, update the DB variable.
DB=sample
# Set UID and PWD if neccesary  
UID=
PWD=

COPY=cp
ERASE=rm -f


#############################################################################
#  3 -- COMMANDS TO MAKE INDIVIDUAL SAMPLES
#                  3a -  utilities
#                  3b -  non embedded SQL, non client/server samples
#                  3c -  embedded SQL, non client/server samples
#                  3d -  client/server samples (mixed)
#############################################################################
EXE = test1
SQC = test1.sqc
C   = test1.c
BND = test1.bnd
OBJ = test1.o

all : $(EXE)
$(EXE) : $(OBJ)
	$(CC) -o $@ $(CFLAGS) $(OBJ) $(LIBS)
$(C) $(BND) : $(SQC)
	db2 connect to $(DB)
	db2 prep $(SQC) bindfile
	db2 bind $(BND) GRANT PUBLIC
	db2 connect reset
	db2 terminate
$(OBJ) : $(C)
	$(CC) -c $(C) $(CFLAGS)
clean :	\
	cleangen \
	cleanemb
cleangen :
	$(ERASE) *.o *.map *~

cleanemb :
	$(ERASE) $(C)

cleanall : \
	clean
	$(ERASE) *.bnd
	$(ERASE) $(EXE)

使用 PRECOMPILE 命令來預編譯嵌入式 SQL 應用程序

基於C語言的DB2數據庫開發

構建嵌入式SQL應用程序

嵌入式sql c文件編譯

db2嵌入式sql實例,轉移數據庫字段

相關文章
相關標籤/搜索