adding test scripts
[csql.git] / test / odbc / Parameters / parameter5.c
blobe08a102097275eb1d0b2890ac2713d9432c75097
1 /* CREATE TABLE "T1" WITH 10 FIELDS.
2 INSERT SOME TUPLES IN IT.
3 SQLNumParam() should return 10.
5 AUTHOR : JITENDRA Lenka.
6 */
8 #include<stdio.h>
9 #include<stdlib.h>
10 #include<sql.h>
11 #include<sqlext.h>
12 #include<string.h>
13 #include<CSql.h>
14 //*************************************************************************
15 // ERROR CHECK FUNCTION
16 inline void checkrc(int rc,int line)
18 if(rc)
20 printf("ERROR %d at line %d\n",rc,line);
21 exit(1);
25 //*************************************************************************
26 // FUNCTION FOR INSERTING ROWS IN IT.
28 int InsertTest(SQLHANDLE env,SQLHANDLE dbc,SQLHANDLE stmt)
31 int ret;
32 int f1=90; // f1 field
33 short int f2=20;//f2 field
34 char f3[50]= "jitendra";
35 float f4 = 2.5;
36 float f5 = 10.50;
37 int f9 = 5;
38 long long f10 = 15000;
39 int result;
41 SQLINTEGER slen = SQL_NTS;
42 //***********************************
43 // STRUCTURE FOR DATE DATATYPE
44 SQL_DATE_STRUCT date;
45 char strDate[30];
47 date.year=2008;
48 date.month=03;
49 date.day=18;
50 // strcpy(strDate,"{d '2008-03-18'}");
51 //*******************************
52 // STRUCTURE FOR TIME DATATYPE.
53 SQL_TIME_STRUCT time;
54 time.hour = 5;
55 time.minute = 22;
56 time.second = 10;
57 //*****************************
58 // STRUCTURE FOR TIMESTAMP DATATYPE
59 SQL_TIMESTAMP_STRUCT timestamp;
60 timestamp.year = 2008;
61 timestamp.month = 03;
62 timestamp.day = 18;
63 timestamp.hour = 5;
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__);
75 SQLSMALLINT nop;
76 SQLNumParams(stmt,&nop);
80 if(nop!=1)return 1;
81 printf("Number of parameter=%d\n",nop);
83 // BIND PARAMETER FOR ALL THE FIELD
85 ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&f1,0,NULL);
86 checkrc(ret,__LINE__);
89 int i,count=0;
90 // EXECUTE THE STATEMENT
91 for(i=0;i<20;i++)
93 f1++;
94 f2++;
95 f4++;
96 ret = SQLExecute(stmt);
97 checkrc(ret,__LINE__);
99 ret = SQLTransact(env,dbc,SQL_COMMIT);
100 checkrc(ret,__LINE__);
101 count++;
103 printf("Total row inserted=%d\n",count);
104 return 0;
106 //***********************************************************************
109 int main()
111 SQLHENV env;
112 SQLHDBC dbc;
113 SQLHSTMT stmt;
114 SQLRETURN ret;
115 SQLCHAR outstr[1024];
116 SQLSMALLINT outstrlen;
118 // Aloocate an environment handle
119 ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env);
120 checkrc(ret,__LINE__);
122 //we need odbc3 support
123 SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
125 //ALLOCATE A Connection handle
126 ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);
127 checkrc(ret,__LINE__);
129 // connect to the DSN mydsn
130 ret = SQLConnect (dbc,
131 (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"),
132 (SQLCHAR *) "root",
133 (SQLSMALLINT) strlen ("root"),
134 (SQLCHAR *) "manager",
135 (SQLSMALLINT) strlen (""));
138 if(SQL_SUCCEEDED(ret))
140 printf("\nConnected to the Data Source..\n");
145 else
147 printf("error in conenction\n");
149 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
150 checkrc(ret,__LINE__);
152 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
153 checkrc(ret,__LINE__);
154 return 1;
158 //*****************************************************
159 // TABLE CREATED
160 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
161 checkrc(ret,__LINE__);
164 SQLCHAR table[200]=
165 "CREATE TABLE T1(F1 INT);";
170 ret = SQLPrepare(stmt,table,SQL_NTS);
171 checkrc(ret,__LINE__);
173 ret = SQLExecute(stmt);
174 checkrc(ret,__LINE__);
175 printf("\nTABLE CREATED\n");
176 //***************************
177 InsertTest(env,dbc,stmt);
179 // FetchTest(env,dbc,stmt);
181 //**************************************************
182 SQLCHAR drop[100]="DROP TABLE T1";
183 ret = SQLPrepare(stmt,drop,SQL_NTS);
184 checkrc(ret,__LINE__);
185 ret = SQLExecute(stmt);
186 if(ret!=SQL_SUCCESS && ret !=SQL_SUCCESS_WITH_INFO)
188 printf("Statement failed\n");
189 return 1;
191 else
192 printf("Table 'T1' dropped successfully\n");
194 ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
195 checkrc(ret,__LINE__);
197 ret = SQLDisconnect(dbc);
198 checkrc(ret,__LINE__);
200 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
201 checkrc(ret,__LINE__);
203 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
204 checkrc(ret,__LINE__);
205 return 0;