1 /**************************************************************************
2 * Copyright (C) 2008 by www.databasecache.com *
3 * Contact : praba_tuty@databasecache.com *
5 * THis program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as Published by *
7 * the Fre Software Foundation;either version 2 of the License, or *
8 * (at your option)any later version. *
10 * This program is distributed in the hope that will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, See the *
13 * GNU General License for more details. *
15 **************************************************************************/
23 void checkrc(int rc
,int line
)
27 printf("Error %d at line %d\n", rc
, line
);
39 SQLSMALLINT outstrlen
;
41 //Allocate an environment handle
42 ret
= SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
43 checkrc(ret
,__LINE__
);
45 //we need ODBC3 support
46 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
48 //Allocate a Connection handle
49 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
50 checkrc(ret
,__LINE__
);
52 //connect to the DSN Data source
53 ret
= SQLConnect (dbc
,
54 (SQLCHAR
*) "test", (SQLSMALLINT
) strlen ("test"),
56 (SQLSMALLINT
) strlen ("root"),
57 (SQLCHAR
*) "manager",
58 (SQLSMALLINT
) strlen (""));
59 if(SQL_SUCCEEDED(ret
))
61 printf("Connected to CSQL\n");
65 printf("error in connection\n");
66 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
67 checkrc(ret
,__LINE__
);
68 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
69 checkrc(ret
,__LINE__
);
73 // Allocation of statement handle for DDL nad DML Operation
74 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
75 checkrc(ret
,__LINE__
);
77 // create table 'T1' with two fields, F1 INTEGER AND F2 CHAR.
78 SQLCHAR table
[200]= "CREATE TABLE T1(F1 INT,F2 CHAR(20))";
80 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
81 checkrc(ret
,__LINE__
);
83 ret
= SQLExecute(stmt
);
84 checkrc(ret
,__LINE__
);
86 printf("Table T1 created\n");
88 //Insert 10 Tuples into the table 'T1'
90 char name
[20]="THIRU";
91 char names
[10][20]={"Gopal", "Aruna", "Kanchana", "Vijay", "Ganga",
92 "XieLiang", "Rajesh", "Steve", "Veda", "Jitendra" };
95 SQLINTEGER slen
= SQL_NTS
;
97 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(?,?);",SQL_NTS
);
98 checkrc(ret
,__LINE__
);
100 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&id
,0,NULL
);
101 checkrc(ret
,__LINE__
);
103 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_CHAR
,SQL_CHAR
,196,0,(void*)name
,0,&slen
);
104 checkrc(ret
,__LINE__
);
110 strcpy(name
,names
[i
]);
112 ret
= SQLExecute(stmt
);
113 checkrc(ret
,__LINE__
);
115 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
116 checkrc(ret
,__LINE__
);
119 printf("%d Rows inserted\n",count
);
122 //Fetch rows from the table 'T1'
125 ret
= SQLPrepare(stmt
,(unsigned char*)"SELECT * FROM T1;",SQL_NTS
);
126 checkrc(ret
,__LINE__
);
128 ret
= SQLBindCol(stmt
,1,SQL_C_SLONG
,&id1
,0,NULL
);
129 checkrc(ret
,__LINE__
);
131 ret
= SQLBindCol(stmt
,2,SQL_C_CHAR
,name
,sizeof(name
),NULL
);
132 checkrc(ret
,__LINE__
);
135 ret
= SQLExecute(stmt
);
136 checkrc(ret
,__LINE__
);
138 printf("Fetching starts on table T1 :\n");
139 while(SQL_SUCCEEDED(ret
=SQLFetch(stmt
)))
141 printf("F1:%d\tF2:%s\n",id1
,name
);
144 ret
= SQLCloseCursor(stmt
);
145 checkrc(ret
,__LINE__
);
147 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
148 checkrc(ret
,__LINE__
);
150 printf("%d rows fetched\n",count
);
152 //Delete all the rows from the table 'T1'
153 ret
= SQLPrepare(stmt
,(unsigned char*)"DELETE FROM T1 WHERE F1=?;",SQL_NTS
);
154 checkrc(ret
,__LINE__
);
156 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&id1
,0,NULL
);
157 checkrc(ret
,__LINE__
);
164 ret
= SQLExecute(stmt
);
165 checkrc(ret
,__LINE__
);
169 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
170 checkrc(ret
,__LINE__
);
171 printf("%d Rows deleted\n",count
);
174 // drop the table 'T1'
175 SQLCHAR drop
[50]="DROP TABLE T1";
176 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
177 checkrc(ret
,__LINE__
);
179 ret
= SQLExecute(stmt
);
180 checkrc(ret
,__LINE__
);
181 printf("Table T1 dropped\n");
183 //Free the statement handle
184 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
185 checkrc(ret
,__LINE__
);
187 //Disconnect from the Data source
188 ret
= SQLDisconnect(dbc
);
189 checkrc(ret
,__LINE__
);
190 printf("Disconnected from CSQL\n");
192 //Free the connection handle
193 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
194 checkrc(ret
,__LINE__
);
196 //Free the environment handle
197 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
198 checkrc(ret
,__LINE__
);