Linux 에서 Makefile 에서 매크로 를 미리 정의 하여 서로 다른 데이터 베 이 스 를 처리 합 니 다.


    이 문장 을 쓰 는 것 은 앞에서 내 가 총 결 한 이 소결 이다.http://blog.csdn.net/sunrier/article/details/7664515 이 끌 어 낸 아 이 디 어 는 주로 Makefile 파일 에서 두 가지 서로 다른 데이터 베 이 스 를 미리 정의 하여 처리 하 는 것 을 실현 합 니 다.
 
//mysqldbproc.c
/*************************************************************       
    FileName : mysqldbproc.c   
    FileFunc : C      MySQL      
    Version  : V0.1       
    Author   : Sunrier       
    Date     : 2012-06-25 
    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 ! 
"); return EXIT_FAILURE; } iRetCode = mysql_options(&s_my_connection,MYSQL_OPT_CONNECT_TIMEOUT,(const char *)&uiTimeOut); if( iRetCode ) { fprintf(stderr,"Connection is timeout!
"); 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!
"); s_iDbconnected = 1; } else { fprintf(stderr,"Connection failed!
"); if( mysql_errno(&s_my_connection) ) { fprintf(stderr,"Connection error %d: %s!
",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 ; }

 
 
//db2dbproc.sqc
/*************************************************************       
    FileName : db2dbproc.sqc   
    FileFunc : C      DB2        
    Version  : V0.1       
    Author   : Sunrier       
    Date     : 2012-06-25 
    Descp    : Linux   C    DB2    
*************************************************************/
#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 : 
"); printf("Error occured : %s ,sqlcode = [%d]
",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 !
"); 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 :        MySQL DB2        
    Version  : V0.1       
    Author   : Sunrier       
    Date     : 2012-06-25  
    Descp    : Linux   C                    
*************************************************************/ 
#include <stdio.h>
 
#ifdef MYSQL 

int main(int argc,char *argv[])
{
	int iRetCode = 0;
	char szServer[20] = "localhost";  
	/*char szServer[20] = "127.0.0.1";*/
  	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 
",iRetCode); mysql_logout(); return 0; } #else 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
",iRetCode); if( iRetCode ) { printf("DB2 Connection failure !
"); } else { printf("DB2 Connection success !
"); } db2_logout(); return 0; } #endif

 
 
//makefile
#makefile
OBJS = demo   
all:$(OBJS)  
CFLAGS = -O -w -ansi     
#CFLAGS = -O -Wall -ansi 

#       
DATABASETYPE = -D MYSQL  
#DATABASETYPE = 

CC = gcc $(CFLAGS) 

#     
LIBPATH = -L/usr/lib

MYSQLINCPATH = -I/usr/include/mysql  
MYSQLLIBPATH = -L/usr/lib/mysql  
MYSQLLIB = -lmysqlclient -lz -lm

DB2LIBPATH = -L/app/db2inst1/sqllib/lib
DB2LIB = -ldb2 -lm
 
ifneq ($(DATABASETYPE),)
demo:demo.c mysqldbproc.c
	@echo -e "Start compile MySQL file ...... "
	@$(CC) $(MYSQLINCPATH) -c mysqldbproc.c
	@$(CC) $(DATABASETYPE) $(MYSQLINCPATH) $(MYSQLLIBPATH)  -o demo demo.c mysqldbproc.o $(MYSQLLIB)
#	@$(CC) $(DATABASETYPE) $(MYSQLINCPATH) $(MYSQLLIBPATH) -o $@ $? $(MYSQLLIB)
else
demo:demo.c db2dbproc.sqc
	@echo -e "Start compile DB2 file ...... "
	@db2 connect to test 
	@db2 prep db2dbproc.sqc  
	@$(CC) -o $@ demo.c db2dbproc.c $(DB2LIB)
	@rm -rf db2dbproc.c
#	@$(CC) -o $@ $? $(DB2LIB) 
#	gcc -ldb2 -L/usr/lib -lm -o demo demo.c db2dbproc.c   
#	@gcc -L/app/db2inst1/sqllib/lib -ldb2 -L/usr/lib -lm -o demo db2dbproc.c
endif
clean:
	@ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ | grep -v [.]sqc$$ | grep -v [.]txt$$ | xargs rm -rf



 
 
MYSQL 데이터 베 이 스 를 정의 한 상태 에서 실행:
[Sunrier@localhost DB]$ make clean [Sunrier@localhost DB]$ ls db2dbproc.sqc  mysqldbproc.c  demo.c  makefile [Sunrier@localhost DB]$ make Start compile MySQL file ...... [Sunrier@localhost DB]$ ls db2dbproc.sqc  mysqldbproc.c  mysqldbproc.o  demo  demo.c  makefile [Sunrier@localhost DB]$ ./demo MySQL Connection success! iRetCode = 0 [Sunrier@localhost DB]$
 
MYSQL 데이터 베 이 스 를 정의 하지 않 은 상태 에서 DB2 데이터 베 이 스 를 실행 합 니 다.
[Sunrier@localhost DB]$ make clean [Sunrier@localhost DB]$ ls db2dbproc.sqc  mysqldbproc.c  demo.c  makefile [Sunrier@localhost DB]$ make
Start compile DB2 file ......
     Database Connection Information
 Database server        = DB2/LINUX 8.2.3  SQL authorization ID   = CPICDB2  Local database alias   = TEST
LINE    MESSAGES FOR db2dbproc.sqc ------  --------------------------------------------------------------------         SQL0060W  The "C" precompiler is in progress.         SQL0091W  Precompilation or binding was ended with "0"                   errors and "0" warnings. [Sunrier@localhost DB]$ ls db2dbproc.sqc  mysqldbproc.c  demo  demo.c  makefile [Sunrier@localhost DB]$ ./demo iRetCode = 0 DB2 Connection success ! [Sunrier@localhost DB]$
 
 

좋은 웹페이지 즐겨찾기