首页 > 数据库 > DB2 > 正文

用Cygwin模拟DB2的Unix/Linux开发环境(2)

2020-03-09 22:30:15
字体:
来源:转载
供稿:网友
执行初始化了db2环境的cygwin  点击桌面上的cygwin图标,可以进入cygwin模拟的linux..   现在,该环境不能执行db2命令,因为没有作db2cmd初始化环境。     为了能在该环境中使用db2,必须先运行db2cmd然后在db2的命令行环境下进入cygwin测试,是否能在cygwin环境下使用db2命令。ok,到目前为止,我们已经拥有了一个和unix一样的环境,并且能使用db2下一步我们来写第一个sqc程序. 编写sqc程序   编写一个简单程序测试,该程序主要完成,读取系统时间,并打印。程序主要部分为:           if (connectdatabase(sdbname,susername,spasswd)<0)   /*连接数据库*/      {             printf("连接数据库失败/n");             return -1;      }      printf("连接数据库成功!/n");                                                        exec sql select char(current timestamp)               into :sdatetime               from (values 1) as a;              if dataerror       {              disconnectdb();              return -1;       }       printf("当前时间%s/n",sdatetime);       disconnectdb(); (完整程序建附件)该程序在unix主机下能编译执行。前面说过,建立这个环境的主要意义在于方便代码移植。所以,代码本身不用作任何更改即可在cygwin环境下编译。修改编译参数       安装过程中已经说明cygwin环境下,支持大部分unix/linux命令并且安装了gcc的编译器,windows平台和unix平台下的库略有不同,gcc和我们在windows下常用的vc编译器参数也略有不同,下面简要说明。1、  在unix环境中,分为静态库和动态库,它们的扩展名分别是 .a 和 .so .2、  在windows中,静态库扩展名为 .lib 动态库扩展名为 .dll3、  unix下,sqc程序编译时必须链接 libdb2.so库,也就是加上 -ldb2参数(忽略lib和扩展名,这是unix下c编译器特点)4、  windows下,sqc程序必须链接db2api.lib静态库。虽然有上述不同,但是我们的修改却非常少,本例中使用了以前我为编译sqc写的makefile模板。在makefile中真正需要修改的只有一行将libs= -l$(db2path)/lib -l$(db2path)/lib/db2修改为libs= -l $(db2path)/lib $(db2path)/lib/db2api.lib当然,你还需要更具环境的不同,修改makefile的其他部分,比如db2path的值啊,这些就是在不同的主机上也需要修改的,并不是windows和unix的区别,不在本文的讨论之列。 运行测试程序cygwin使用windows文件系统,进入cygwin环境后 系统的根目录/其实就是你的cygwin安装目录。用户目录通常在/home/user,(user是你windows的用户名)  比如在windows环境中我的cygwin安装在d:/cygwin 我的用户目录是 d:/cygwin/home/rocfu,,如果你在cygwin中使用pwd命令察看当前路径,会显示/home/roc. 在用户目录下新建db2test子目录,附件附带文件 makefile  test.sqc,将这三个文件复制到db2test目录.修改 test.sqc中的下面代码       strcpy(susername,"db2admin");      strcpy(spasswd,"db2admin");      strcpy(sdbname,"dwctrldb");为你自己的服务器连接(为简化测试程序,这里并没有从配置文件中读取连接信息)执行命令 make all你会发现编译的结果竟然是一个exe文件,对了,这是在windows下编译的程序,当然是一个exe文件了。ok 执行该程序,运行结果如下图总结       完成上述步骤之后,我们有了一个仿真的unix环境,能通过makefile中的小小改动,将源代码在各种平台上编译,当你不能连上主机工作时,完全可以用这个小巧的环境暂时代用。这个环境具有unix/linux高级特性,对于信号、管道、多进程、守护进程完全支持。    还有更重要的,如果希望这个程序在windows下脱离cygwin环境运行,只要把cygwin1.dll复制到windows的system32目录下即可,这样,你得程序在一套源码的情况下,支持两个环境,何乐而不为。事实上,很多从linux移植到windows的程序就是这么干的。 附件makefile.suffixes: .sqc .c .o gcc=gcccc=gcc  embprep=embprep  ccflags=-g -mno-cygwin   cflags= $(extra_cflags) -i$(db2path)/include -mno-cygwin db2path=/cygdrive/d/db2/sqllib#libs= -l$(db2path)/lib -l$(db2path)/lib/db2 libs= -l $(db2path)/lib $(db2path)/lib/db2api.lib uid=db2adminpwd=db2admindb=dwctrldb billhome=.billbin=.billsrc=$(billhome)billobj=$(billhome)billlib=../lib include=-i. -i$(billhome)/src -i$(billlib) /       -i$(db2path)/include /       -i/usr/lib -i/usr/local/include /       -i/usr/include               target1 = $(billbin)/test  all:$(target1) .sqc.o:       db2 connect to $(db) user $(uid) using $(pwd);/       db2 prep $*.sqc bindfile;/       db2 bind $*.bnd;/       db2 connect reset;/       db2 terminate;/       $(cc) $(include) -o $*.o -c $(ccflags) $(cflags) $(defs)  $*.c ; .c.o:       $(cc) $ (include) -o $*.o -c $(ccflags) $(cflags) $(libs) $(defs) $*.c --def tandard.def        pick_objs1= $(billsrc)/test.o $(billbin)/test:    $(comm_objs) $(pick_objs1)              $(cc) $(cflags) -o $(target1)  $(pick_objs1) $(comm_objs) $(oracle_lib)$(linkflag) $(libs)   clean:       rm -f *.o $(target1) test.c test.o test.bnd test.sqc/**********************************************************************************                 文件名:test.sqc    创建人: roc.fu   日期  2004-03-07  版 本:v1.0         功 能:读取系统当前时间                                             描 述:**********************************************************************************/#include <stdio.h>#include <sql.h>#include <sqlenv.h>#include <sqlda.h>#include <sqlca.h>#include <sqladef.h>#include <sqlenv.h> struct sqlca sqlca;char gusername[20];char gpassword[20];char gservername[20];char gtpassword[20]; #ifndef dataerror#define dataerror   (sqlca.sqlcode<0 )#endif   /************************************************************************************     功能 :连接数据库   *        返回值 :0 正常连接  -1 连接失败*        参数: sdbalias 数据库名*              suser    用户名*              spasswd  密码***********************************************************************************/int connectdatabase (char *sdbalias,char *suser,char *spasswd){    int        rc = 0;    char       smsg[1024];         exec sql begin declare section ;        char db[15] ;        char userid[15] ;         char passwd[15] ;     exec sql end declare section;     memset(smsg,0,1024);    printf("1/n");    strcpy( db, sdbalias) ;    strcpy( userid, suser) ;        strcpy( passwd, spasswd) ;        if ( strlen(userid) == 0)    {        printf("2/n");     exec sql connect to :db;     }    else     {              printf("用户名:%s/n",userid);           printf("密码:%s/n",passwd);           printf("密码:%s/n",db);                      exec sql connect to :db user :userid using :passwd;         printf("4/n");     }        return 0; } /************************************************************************************                   断开数据库连接************************************************************************************/void disconnectdb(){       exec sql connect reset;}  int main(int argc, char *argv[]) {      int iret;                exec sql begin declare section;            char susername[100];          char spasswd[100];          char sdbname[100];          char sdatetime[100];        exec sql end declare section;              printf("start read config.../n");       strcpy(susername,"db2admin");      strcpy(spasswd,"db2admin");      strcpy(sdbname,"dwctrldb");              if (connectdatabase(sdbname,susername,spasswd)<0)   /*连接数据库*/      {             printf("连接数据库失败/n");             return -1;      }      printf("连接数据库成功!/n");                                                        exec sql select char(current timestamp)               into :sdatetime               from (values 1) as a;              if dataerror       {              disconnectdb();              return -1;       }       printf("当前时间%s/n",sdatetime);       disconnectdb();       printf("完成/n");       return 0;} 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表