MySQL下
//dbproc.c
/************************************************************* FileName : dbproc.c FileFunc : C语言接口访问MySQL Version : V0.1 Author : Sunrier Date : 2012-06-14 Descp : Linux下使用C语言访问MySQL函数 *************************************************************/ #include <stdio.h> #include <stdlib.h> #include <mysql.h> static MYSQL s_my_connection; static int s_iDbconnected = 0;//数据库连接标志 连接时为1,断开时为0 int mysql_login(char* pServer,char *pUser,char *pPassword,char *pDataBase) { MYSQL *conn_ptr = NULL; MYSQL_RES *res_ptr = NULL; MYSQL_ROW sqlrow; int iRetCode = 1; unsigned int uiTimeOut = 7; if( s_iDbconnected ) return 0; conn_ptr = mysql_init(&s_my_connection); if( !conn_ptr ) { fprintf(stderr,"mysql_init failed ! \n"); return EXIT_FAILURE; } iRetCode = mysql_options(&s_my_connection,MYSQL_OPT_CONNECT_TIMEOUT,(const char *)&uiTimeOut); if( iRetCode ) { fprintf(stderr,"Connection is timeout! \n"); return EXIT_FAILURE; } conn_ptr = NULL; conn_ptr = mysql_real_connect(&s_my_connection,pServer,pUser,pPassword,pDataBase,0,NULL,0); if( conn_ptr ) { printf("MySQL Connection success!\n"); s_iDbconnected = 1; } else { fprintf(stderr,"Connection failed!\n"); if( mysql_errno(&s_my_connection) ) { fprintf(stderr,"Connection error %d: %s!\n",mysql_errno(&s_my_connection),mysql_error(&s_my_connection)); return EXIT_FAILURE; } } return EXIT_SUCCESS; } void mysql_logout( void ) { if( s_iDbconnected ) mysql_close(&s_my_connection);//关闭连接 s_iDbconnected = 0 ; }
//demo.c
/************************************************************* FileName : demo.c FileFunc : 测试登陆和关闭MySQL数据库 Version : V0.1 Author : Sunrier Date : 2012-06-14 Descp : Linux下使用C语言访问MySQL函数 *************************************************************/ #include <stdio.h> int main(int argc,char *argv[]) { int iRetCode = 0; char szServer[20] = "localhost"; //char szServer[20] = "192.168.2.8"; char szUser[20] = "Sunrier"; char szPassword[20] = "redhat"; char szDatabase[20] = "test"; iRetCode = mysql_login(szServer,szUser,szPassword,szDatabase); if( iRetCode ) { system("service mysqld start"); iRetCode = mysql_login(szServer,szUser,szPassword,szDatabase); } printf("iRetCode = %d \n",iRetCode); mysql_logout(); return 0; }
//makefile
#makefile all:demo demo:demo.c dbproc.c @gcc -I/usr/include/mysql $? -L/usr/lib/mysql -lmysqlclient -lz -lm -o demo # gcc -I/usr/include/mysql demo.c dbproc.c -L/usr/lib/mysql -lmysqlclient -lz -lm -o demo clean: @ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ |xargs rm -rf #makefile
[root@localhost MYSQL]# ls
dbproc.c demo.c makefile
[root@localhost MYSQL]# make
[root@localhost MYSQL]# ./demo
Connection failed!
Connection error 2002: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)!
启动 MySQL: [确定]
MySQL Connection success!
iRetCode = 0
[root@localhost MYSQL]#
DB2下
//dbproc.sqc
/************************************************************* FileName : dbproc.sqc FileFunc : C语言接口访问本地DB2数据库 Version : V0.1 Author : Sunrier Date : 2012-06-15 Descp : Linux下使用C语言访问DB2函数 *************************************************************/ //(嵌入SQL语句的关键字不区分大小写) #include <stdio.h> #include <string.h> #include <sqlca.h> #define PARAERR 0x04 #define DATABASEERR 0x08 EXEC SQL include sqlca; int check_error(char szMessage[]) { if( sqlca.sqlcode ) { printf("Check error report : \n"); printf("Error occured : %s ,sqlcode = [%d] \n",szMessage,sqlca.sqlcode); return 1; } return 0; } int db2_login(int iArgcFlag,char *pUserName,char *pPassword,char *pDataBase) { int iRetCode = 1; EXEC SQL BEGIN DECLARE SECTION; char szUserName[50]; char szPassword[50]; char szDataBase[50]; EXEC SQL END DECLARE SECTION; memset(szUserName,0,sizeof(szUserName)); memset(szPassword,0,sizeof(szPassword)); memset(szDataBase,0,sizeof(szDataBase)); strcpy(szDataBase,pDataBase); if( 1==iArgcFlag ) { EXEC SQL CONNECT TO:szDataBase ; } else { if( 3==iArgcFlag ) { strcpy(szUserName,pUserName); strcpy(szPassword,pPassword); EXEC SQL CONNECT TO:szDataBase USER:szUserName USING:szPassword; } else { printf("Usage : argc found error !\n"); return PARAERR; } } iRetCode = check_error("CONNECT TO DATABASE"); return iRetCode; } void db2_logout( void ) { EXEC SQL CONNECT RESET; check_error("CONNECT RESET"); }
//demo.c
/************************************************************* FileName : demo.c FileFunc : 测试登陆和关闭DB2数据库 Version : V0.1 Author : Sunrier Date : 2012-06-15 Descp : Linux下使用C语言访问DB2函数 *************************************************************/ #include <stdio.h> int main(int argc,char *argv[]) { int iRetCode = 0; int iArgcFlag; char szUserName[20] = "Sunrier"; char szPassword[20] = "redhat"; char szDatabase[20] = "test"; iArgcFlag = 3; iRetCode = db2_login(iArgcFlag,szUserName,szPassword,szDatabase); printf("iRetCode = %d \n",iRetCode); if( iRetCode ) { printf("DB2 Connection failure !\n"); return iRetCode; } else { printf("DB2 Connection success !\n"); } db2_logout(); return 0; }
//makefile
all:demo DB2INCPATH=/home/db2inst1/sqllib/include DB2LIBPATH=/home/db2inst1/sqllib/lib demo:demo.c dbproc.sqc @db2 connect to test @db2 prep dbproc.sqc @gcc -ldb2 -L/usr/lib -lm -o demo demo.c dbproc.c # @gcc -L/home/db2inst1/sqllib/lib -ldb2 -L/usr/lib -lm -o demo dbproc.c clean: @ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sqc$$ | grep -v [.]sql$$ |xargs rm -rf #@db2start //数据库的启动 #@db2stop //数据库的停止 #db2 connect to test //连接test数据库 #db2 prep dbproc.sqc bindfile //prep命令生成.c文件和.bnd文件( 加bindfile选项 ) #db2 bind dbproc.bnd //bind命令生成PACKET #gcc -L/home/db2inst1/sqllib/lib -ldb2 -L/usr/lib -lm -o test test.c //生成可执行文件 #@db2 -r rep.rpt prep dbproc.sqc //把db2 prep dbproc.sqc执行后的信息输出保存到rep.rpt报告文件中(-r的作用将输出保存到报告文件) #@db2 +o prep dbproc.sqc //不显示db2 prep dbproc.sqc执行后的信息(+o的作用为不显示执行信息) ## Disconnect from the database. #@db2 connect reset//db2 disconnect current//断开数据库连接,后台db2bp依然存在 #@db2 terminate //断开数据库连接,干掉db2bp
[Sunrier@localhost DB2]$ ls
dbproc.sqc demo.c makefile
[Sunrier@localhost DB2]$ make
Database Connection Information
Database server = DB2/LINUX 8.2.3
SQL authorization ID = Sunrier
Local database alias = TEST
LINE MESSAGES FOR dbproc.sqc
------ --------------------------------------------------------------------
SQL0060W The "C" precompiler is in progress.
SQL0091W Precompilation or binding was ended with "0"
errors and "0" warnings.
[Sunrier@localhost DB2]$ ls
dbproc.c dbproc.sqc demo demo.c makefile
[Sunrier@localhost DB2]$ ./demo
iRetCode = 0
DB2 Connection success !
[Sunrier@localhost DB2]$
Informix下
//informixdbproc.ec
/************************************************************* FileName : informixdbproc.ec FileFunc : C语言接口访问Informix数据库 Version : V0.1 Author : Sunrier Date : 2012-07-27 Descp : Linux下使用C语言访问Informix函数 *************************************************************/ #include <stdio.h> #include <string.h> EXEC SQL include sqlca; int check_error(char szMessage[]) { if( sqlca.sqlcode ) { printf("Check error report : \n"); printf("Error occured : %s ,sqlcode = [%d] \n",szMessage,sqlca.sqlcode); return 1; } return 0; } int informix_login(char *pDataBase) { int iRetCode = 1; EXEC SQL BEGIN DECLARE SECTION; char szDataBase[30]; EXEC SQL END DECLARE SECTION; memset(szDataBase,0,sizeof(szDataBase)); strcpy(szDataBase,pDataBase); EXEC SQL CONNECT TO:szDataBase ; iRetCode = check_error("CONNECT TO DATABASE"); return iRetCode; } int informix_logout(char *pDataBase) { int iRetCode = 1; EXEC SQL BEGIN DECLARE SECTION; char szDataBase[50+1]; EXEC SQL END DECLARE SECTION; memset(szDataBase,0,sizeof(szDataBase)); strncpy(szDataBase,pDataBase,50); EXEC SQL DISCONNECT :szDataBase; iRetCode = check_error("DisConnect"); return iRetCode; } int informix_logout_ex( void ) { int iRetCode = 1; EXEC SQL DISCONNECT CURRENT; iRetCode = check_error("CONNECT RESET"); return iRetCode; }
//demo.c
/************************************************************* FileName : demo.c FileFunc : 测试登陆和关闭本地Informix数据库 Version : V0.1 Author : Sunrier Date : 2012-07-27 Descp : Linux下使用C语言访问Informix函数 *************************************************************/ #include <stdio.h> int main(int argc,char *argv[]) { int iRetCode = 0; char szDatabase[30] = "test"; iRetCode = informix_login(szDatabase); printf("iRetCode = %d \n",iRetCode); if( iRetCode ) { printf("Informix Connection failure !\n"); } else { printf("Informix Connection success !\n"); } /*informix_logout(szDatabase); */ return 0; }
//makefile
#makefile OBJS = demo all:$(OBJS) CFLAGS = -O -w -ansi #CFLAGS = -O -Wall -ansi CC = gcc $(CFLAGS) $PATH=$PATH:$HOME/bin:/home/informix/bin:$HOME/sbin: export PATH unset USERNAME INFORMIXDIR=/home/informix ONCONFIG=onconfig INFORMIXSERVER=online LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INFORMIXDIR/lib:$INFORMIXDIR/lib/esql export INFORMIXDIR INFORMIXSERVER ONCONFIG LD_LIBRARY_PATH DB_LOCALE=zh_CN.gb CLIENT_LOCALE=zh_CN.gb SERVER_LOCALE=zh_CN.gb export DB_LOCALE CLIENT_LOCALE SERVER_LOCALE export LD_ASSUME_KERNEL=2.4.1 PATH=$INFORMIXDIR/bin:$PATH export PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/src/lib export LD_LIBRARY_PATH TERMCAP=$INFORMIXDIR/etc/Termcap export TERMCAP ETCPATH=$HOME/etc export ETCPATH #Set system path MAINPATH=$(HOME) SRCPATH=$(MAINPATH)/src BINPATH=$(MAINPATH)/bin SYSINCLPATH=$(MAINPATH)/include SYSLIBPATH=$(MAINPATH)/lib APPINCLPATH=$(SRCPATH)/include APPLIBPATH=$(SRCPATH)/lib #EC = esql -I$(SYSINCLPATH) -I$(APPINCLPATH) -L$(SYSLIBPATH) -L$(APPLIBPATH) EC = esql INFORMIXLINCPATH = -I/home/informix/bin INFORMIXLIBPATH = -L/usr/lib/informix INFORMIXLIB = -lm demo:demo.c informixdbproc.ec @echo -e "Start compile Informix file ...... " @$(EC) -o demo demo.c informixdbproc.ec #$(EC) -o demo demo.c informixdbproc.ec 不会生成informixdbproc.o文件,只生成informixdbproc.c文件 #或者 #$(EC) -c informixdbproc.ec 会生成.c和.o文件执行后会生成informixdbproc.c和informixdbproc.o这两个文件 #@$(EC) -c informixdbproc.ec #$(EC) -o demo demo.c informixdbproc.c 生成可执行文件 #@$(EC) -o demo demo.c informixdbproc.c #或$(EC) -o demo demo.c informixdbproc.o 生成可执行文件 #@$(EC) -o demo demo.c informixdbproc.o # 其中$(EC) -o demo demo.c informixdbproc.c和$(EC) -o demo demo.c informixdbproc.o一样 #或者使用后缀方式 #.SUFFIXES: .ec #demo:demo.o informixdbproc.o # @$(EC) -o $@ $? #.c.o: # @$(CC) -c $< #.ec.o: # @$(EC) -c $< # @rm -f $*.c clean: @rm -rf informixdbproc.c @ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ | grep -v [.]ec$$ | xargs rm -rf #makefile
[Sunrier@localhost Informix]$ ls
demo demo.c informixdbproc.ec makefile
[Sunrier@localhost Informix]$ make
Start compile Informix file ......
[Sunrier@localhost Informix]$ ls
demo demo.c informixdbproc.c informixdbproc.ec makefile
[Sunrier@localhost Informix]$ ./demo
iRetCode = 0
Informix Connection success !
[Sunrier@localhost Informix]$
Oracle下
//oracledbproc.pc
/************************************************************* FileName : oracledbproc.pc FileFunc : C语言接口访问Oracle数据库 Version : V0.1 Author : Sunrier Date : 2012-07-27 Descp : Linux下使用C语言访问Oracle函数 *************************************************************/ #include <stdio.h> #include <string.h> EXEC SQL include sqlca; int check_error(char szMessage[]) { if( sqlca.sqlcode ) { printf("Check error report : \n"); printf("Error occured : %s ,sqlcode = [%d] \n",szMessage,sqlca.sqlcode); return 1; } return 0; } int oracle_login(char *pUserName,char *pPassword,char *pDataBase) { int iRetCode = 1; EXEC SQL BEGIN DECLARE SECTION; char szUserName[30]; char szPassword[30]; char szDataBase[30]; EXEC SQL END DECLARE SECTION; memset(szUserName,0,sizeof(szUserName)); memset(szPassword,0,sizeof(szPassword)); memset(szDataBase,0,sizeof(szDataBase)); strcpy(szUserName,pUserName); strcpy(szPassword,pPassword); strcpy(szDataBase,pDataBase); EXEC SQL CONNECT :szUserName identified by :szPassword using :szDataBase; iRetCode = check_error("CONNECT TO DATABASE"); return iRetCode; } int oracle_logout( void ) { int iRetCode = 1; EXEC SQL commit work release; iRetCode = check_error("DisConnect TO DATABASE"); return iRetCode; }
//demo.c
/************************************************************* FileName : demo.c FileFunc : 测试登陆和关闭本地Oracle数据库 Version : V0.1 Author : Sunrier Date : 2012-07-27 Descp : Linux下使用C语言访问Oracle函数 *************************************************************/ #include <stdio.h> int main(int argc,char *argv[]) { int iRetCode = 0; char szUserName[20] = "Sunrier"; char szPassword[20] = "redhat"; char szDatabase[20] = "test"; iRetCode = oracle_login(szUserName,szPassword,szDatabase); printf("iRetCode = %d \n",iRetCode); if( iRetCode ) { printf("Oracle Connection failure !\n"); return iRetCode; } else { printf("Oracle Connection success !\n"); } iRetCode = oracle_logout(); return iRetCode; }
//makefile
#makefile OBJS = demo all:$(OBJS) PATH=$PATH:$HOME/bin:$MAINPATH/sbin:$MAINPATH/sql export PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/runlib export LD_LIBRARY_PATH export LANG=zh_CN.gb2312 export LANG=zh_cn.gbk #oracle setting export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK TMP=/tmp; export TMP TMPDIR=$TMP; export TMPDIR ORACLE_BASE=/home/oracle; export ORACLE_BASE ORACLE_HOME=$ORACLE_BASE/product/10.2/db_1; export ORACLE_HOME ORACLE_SID=Sunrier; export ORACLE_SID ORACLE_TERM=xterm; export ORACLE_TERM PATH=/usr/sbin:$PATH; export PATH PATH=$ORACLE_HOME/bin:$PATH; export PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib; export CLASSPATH CFLAGS = -O -w -ansi #CFLAGS = -O -Wall -ansi CC = gcc $(CFLAGS) #Set system path #运行动态库路径 RUNLIBPATH=$(HOME)/runlib #主路径 MAINPATH=$(HOME)/Proj #库头文件 SYSINCLPATH=$(MAINPATH)/include #执行文件 BINPATH=$(HOME)/bin #源程序路径 SRCPATH=$(MAINPATH)/src APPINCLPATH=$(SRCPATH)/include #PROCFLAGS= \ # include=${ORACLE_HOME}/rdbms/public \ # include=$(SYSINCLPATH) \ # include=$(APPINCLPATH) \ # parse=full \ # unsafe_null=yes \ # dbms=V8 \ # mode=oracle \ # lines=yes \ # char_map=string #proc用来对Oracle源程序.pc进行预编译,生成单纯的C源程序 #PROC= proc $(PROCFLAGS) PROC= proc ORALIB=-L$(ORACLE_HOME)/lib -lclntsh demo:demo.c oracledbproc.pc @echo -e "Start compile Oracle file ......" @$(PROC) PARSE=NONE oracledbproc.pc @$(CC) -o $@ demo.c oracledbproc.c $(ORALIB) # @$(CC) -o $@ demo.c oracledbproc.c $(ORACLE_HOME)/lib/libclntsh.so clean: @rm -rf oracledbproc.c @ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ | grep -v [.]pc$$ | xargs rm -rf #makefile
[Sunrier@localhost Oracle]$ ls
demo.c makefile oracledbproc.pc
[Sunrier@localhost Oracle]$ make
Start compile Oracle file ......
Pro*C/C++: Release 10.2.0.5.0 - Production on Fri Jul 27 16:31:30 2012
Copyright (c) 1982, 2007, Oracle. All rights reserved.
System default option values taken from: /home/oracle/product/10.2/db_1/precomp/admin/pcscfg.cfg
[Sunrier@localhost Oracle]$ ls
demo demo.c makefile oracledbproc.c oracledbproc.lis oracledbproc.pc
[Sunrier@localhost Oracle]$ ./demo
iRetCode = 0
Oracle Connection success !
[Sunrier@localhost Oracle]$