Performed Cygwin in initializing the DB2 environment
Click on the Cygwin icon on your desktop to enter the Linux of Cygwin simulation..
Now, the environment cannot perform DB2 commands because there is no DB2CMD initialization environment.
In order to use DB2 in this environment, you must first run DB2CMD.
Then enter Cygwin in the command line environment of DB2
Test, whether you can use the DB2 command in a Cygwin environment.
OK, so far, we already have a environment like UNIX, and can use DB2
Next, let's write the first SQC program.
Write a SQC program
Write a simple program test, the program is mainly completed, read system time, and print.
The main part of the program is:
IF (ConnectDatabase (SDBNAME, SUSERNAME, SPASSWD) <0) / * Connection Database * /
{
Printf ("Connect Database Failed / N");
Return -1;
}
Printf ("Connect Database! / N");
EXEC SQL SELECT Char (Current TimeStamp)
INTO: SDATETIME
From (VALUES 1) AS A;
IF dataerror
{
DisconnectDB ();
Return -1;
}
Printf ("Current time% S / N", SDATETIME);
DisconnectDb ();
(Complete Program Construction Accessories)
This program can compile execution under the UNIX host.
As mentioned earlier, the main significance of establishing this environment is to facilitate code transplantation. So, the code itself is not used as any changes to compile in the Cygwin environment.
Modify compilation parameters
In the installation process, the Cygwin environment has been described, supporting most UNIX / Linux commands and installing the GCC compiler, the Windows platform, and the library of the GCC have different, GCC and our VC compiler parameters that we use under Windows are also slightly Different, the following is a brief description.
1,
In the UNIX environment, divided into static libraries and dynamic libraries, their extensions are .a and .so, respectively.
2,
In Windows, the static library expansion is called .lib dynamic library extension is .dll
3,
Under UNIX, the libdb2.so library must be linked when compiled, which is plus -ldb2 parameter (ignoring the lib and extension, this is the characteristics of UNIX C compiler)
4,
Under Windows, the SQC program must link DB2API.LIB static library.
Although there is the above difference, our modification is very small. In this example, I used the Makefile Template written to compile SQC. Only one line really needs to be modified in Makefile
will
Libs = -l $ (db2path) / lib -l $ (db2path) / lib / db2
change into
Libs = -l $ (db2path) / lib $ (db2path) /LIB/db2api.lib
Of course, you also need more environmentally different, modify the other parts of makefile, such as DB2PATH, which is also modified on different hosts, not the difference between Windows and UNIX, not in this article discussion.
Run the test program
Cygwin uses the Windows file system, the root directory of the system after entering the Cygwin environment, is actually your Cygwin installation directory. User directory is usually in / home / user, (User is your username)
For example, in the Windows environment I installed in d: / cygwin my user directory is d: / cygwin / home / rocfu, if you use the PWD command to use the PWD command in Cygwin, you will display / home / ROC.
In the user directory, create file Makefile Test.sqc, copy these three files to the DB2Test directory. Modify the following code in Test.sqc
STRCPY (SUSERNAME, "DB2ADMIN");
STRCPY (Spasswd, "DB2Admin");
STRCPY (SDBNAME, "DWCTRLDB");
Connect your own server (for simplifying test programs, there is no connection information from the configuration file)
Execute Make All
You will find the result of the compilation is an exe file, yes, this is a program compiled under Windows, of course, an exe file.
OK executes the program, the result is as follows
to sum up
After completing the above steps, we have a simulated UNIX environment that can be compiled on various platforms through small changes in makefile, and you can use this compact environment to temporarily use this compact environment when you don't connect to the host. Substitute. This environment has UNIX / Linux advanced features, for signal, pipeline, multi-process, and daemon full support.
Also more important, if you want this program to run out of the Cygwin environment under Windows, just copy Cygwin1.dll to the system32 directory of Windows, this, you have to support two environments in a source code Who is not. In fact, many programs that transplant to Windows from Linux are doing.
annex
Makefile
.Sqfixes: .sqc .c .o
GCC = GCC
CC = GCC
EMBPREP = EMBPREP
Ccflags = -g-malo-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 = db2admin
PWD = DB2Admin
DB = dwctrldb
Billhome =.
Billbin =.
BILLSRC = $ (Billhome)
Billobj = $ (Billhome)
Billlib = .. / lib
INCLUDE = -i. -I $ (BillHome) / src -i $ (Billlib) /
-I $ (db2path) / include / include /
-I / usr / lib -i / usr / local / incrude /
-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) $ (DEFS) $ *. C --def /standard.def
Pick_Objs1 = $ (BILLSRC) /TEST.O
$ (BILLBIN) / TEST: $ (COMM_OBJS) $ (Pick_OBJS1) $ (CFLAGS) -O $ (Pick_OBJS1) $ (Comm_OBJS) $ (LINKFLAG) $ (LIBS)
Clean:
RM -F * .O $ (target1) Test.c test.o test.bnd
Test.sqc
/ ************************************************** ***************************************
File Name: Test.sqc Created: ROC.FU Date 2004-03-07
Version: v1.0
Function: Read the system current time
Description:
*********************************************************** ****************************** /
#include
#include
#include
#include
#include
#include
#include
Struct Sqlca SQLCA;
Char gusername [20];
Char gpassword [20];
Char GserveName [20];
Char gtpassword [20];
#ifndef Datarror
#define dataerror (Sqlca.sqlcode <0)
#ENDIF
/ ************************************************** ***************************************
*
* Function: Connect the database
* Return value: 0 Normal connection-1 connection failed
* Parameters: SDBALIAS Database Name
* SUSER username
* SPASSWD password
*********************************************************** ******************************* /
INT ConnectDatabase (Char * SDBALIAS, Char * SUSER, CHAR * SPASWD)
{
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 ("User Name:% S / N", UserID;
Printf ("Password:% S / N", Passwd;
Printf ("Password:% S / N", DB);
Exec SQL Connect To: DB User: Userid Using: Passwd; Printf ("4 / N");
}
Return 0;
}
/ ************************************************** ****************************************
* Disconnect the database connection
*********************************************************** ***************************************** /
Void disconnectdb ()
{
EXEC SQL Connect Reset;
}
Int main (int Argc, char * argv [])
{
Irge;
EXEC SQL Begin Declare Section;
Char susename [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) / * Connection Database * /
{
Printf ("Connect Database Failed / N");
Return -1;
}
Printf ("Connect Database! / N");
EXEC SQL SELECT Char (Current TimeStamp)
INTO: SDATETIME
From (VALUES 1) AS A;
IF dataerror
{
DisconnectDb ();
Return -1;
}
Printf ("Current time% S / N", SDATETIME);
DisconnectDb ();
Printf ("Completed / N");
Return 0;
}