*** empty log message ***
[csql.git] / test / odbc / Connect / odbcconnect6.c
bloba3ec2affa6181b0747d8ccd8b5432fa77837a8a1
1 // close the connection and then call fetch,it should fail.
3 // AUTHOR : Jitendra Lenka
5 #include<stdio.h>
6 #include<stdlib.h>
7 #include<sql.h>
8 #include<sqlext.h>
9 #include<string.h>
11 //*************************************************************************
13 inline void checkrc(int rc,int line)
15 if(rc)
17 printf("ERROR %d at line %d\n",rc,line);
18 exit(1);
22 //*************************************************************************
26 int main()
28 SQLHENV env;
29 SQLHDBC dbc;
30 SQLHSTMT stmt;
31 SQLRETURN ret;
32 SQLCHAR outstr[1024];
33 SQLSMALLINT outstrlen;
35 // Aloocate an environment handle
36 ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env);
37 checkrc(ret,__LINE__);
39 //we need odbc3 support
40 SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
42 //ALLOCATE A Connection handle
43 ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);
44 checkrc(ret,__LINE__);
46 // connect to the DSN mydsn
47 ret = SQLConnect (dbc,
48 (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"),
49 (SQLCHAR *) "root",
50 (SQLSMALLINT) strlen ("root"),
51 (SQLCHAR *) "manager",
52 (SQLSMALLINT) strlen (""));
55 if(SQL_SUCCEEDED(ret))
57 printf("\nConnected to the Data Source successfully..\n");
62 else
64 printf("error in connection\n");
65 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
66 checkrc(ret,__LINE__);
68 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
69 checkrc(ret,__LINE__);
70 return 1;
74 //******************************************************************
75 // TABLE CREATED
76 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
77 checkrc(ret,__LINE__);
79 SQLCHAR table[200]=
80 "CREATE TABLE T1(F1 INT,F2 INT)";
81 ret = SQLPrepare(stmt,table,SQL_NTS);
82 checkrc(ret,__LINE__);
83 ret = SQLExecute(stmt);
84 checkrc(ret,__LINE__);
85 printf("\nTABLE CREATED\n");
86 //****************************************************************
87 // insert tuple into table
88 ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(100,200)",SQL_NTS);
89 checkrc(ret,__LINE__);
91 ret = SQLExecute(stmt);
92 checkrc(ret,__LINE__);
93 //*****************************************************************
94 // FETCHING ROWS FROM TABLE
95 int f1=1;
96 int f2=2;
97 ret = SQLPrepare(stmt,(unsigned char*)"SELECT * FROM T1",SQL_NTS);
98 checkrc(ret,__LINE__);
100 ret = SQLBindCol(stmt,1,SQL_C_LONG,&f1,0,NULL);
101 checkrc(ret,__LINE__);
103 ret = SQLBindCol(stmt,2,SQL_C_LONG,&f2,0,NULL);
104 checkrc(ret,__LINE__);
106 int j,count=0;
107 ret = SQLExecute(stmt);
108 checkrc(ret,__LINE__);
110 //close the connection
111 ret = SQLDisconnect(dbc);
112 checkrc(ret,__LINE__);
114 // after closing call fetch.
115 int rettype;
116 while(SQL_SUCCEEDED(ret =SQLFetch(stmt)))
119 if(ret )
120 printf("After disconnect, Fetch failed\n");
121 else
122 count++;
124 rettype=ret;
125 printf("Number of rows are fetched=%d\n",count);
128 // *****************************************************************
129 // again connect for drop the table
132 ret = SQLConnect (dbc,
133 (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"),
134 (SQLCHAR *) "root",
135 (SQLSMALLINT) strlen ("root"),
136 (SQLCHAR *) "manager",
137 (SQLSMALLINT) strlen (""));
140 if(SQL_SUCCEEDED(ret))
142 printf("\nagain Connected to the Data Source successfully..\n");
147 else
149 printf("error in connection\n");
150 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
151 checkrc(ret,__LINE__);
153 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
154 checkrc(ret,__LINE__);
155 return 1;
159 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
160 checkrc(ret,__LINE__);
163 ret = SQLPrepare(stmt,(unsigned char*)"drop table T1;",SQL_NTS);
164 checkrc(ret,__LINE__);
166 ret = SQLExecute(stmt);
167 checkrc(ret,__LINE__);
168 printf("Table dropped\n");
171 //****************************************************************
172 ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
173 checkrc(ret,__LINE__);
175 ret = SQLDisconnect(dbc);
176 checkrc(ret,__LINE__);
178 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
179 checkrc(ret,__LINE__);
181 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
182 checkrc(ret,__LINE__);
183 if(rettype == 0) return 1;
184 printf("After closing the connection, fetch does not work\n");
185 return 0;