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 SQLDescribecol() Should return all column information in the resultset
4 SQLNumResultCols() Should return no of column in result set
14 //*************************************************************************
15 // ERROR CHECK FUNCTION
16 inline void checkrc(int rc
,int line
)
20 printf("ERROR %d at line %d\n",rc
,line
);
25 //*************************************************************************
26 // FUNCTION FOR INSERTING ROWS IN IT.
28 int InsertTest(SQLHANDLE env
,SQLHANDLE dbc
,SQLHANDLE stmt
)
32 int f1
=90; // f1 field
33 short int f2
=20;//f2 field
34 char f3
[50]= "jitendra";
38 long long f10
= 15000;
41 SQLINTEGER slen
= SQL_NTS
;
42 //***********************************
43 // STRUCTURE FOR DATE DATATYPE
50 // strcpy(strDate,"{d '2008-03-18'}");
51 //*******************************
52 // STRUCTURE FOR TIME DATATYPE.
57 //*****************************
58 // STRUCTURE FOR TIMESTAMP DATATYPE
59 SQL_TIMESTAMP_STRUCT timestamp
;
60 timestamp
.year
= 2008;
64 timestamp
.minute
= 22;
65 timestamp
.second
= 10;
66 timestamp
.fraction
= 764576;
67 //******************************
68 // PREPARE THE STATEMENT.
71 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(?,?,?,?,?,?,?,?,?,?);",SQL_NTS
);
72 checkrc(ret
,__LINE__
);
74 // BIND PARAMETER FOR ALL THE FIELD
76 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&f1
,0,NULL
);
77 checkrc(ret
,__LINE__
);
79 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_SSHORT
,SQL_SMALLINT
,0,0,&f2
,0,NULL
);
80 checkrc(ret
,__LINE__
);
82 ret
= SQLBindParameter(stmt
,3,SQL_PARAM_INPUT
,SQL_C_CHAR
,SQL_CHAR
,196,0,(void*)f3
,0,&slen
);
83 checkrc(ret
,__LINE__
);
85 ret
= SQLBindParameter(stmt
,4,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_FLOAT
,0,0,&f4
,0,NULL
);
86 checkrc(ret
,__LINE__
);
88 ret
= SQLBindParameter(stmt
,5,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_FLOAT
,0,0,&f5
,0,NULL
);
89 checkrc(ret
,__LINE__
);
91 ret
= SQLBindParameter(stmt
,6,SQL_PARAM_INPUT
,SQL_C_TYPE_DATE
,SQL_TYPE_DATE
,196,0,&date
,sizeof(date
),&slen
);
92 checkrc(ret
,__LINE__
);
94 ret
= SQLBindParameter(stmt
,7,SQL_PARAM_INPUT
,SQL_C_TYPE_TIME
,SQL_TYPE_TIME
,196,0,&time
,sizeof(time
),&slen
);
95 checkrc(ret
,__LINE__
);
97 ret
= SQLBindParameter(stmt
,8,SQL_PARAM_INPUT
,SQL_C_TYPE_TIMESTAMP
,SQL_TYPE_TIMESTAMP
,196,0,×tamp
,sizeof(timestamp
),&slen
);
98 checkrc(ret
,__LINE__
);
100 ret
= SQLBindParameter(stmt
,9,SQL_PARAM_INPUT
,SQL_C_TINYINT
,SQL_TINYINT
,0,0,&f9
,0,NULL
);
101 checkrc(ret
,__LINE__
);
103 ret
= SQLBindParameter(stmt
,10,SQL_PARAM_INPUT
,SQL_C_SBIGINT
,SQL_BIGINT
,0,0,&f10
,0,NULL
);
104 checkrc(ret
,__LINE__
);
107 // EXECUTE THE STATEMENT
113 ret
= SQLExecute(stmt
);
114 checkrc(ret
,__LINE__
);
116 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
117 checkrc(ret
,__LINE__
);
120 printf("Total row inserted=%d\n",count
);
123 //***********************************************************************
124 int FetchTest(SQLHANDLE env
, SQLHANDLE dbc
, SQLHANDLE stmt
)
127 int f1
=10; // f1 field
128 short int f2
=20;//f2 field
129 char f3
[50]= "praba";
134 SQLINTEGER slen
= SQL_NTS
;
135 SQL_DATE_STRUCT date
,f6Date
;
136 SQL_TIME_STRUCT time
, f7Time
;
137 SQL_TIMESTAMP_STRUCT timestamp
, f8Timestamp
;
151 timestamp
.minute
= 22;
152 timestamp
.second
= 10;
153 timestamp
.fraction
= 764576;
157 float f4temp
, f5temp
;
162 ret
= SQLPrepare(stmt
,(unsigned char*)"SELECT * FROM T1 ",SQL_NTS
);
163 checkrc(ret
,__LINE__
);
165 ret
= SQLNumResultCols (stmt
, &totalFields
);
166 checkrc(ret
,__LINE__
);
167 printf( "No of columns in resultset = %d\n",totalFields
);
169 UCHAR colName
[IDENTIFIER_LENGTH
];
176 icol
= 1; colNameMax
= IDENTIFIER_LENGTH
;
178 ret
= SQLDescribeCol(stmt
, icol
, colName
, colNameMax
, &nameLength
, &colType
, &colLength
, &scale
, &nullable
);
179 checkrc(ret
,__LINE__
);
181 printf("Name %s len %d type %d collen %d scale %d nullable %d\n",colName
,nameLength
,colType
,colLength
,scale
,nullable
);
186 ret
= SQLBindCol(stmt
,1,SQL_C_SLONG
,&f1
,0,NULL
);
187 checkrc(ret
,__LINE__
);
189 ret
= SQLBindCol(stmt
,2,SQL_C_SSHORT
,&f2
,0,NULL
);
190 checkrc(ret
,__LINE__
);
193 ret
= SQLBindCol(stmt
,3,SQL_C_CHAR
,f3
,sizeof(f3
),NULL
);
194 checkrc(ret
,__LINE__
);
196 ret
= SQLBindCol(stmt
,4,SQL_C_FLOAT
,&f4
,0,NULL
);
197 checkrc(ret
,__LINE__
);
199 ret
= SQLBindCol(stmt
,5,SQL_C_FLOAT
,&f5
,0,NULL
);
200 checkrc(ret
,__LINE__
);
202 ret
= SQLBindCol(stmt
,6,SQL_C_TYPE_DATE
,&date
,sizeof(date
),NULL
);
203 checkrc(ret
,__LINE__
);
205 ret
= SQLBindCol(stmt
,7,SQL_C_TYPE_TIME
,&time
,sizeof(time
),NULL
);
206 checkrc(ret
,__LINE__
);
208 ret
= SQLBindCol(stmt
,8,SQL_C_TYPE_TIMESTAMP
,×tamp
,sizeof(timestamp
),NULL
);
209 checkrc(ret
,__LINE__
);
211 ret
= SQLBindCol(stmt
,9,SQL_C_TINYINT
,&f9
,0,NULL
);
212 checkrc(ret
,__LINE__
);
214 ret
= SQLBindCol(stmt
,10,SQL_C_SBIGINT
,&f10
,0,NULL
);
215 checkrc(ret
,__LINE__
);
223 f8Timestamp
=timestamp
;
228 strcpy(f3temp
,"praba");
234 ret
= SQLExecute(stmt
);
235 checkrc(ret
,__LINE__
);
236 ret
= SQLFetch(stmt
);
237 ret
= SQLCloseCursor(stmt
);
238 checkrc(ret
,__LINE__
);
241 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
);
246 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
247 checkrc(ret
,__LINE__
);
249 printf("Total row fetched=%d\n",count
);
261 SQLCHAR outstr
[1024];
262 SQLSMALLINT outstrlen
;
264 // Aloocate an environment handle
265 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
266 checkrc(ret
,__LINE__
);
268 //we need odbc3 support
269 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
271 //ALLOCATE A Connection handle
272 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
273 checkrc(ret
,__LINE__
);
275 // connect to the DSN mydsn
276 ret
= SQLConnect (dbc
,
277 (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;"),
279 (SQLSMALLINT
) strlen ("root"),
280 (SQLCHAR
*) "manager",
281 (SQLSMALLINT
) strlen (""));
284 if(SQL_SUCCEEDED(ret
))
286 printf("\nConnected to the Data Source..\n");
293 printf("error in conenction\n");
295 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
296 checkrc(ret
,__LINE__
);
298 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
299 checkrc(ret
,__LINE__
);
304 //*****************************************************
306 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
307 checkrc(ret
,__LINE__
);
310 "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT);";
312 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
313 checkrc(ret
,__LINE__
);
315 ret
= SQLExecute(stmt
);
316 checkrc(ret
,__LINE__
);
317 printf("\nTABLE CREATED\n");
318 //***************************
319 InsertTest(env
,dbc
,stmt
);
320 FetchTest(env
,dbc
,stmt
);
321 //**************************************************
322 SQLCHAR drop
[100]="DROP TABLE T1";
323 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
324 checkrc(ret
,__LINE__
);
325 ret
= SQLExecute(stmt
);
326 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
328 printf("Statement failed\n");
332 printf("Table 'T1' dropped successfully\n");
334 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
335 checkrc(ret
,__LINE__
);
337 ret
= SQLDisconnect(dbc
);
338 checkrc(ret
,__LINE__
);
340 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
341 checkrc(ret
,__LINE__
);
343 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
344 checkrc(ret
,__LINE__
);