1 // CREATE TABLE "T1" WITH 10 FIELDS.
2 // INSERT SOME TUPLES IN IT.
3 // SELECT TUPLES WITH WHERE CLAUSE
5 // AUTHOR : Jitendra Lenka
13 //*************************************************************************
14 // ERROR CHECK FUNCTION
15 inline void checkrc(int rc
,int line
)
19 printf("ERROR %d at line %d\n",rc
,line
);
25 //*************************************************************************
26 // FUNCTION FOR INSERTING ROWS IN IT.
27 int InsertTest(SQLHANDLE env
,SQLHANDLE dbc
,SQLHANDLE stmt
)
31 int f1
=90; // f1 field
32 short int f2
=20;//f2 field
37 long long f10
= 15000;
40 SQLINTEGER slen
= SQL_NTS
;
41 //***********************************
42 // STRUCTURE FIOR DATE DATATYPE
49 // strcpy(strDate,"{d '2008-03-18'}");
50 //*******************************
51 // STRUCTURE FOR TIME DATATYPE.
56 //*****************************
57 // STRUCTURE FOR TIMESTAMP DATATYPE
58 SQL_TIMESTAMP_STRUCT timestamp
;
59 timestamp
.year
= 2008;
63 timestamp
.minute
= 22;
64 timestamp
.second
= 10;
65 timestamp
.fraction
= 764576;
66 //******************************
67 // PREPARE THE STATEMENT.
68 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(?,?,?,?,?,?,?,?,?,?)",SQL_NTS
);
69 checkrc(ret
,__LINE__
);
71 // BIND PARAMETER FOR ALL THE FIELD
73 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&f1
,0,NULL
);
74 checkrc(ret
,__LINE__
);
76 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_SSHORT
,SQL_SMALLINT
,0,0,&f2
,0,NULL
);
77 checkrc(ret
,__LINE__
);
79 ret
= SQLBindParameter(stmt
,3,SQL_PARAM_INPUT
,SQL_C_CHAR
,SQL_CHAR
,196,0,(void*)f3
,0,&slen
);
80 checkrc(ret
,__LINE__
);
82 ret
= SQLBindParameter(stmt
,4,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_FLOAT
,0,0,&f4
,0,NULL
);
83 checkrc(ret
,__LINE__
);
85 ret
= SQLBindParameter(stmt
,5,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_REAL
,0,0,&f5
,0,NULL
);
86 checkrc(ret
,__LINE__
);
88 ret
= SQLBindParameter(stmt
,6,SQL_PARAM_INPUT
,SQL_C_TYPE_DATE
,SQL_TYPE_DATE
,196,0,&date
,sizeof(date
),&slen
);
89 checkrc(ret
,__LINE__
);
91 ret
= SQLBindParameter(stmt
,7,SQL_PARAM_INPUT
,SQL_C_TYPE_TIME
,SQL_TYPE_TIME
,196,0,&time
,sizeof(time
),&slen
);
92 checkrc(ret
,__LINE__
);
94 ret
= SQLBindParameter(stmt
,8,SQL_PARAM_INPUT
,SQL_C_TYPE_TIMESTAMP
,SQL_TYPE_TIMESTAMP
,196,0,×tamp
,sizeof(timestamp
),&slen
);
95 checkrc(ret
,__LINE__
);
97 ret
= SQLBindParameter(stmt
,9,SQL_PARAM_INPUT
,SQL_C_TINYINT
,SQL_TINYINT
,0,0,&f9
,0,NULL
);
98 checkrc(ret
,__LINE__
);
100 ret
= SQLBindParameter(stmt
,10,SQL_PARAM_INPUT
,SQL_C_SBIGINT
,SQL_BIGINT
,0,0,&f10
,0,NULL
);
101 checkrc(ret
,__LINE__
);
104 // EXECUTE THE STATEMENT
110 ret
= SQLExecute(stmt
);
111 checkrc(ret
,__LINE__
);
113 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
114 checkrc(ret
,__LINE__
);
117 printf("Total row inserted=%d\n",count
);
120 //***********************************************************************
121 // FETCH ROWS FROM THE TABLE "T1"......select * from T1;
122 int FetchTest(SQLHANDLE env
, SQLHANDLE dbc
, SQLHANDLE stmt
)
126 int f1
=10; // f1 field
127 short int f2
=20;//f2 field
128 char f3
[50]= "praba";
133 SQLINTEGER slen
= SQL_NTS
;
134 SQL_DATE_STRUCT date
,f6Date
;
135 SQL_TIME_STRUCT time
, f7Time
;
136 SQL_TIMESTAMP_STRUCT timestamp
, f8Timestamp
;
150 timestamp
.minute
= 22;
151 timestamp
.second
= 10;
152 timestamp
.fraction
= 764576;
156 float f4temp
, f5temp
;
161 ret
= SQLPrepare(stmt
,(unsigned char*)"SELECT * FROM T1 WHERE F1=? AND F2=? AND F3=? AND F4=? AND F5=? AND F6=? AND F7=? AND F8=? AND F9=? AND F10=?",SQL_NTS
);
162 checkrc(ret
,__LINE__
);
164 //ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SMALL,SQL_SMALL,0,0,)
165 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&f1temp
,0,NULL
);
166 checkrc(ret
,__LINE__
);
168 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_SSHORT
,SQL_INTEGER
,0,0,&f2temp
,0,NULL
);
169 checkrc(ret
,__LINE__
);
171 ret
= SQLBindParameter(stmt
,3,SQL_PARAM_INPUT
,SQL_C_CHAR
,SQL_CHAR
,196,0,(void*)f3temp
,0,&slen
);
172 checkrc(ret
,__LINE__
);
174 ret
= SQLBindParameter(stmt
,4,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_FLOAT
,0,0,&f4temp
,0,NULL
);
175 checkrc(ret
,__LINE__
);
177 ret
= SQLBindParameter(stmt
,5,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_REAL
,0,0,&f5temp
,0,NULL
);
178 checkrc(ret
,__LINE__
);
180 ret
= SQLBindParameter(stmt
,6,SQL_PARAM_INPUT
,SQL_C_TYPE_DATE
,SQL_TYPE_DATE
,196,0,&f6Date
,sizeof(f6Date
),&slen
);
181 checkrc(ret
,__LINE__
);
183 ret
= SQLBindParameter(stmt
,7,SQL_PARAM_INPUT
,SQL_C_TYPE_TIME
,SQL_TYPE_TIME
,196,0,&f7Time
,sizeof(f7Time
),&slen
);
184 checkrc(ret
,__LINE__
);
186 ret
= SQLBindParameter(stmt
,8,SQL_PARAM_INPUT
,SQL_C_TYPE_TIMESTAMP
,SQL_TYPE_TIMESTAMP
,196,0,&f8Timestamp
,sizeof(f8Timestamp
),&slen
);
187 checkrc(ret
,__LINE__
);
189 ret
= SQLBindParameter(stmt
,9,SQL_PARAM_INPUT
,SQL_C_TINYINT
,SQL_TINYINT
,0,0,&f9temp
,0,NULL
);
190 checkrc(ret
,__LINE__
);
192 ret
= SQLBindParameter(stmt
,10,SQL_PARAM_INPUT
,SQL_C_SBIGINT
,SQL_BIGINT
,0,0,&f10temp
,0,NULL
);
193 checkrc(ret
,__LINE__
);
194 //*******************************************************************************************************
196 ret
= SQLBindCol(stmt
,1,SQL_C_SLONG
,&f1
,0,NULL
);
197 checkrc(ret
,__LINE__
);
199 ret
= SQLBindCol(stmt
,2,SQL_C_SSHORT
,&f2
,0,NULL
);
200 checkrc(ret
,__LINE__
);
203 ret
= SQLBindCol(stmt
,3,SQL_C_CHAR
,f3
,sizeof(f3
),NULL
);
204 checkrc(ret
,__LINE__
);
206 ret
= SQLBindCol(stmt
,4,SQL_C_FLOAT
,&f4
,0,NULL
);
207 checkrc(ret
,__LINE__
);
209 ret
= SQLBindCol(stmt
,5,SQL_C_FLOAT
,&f5
,0,NULL
);
210 checkrc(ret
,__LINE__
);
212 ret
= SQLBindCol(stmt
,6,SQL_C_TYPE_DATE
,&date
,sizeof(date
),NULL
);
213 checkrc(ret
,__LINE__
);
215 ret
= SQLBindCol(stmt
,7,SQL_C_TYPE_TIME
,&time
,sizeof(time
),NULL
);
216 checkrc(ret
,__LINE__
);
218 ret
= SQLBindCol(stmt
,8,SQL_C_TYPE_TIMESTAMP
,×tamp
,sizeof(timestamp
),NULL
);
219 checkrc(ret
,__LINE__
);
221 ret
= SQLBindCol(stmt
,9,SQL_C_TINYINT
,&f9
,0,NULL
);
222 checkrc(ret
,__LINE__
);
224 ret
= SQLBindCol(stmt
,10,SQL_C_SBIGINT
,&f10
,0,NULL
);
225 checkrc(ret
,__LINE__
);
233 f8Timestamp
=timestamp
;
238 strcpy(f3temp
,"praba");
244 ret
= SQLExecute(stmt
);
245 checkrc(ret
,__LINE__
);
246 ret
= SQLFetch(stmt
);
247 ret
= SQLCloseCursor(stmt
);
248 checkrc(ret
,__LINE__
);
251 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
);
256 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
257 checkrc(ret
,__LINE__
);
259 printf("Total row fetched=%d\n",count
);
270 SQLCHAR outstr
[1024];
271 SQLSMALLINT outstrlen
;
273 // Aloocate an environment handle
274 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
275 checkrc(ret
,__LINE__
);
277 //we need odbc3 support
278 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
280 //ALLOCATE A Connection handle
281 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
282 checkrc(ret
,__LINE__
);
284 // connect to the DSN mydsn
285 ret
= SQLConnect (dbc
,
286 (SQLCHAR
*) "test", (SQLSMALLINT
) strlen ("test"),
288 (SQLSMALLINT
) strlen ("root"),
289 (SQLCHAR
*) "manager",
290 (SQLSMALLINT
) strlen (""));
292 if(SQL_SUCCEEDED(ret
))
294 printf("\nConnected to the Data Source..\n");
300 printf("error in connection\n");
301 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
302 checkrc(ret
,__LINE__
);
304 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
305 checkrc(ret
,__LINE__
);
310 //******************************************************************
312 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
313 checkrc(ret
,__LINE__
);
316 "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT)";
317 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
318 checkrc(ret
,__LINE__
);
319 ret
= SQLExecute(stmt
);
320 checkrc(ret
,__LINE__
);
321 printf("\nTABLE CREATED\n");
322 //*****************************
323 InsertTest(env
,dbc
,stmt
);
324 FetchTest(env
,dbc
,stmt
);
326 //****************************************************************
327 SQLCHAR drop
[100]="DROP TABLE T1";
328 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
329 checkrc(ret
,__LINE__
);
331 ret
= SQLExecute(stmt
);
332 checkrc(ret
,__LINE__
);
333 printf("Table 'T1' dropped successfully\n");
335 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
336 checkrc(ret
,__LINE__
);
338 ret
= SQLDisconnect(dbc
);
339 checkrc(ret
,__LINE__
);
341 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
342 checkrc(ret
,__LINE__
);
344 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
345 checkrc(ret
,__LINE__
);