commenting failing queries
[csql.git] / test / odbc / Parameters / parameter2.c
blob8183d92e5b9d8ad3cbe3ec777bd78fc9e9ad87d2
1 // CREATE TABLE "T1" WITH 10 FIELDS.
2 // INSERT SOME TUPLES IN IT.
3 // SELECT TUPLES WITH WHERE CLAUSE
5 // AUTHOR : Jitendra Lenka
7 #include<stdio.h>
8 #include<stdlib.h>
9 #include<sql.h>
10 #include<sqlext.h>
11 #include<string.h>
13 //*************************************************************************
14 // ERROR CHECK FUNCTION
15 inline void checkrc(int rc,int line)
17 if(rc)
19 printf("ERROR %d at line %d\n",rc,line);
20 exit(1);
25 //*************************************************************************
26 // FUNCTION FOR INSERTING ROWS IN IT.
27 int InsertTest(SQLHANDLE env,SQLHANDLE dbc,SQLHANDLE stmt)
30 int ret;
31 int f1=90; // f1 field
32 short int f2=20;//f2 field
33 char f3[50]= "Praba";
34 float f4 = 2.5;
35 float f5 = 10.50;
36 int f9 = 5;
37 long long f10 = 15000;
38 int result;
40 SQLINTEGER slen = SQL_NTS;
41 //***********************************
42 // STRUCTURE FIOR DATE DATATYPE
43 SQL_DATE_STRUCT date;
44 char strDate[30];
46 date.year=2008;
47 date.month=03;
48 date.day=18;
49 // strcpy(strDate,"{d '2008-03-18'}");
50 //*******************************
51 // STRUCTURE FOR TIME DATATYPE.
52 SQL_TIME_STRUCT time;
53 time.hour = 5;
54 time.minute = 22;
55 time.second = 10;
56 //*****************************
57 // STRUCTURE FOR TIMESTAMP DATATYPE
58 SQL_TIMESTAMP_STRUCT timestamp;
59 timestamp.year = 2008;
60 timestamp.month = 03;
61 timestamp.day = 18;
62 timestamp.hour = 5;
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,&timestamp,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__);
103 int i,count=0;
104 // EXECUTE THE STATEMENT
105 for(i=0;i<10;i++)
107 f1++;
108 f2++;
109 f4++;
110 ret = SQLExecute(stmt);
111 checkrc(ret,__LINE__);
113 ret = SQLTransact(env,dbc,SQL_COMMIT);
114 checkrc(ret,__LINE__);
115 count++;
117 printf("Total row inserted=%d\n",count);
119 //***********************************************************************
120 // FETCH ROWS FROM THE TABLE "T1"......select * from T1;
121 int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt)
124 int ret;
125 int f1=10; // f1 field
126 short int f2=20;//f2 field
127 char f3[50]= "praba";
128 float f4 = 2.5;
129 float f5 = 10.50;
130 int f9=5;
131 long long f10=15000;
132 SQLINTEGER slen = SQL_NTS;
133 SQL_DATE_STRUCT date,f6Date;
134 SQL_TIME_STRUCT time, f7Time;
135 SQL_TIMESTAMP_STRUCT timestamp, f8Timestamp;
137 date.year=2008;
138 date.month=03;
139 date.day=18;
141 time.hour=5;
142 time.minute=22;
143 time.second=10;
145 timestamp.year=2008;
146 timestamp.month=03;
147 timestamp.day=18;
148 timestamp.hour=5;
149 timestamp.minute = 22;
150 timestamp.second = 10;
151 timestamp.fraction = 764576;
153 int f1temp,f2temp;
154 char f3temp[20];
155 float f4temp, f5temp;
157 int f9temp;
158 long long f10temp;
160 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);
161 checkrc(ret,__LINE__);
163 //ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SMALL,SQL_SMALL,0,0,)
164 ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&f1temp,0,NULL);
165 checkrc(ret,__LINE__);
167 ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_SSHORT,SQL_INTEGER,0,0,&f2temp,0,NULL);
168 checkrc(ret,__LINE__);
170 ret = SQLBindParameter(stmt,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,196,0,(void*)f3temp,0,&slen);
171 checkrc(ret,__LINE__);
173 ret = SQLBindParameter(stmt,4,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_FLOAT,0,0,&f4temp,0,NULL);
174 checkrc(ret,__LINE__);
176 ret = SQLBindParameter(stmt,5,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_REAL,0,0,&f5temp,0,NULL);
177 checkrc(ret,__LINE__);
179 ret = SQLBindParameter(stmt,6,SQL_PARAM_INPUT,SQL_C_TYPE_DATE,SQL_TYPE_DATE,196,0,&f6Date,sizeof(f6Date),&slen);
180 checkrc(ret,__LINE__);
182 ret = SQLBindParameter(stmt,7,SQL_PARAM_INPUT,SQL_C_TYPE_TIME,SQL_TYPE_TIME,196,0,&f7Time,sizeof(f7Time),&slen);
183 checkrc(ret,__LINE__);
185 ret = SQLBindParameter(stmt,8,SQL_PARAM_INPUT,SQL_C_TYPE_TIMESTAMP,SQL_TYPE_TIMESTAMP,196,0,&f8Timestamp,sizeof(f8Timestamp),&slen);
186 checkrc(ret,__LINE__);
188 ret = SQLBindParameter(stmt,9,SQL_PARAM_INPUT,SQL_C_TINYINT,SQL_TINYINT,0,0,&f9temp,0,NULL);
189 checkrc(ret,__LINE__);
191 ret = SQLBindParameter(stmt,10,SQL_PARAM_INPUT,SQL_C_SBIGINT,SQL_BIGINT,0,0,&f10temp,0,NULL);
192 checkrc(ret,__LINE__);
193 //*******************************************************************************************************
195 ret = SQLBindCol(stmt,1,SQL_C_SLONG,&f1,0,NULL);
196 checkrc(ret,__LINE__);
198 ret = SQLBindCol(stmt,2,SQL_C_SSHORT,&f2,0,NULL);
199 checkrc(ret,__LINE__);
202 ret = SQLBindCol(stmt,3,SQL_C_CHAR,f3,sizeof(f3),NULL);
203 checkrc(ret,__LINE__);
205 ret = SQLBindCol(stmt,4,SQL_C_FLOAT,&f4,0,NULL);
206 checkrc(ret,__LINE__);
208 ret = SQLBindCol(stmt,5,SQL_C_FLOAT,&f5,0,NULL);
209 checkrc(ret,__LINE__);
211 ret = SQLBindCol(stmt,6,SQL_C_TYPE_DATE,&date,sizeof(date),NULL);
212 checkrc(ret,__LINE__);
214 ret = SQLBindCol(stmt,7,SQL_C_TYPE_TIME,&time,sizeof(time),NULL);
215 checkrc(ret,__LINE__);
217 ret = SQLBindCol(stmt,8,SQL_C_TYPE_TIMESTAMP,&timestamp,sizeof(timestamp),NULL);
218 checkrc(ret,__LINE__);
220 ret = SQLBindCol(stmt,9,SQL_C_TINYINT,&f9,0,NULL);
221 checkrc(ret,__LINE__);
223 ret = SQLBindCol(stmt,10,SQL_C_SBIGINT,&f10,0,NULL);
224 checkrc(ret,__LINE__);
226 int j, count=0;
230 f6Date=date;
231 f7Time=time;
232 f8Timestamp=timestamp;
233 for(j=0;j<10;j++)
235 f1temp=f1++;
236 f2temp=f2++;
237 strcpy(f3temp,"praba");
238 f4temp=f4++;
239 f5temp=f5;
240 f9temp=f9;
241 f10temp=f10;
243 ret = SQLExecute(stmt);
244 checkrc(ret,__LINE__);
245 ret = SQLFetch(stmt);
246 ret = SQLCloseCursor(stmt);
247 checkrc(ret,__LINE__);
248 count++;
250 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);
255 ret = SQLTransact(env,dbc,SQL_COMMIT);
256 checkrc(ret,__LINE__);
258 printf("Total row fetched=%d\n",count);
262 int main()
264 SQLHENV env;
265 SQLHDBC dbc;
266 SQLHSTMT stmt;
267 SQLRETURN ret;
268 SQLCHAR outstr[1024];
269 SQLSMALLINT outstrlen;
271 // Aloocate an environment handle
272 ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env);
273 checkrc(ret,__LINE__);
275 //we need odbc3 support
276 SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
278 //ALLOCATE A Connection handle
279 ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);
280 checkrc(ret,__LINE__);
282 // connect to the DSN mydsn
283 ret = SQLConnect (dbc,
284 (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"),
285 (SQLCHAR *) "root",
286 (SQLSMALLINT) strlen ("root"),
287 (SQLCHAR *) "manager",
288 (SQLSMALLINT) strlen (""));
290 if(SQL_SUCCEEDED(ret))
292 printf("\nConnected to the Data Source..\n");
296 else
298 printf("error in connection\n");
299 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
300 checkrc(ret,__LINE__);
302 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
303 checkrc(ret,__LINE__);
304 return 1;
308 //******************************************************************
309 // TABLE CREATED
310 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
311 checkrc(ret,__LINE__);
313 SQLCHAR table[200]=
314 "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT)";
315 ret = SQLPrepare(stmt,table,SQL_NTS);
316 checkrc(ret,__LINE__);
317 ret = SQLExecute(stmt);
318 checkrc(ret,__LINE__);
319 printf("\nTABLE CREATED\n");
320 //*****************************
321 InsertTest(env,dbc,stmt);
322 FetchTest(env,dbc,stmt);
324 //****************************************************************
325 SQLCHAR drop[100]="DROP TABLE T1";
326 ret = SQLPrepare(stmt,drop,SQL_NTS);
327 checkrc(ret,__LINE__);
329 ret = SQLExecute(stmt);
330 checkrc(ret,__LINE__);
331 printf("Table 'T1' dropped successfully\n");
333 ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
334 checkrc(ret,__LINE__);
336 ret = SQLDisconnect(dbc);
337 checkrc(ret,__LINE__);
339 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
340 checkrc(ret,__LINE__);
342 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
343 checkrc(ret,__LINE__);
344 return 0;