1 // Use Connection String "DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;" for Connect Data Source
2 // CREATE TABLE "T1" WITH 10 FIELDS.
3 // INSERT SOME TUPLES IN IT. AND FETCH ALL THE TUPLES.
5 // Author: jitendra Lenka
16 //*************************************************************************
17 // ERROR CHECK FUNCTION
18 inline void checkrc(int rc
,int line
)
22 printf("ERROR %d at line %d\n",rc
,line
);
27 //*************************************************************************
28 // FUNCTION FOR INSERTING ROWS IN IT.
29 int InsertTest(SQLHANDLE env
,SQLHANDLE dbc
,SQLHANDLE stmt
)
33 int f1
=50; // f1 field
34 short int f2
=0;//f2 field
39 long long f10
= 15000;
42 SQLINTEGER slen
= SQL_NTS
;
43 //***********************************
44 // STRUCTURE FIOR DATE DATATYPE
51 // strcpy(strDate,"{d '2008-03-18'}");
52 //*******************************
53 // STRUCTURE FOR TIME DATATYPE.
58 //*****************************
59 // STRUCTURE FOR TIMESTAMP DATATYPE
60 SQL_TIMESTAMP_STRUCT timestamp
;
61 timestamp
.year
= 2007;
65 timestamp
.minute
= 22;
66 timestamp
.second
= 10;
68 //******************************
69 // PREPARE THE STATEMENT.
70 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(?,?,?,?,?,?,?,?,?,?)",SQL_NTS
);
71 checkrc(ret
,__LINE__
);
73 // BIND PARAMETER FOR ALL THE FIELD
75 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&f1
,0,NULL
);
76 checkrc(ret
,__LINE__
);
78 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_SSHORT
,SQL_SMALLINT
,0,0,&f2
,0,NULL
);
79 checkrc(ret
,__LINE__
);
81 ret
= SQLBindParameter(stmt
,3,SQL_PARAM_INPUT
,SQL_C_CHAR
,SQL_CHAR
,196,0,(void*)f3
,0,&slen
);
82 checkrc(ret
,__LINE__
);
84 ret
= SQLBindParameter(stmt
,4,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_FLOAT
,0,0,&f4
,0,NULL
);
85 checkrc(ret
,__LINE__
);
87 ret
= SQLBindParameter(stmt
,5,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_REAL
,0,0,&f5
,0,NULL
);
88 checkrc(ret
,__LINE__
);
90 ret
= SQLBindParameter(stmt
,6,SQL_PARAM_INPUT
,SQL_C_TYPE_DATE
,SQL_TYPE_DATE
,196,0,&date
,sizeof(date
),&slen
);
91 checkrc(ret
,__LINE__
);
93 ret
= SQLBindParameter(stmt
,7,SQL_PARAM_INPUT
,SQL_C_TYPE_TIME
,SQL_TYPE_TIME
,196,0,&time
,sizeof(time
),&slen
);
94 checkrc(ret
,__LINE__
);
96 ret
= SQLBindParameter(stmt
,8,SQL_PARAM_INPUT
,SQL_C_TYPE_TIMESTAMP
,SQL_TYPE_TIMESTAMP
,196,0,×tamp
,sizeof(timestamp
),&slen
);
97 checkrc(ret
,__LINE__
);
99 ret
= SQLBindParameter(stmt
,9,SQL_PARAM_INPUT
,SQL_C_TINYINT
,SQL_TINYINT
,0,0,&f9
,0,NULL
);
100 checkrc(ret
,__LINE__
);
102 ret
= SQLBindParameter(stmt
,10,SQL_PARAM_INPUT
,SQL_C_SBIGINT
,SQL_BIGINT
,0,0,&f10
,0,NULL
);
103 checkrc(ret
,__LINE__
);
106 // EXECUTE THE STATEMENT
112 // date.month = date.month+1;
113 date
.day
= date
.day
+1;
114 ret
= SQLExecute(stmt
);
115 checkrc(ret
,__LINE__
);
116 date
.month
= date
.month
+1;
118 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
119 checkrc(ret
,__LINE__
);
122 printf("Total row inserted=%d\n",count
);
125 //***********************************************************************
126 // FETCH ROWS FROM THE TABLE "T1"......select * from T1;
127 int FetchTest(SQLHANDLE env
, SQLHANDLE dbc
, SQLHANDLE stmt
)
131 int f1
=10; // f1 field
132 short int f2
=20;//f2 field
133 char f3
[50]= "jitendra";
137 long long f10
= 15000;
141 SQL_DATE_STRUCT date
;
147 // strcpy(strDate,"{d '2008-03-18'}");
148 //**********************************************
149 SQL_TIME_STRUCT time
;
153 //*********************************************
154 SQL_TIMESTAMP_STRUCT timestamp
;
155 timestamp
.year
= 2007;
156 timestamp
.month
= 03;
159 timestamp
.minute
= 22;
160 timestamp
.second
= 10;
163 ret
= SQLPrepare(stmt
,(unsigned char*)"SELECT * FROM T1",SQL_NTS
);
164 checkrc(ret
,__LINE__
);
166 //ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SMALL,SQL_SMALL,0,0,)
167 ret
= SQLBindCol(stmt
,1,SQL_C_SLONG
,&f1
,0,NULL
);
168 checkrc(ret
,__LINE__
);
170 ret
= SQLBindCol(stmt
,2,SQL_C_SSHORT
,&f2
,0,NULL
);
171 checkrc(ret
,__LINE__
);
174 ret
= SQLBindCol(stmt
,3,SQL_C_CHAR
,f3
,sizeof(f3
),NULL
);
175 checkrc(ret
,__LINE__
);
177 ret
= SQLBindCol(stmt
,4,SQL_C_FLOAT
,&f4
,0,NULL
);
178 checkrc(ret
,__LINE__
);
180 ret
= SQLBindCol(stmt
,5,SQL_C_FLOAT
,&f5
,0,NULL
);
181 checkrc(ret
,__LINE__
);
183 ret
= SQLBindCol(stmt
,6,SQL_C_TYPE_DATE
,&date
,sizeof(date
),NULL
);
184 checkrc(ret
,__LINE__
);
186 ret
= SQLBindCol(stmt
,7,SQL_C_TYPE_TIME
,&time
,sizeof(time
),NULL
);
187 checkrc(ret
,__LINE__
);
189 ret
= SQLBindCol(stmt
,8,SQL_C_TYPE_TIMESTAMP
,×tamp
,sizeof(timestamp
),NULL
);
190 checkrc(ret
,__LINE__
);
192 ret
= SQLBindCol(stmt
,9,SQL_C_TINYINT
,&f9
,0,NULL
);
193 checkrc(ret
,__LINE__
);
195 ret
= SQLBindCol(stmt
,10,SQL_C_SBIGINT
,&f10
,0,NULL
);
196 checkrc(ret
,__LINE__
);
199 ret
= SQLExecute(stmt
);
200 checkrc(ret
,__LINE__
);
201 printf("Fetching starts on table T1 :\n");
203 while(SQL_SUCCEEDED(ret
= SQLFetch(stmt
)))
205 printf("F1=%d\tF2=%d\tF3=%s\tF4=%f\tF5=%f\tDATE=%d/%d/%d\tTIME=%d-%d-%d\tTIMESTAMP=%d/%d/%d %d-%d-%d\tF9=%d\tF10=%lld\n ",f1
,f2
,f3
,f4
,f5
,date
.year
,date
.month
,date
.day
,time
.hour
,time
.minute
,time
.second
,timestamp
.year
,timestamp
.month
,timestamp
.day
,timestamp
.hour
,timestamp
.minute
,timestamp
.second
,f9
,f10
);
214 ret
= SQLCloseCursor(stmt
);
215 checkrc(ret
,__LINE__
);
217 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
218 checkrc(ret
,__LINE__
);
220 printf("Total row fetched=%d\n",count
);
231 SQLCHAR outstr
[1024];
232 SQLSMALLINT outstrlen
;
234 // Aloocate an environment handle
235 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
236 checkrc(ret
,__LINE__
);
238 //we need odbc3 support
239 ret
= SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
240 checkrc(ret
,__LINE__
);
241 //ALLOCATE A Connection handle
242 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
243 checkrc(ret
,__LINE__
);
245 // connect to the DSN mydsn
246 ret
= SQLConnect (dbc
,
247 (SQLCHAR
*) "DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT
) strlen ("DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;"),
249 (SQLSMALLINT
) strlen ("root"),
250 (SQLCHAR
*) "manager",
251 (SQLSMALLINT
) strlen (""));
253 if(SQL_SUCCEEDED(ret
))
255 printf("\nConnected to the Data Source SUCCESSFULLY..\n");
261 printf("error in connection\n");
264 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
265 checkrc(ret
,__LINE__
);
267 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
268 checkrc(ret
,__LINE__
);
274 //******************************************************************
276 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
277 checkrc(ret
,__LINE__
);
280 "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT)";
281 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
282 checkrc(ret
,__LINE__
);
283 ret
= SQLExecute(stmt
);
284 checkrc(ret
,__LINE__
);
285 printf("\nTABLE CREATED\n");
286 //*****************************
287 InsertTest(env
,dbc
,stmt
);
288 FetchTest(env
,dbc
,stmt
);
290 //****************************************************************
291 SQLCHAR drop
[100]="DROP TABLE T1";
292 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
293 checkrc(ret
,__LINE__
);
294 ret
= SQLExecute(stmt
);
295 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
297 printf("Statement failed\n");
301 printf("Table 'T1' dropped successfully\n");
303 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
304 checkrc(ret
,__LINE__
);
306 ret
= SQLDisconnect(dbc
);
307 checkrc(ret
,__LINE__
);
309 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
310 checkrc(ret
,__LINE__
);
312 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
313 checkrc(ret
,__LINE__
);