1 //connect to the DATA SOURCE with the connection string "DSN=mycsql;MODE=ADAPTER;SERVER=localhost;PORT=5678;"
2 // close the connection and then call fetch,it should fail.
4 // AUTHOR : Jitendra Lenka
12 //*************************************************************************
14 inline void checkrc(int rc
,int line
)
18 printf("ERROR %d at line %d\n",rc
,line
);
23 //*************************************************************************
34 SQLSMALLINT outstrlen
;
36 // Aloocate an environment handle
37 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
38 checkrc(ret
,__LINE__
);
40 //we need odbc3 support
41 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
43 //ALLOCATE A Connection handle
44 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
45 checkrc(ret
,__LINE__
);
47 // connect to the DSN mydsn
48 ret
= SQLConnect (dbc
,
49 (SQLCHAR
*) "DSN=mycsql;MODE=ADAPTER;SERVER=localhost;PORT=5678;", (SQLSMALLINT
) strlen ("DSN=mycsql;MODE=ADAPTER;SERVER=localhost;PORT=5678;"),
51 (SQLSMALLINT
) strlen ("root"),
52 (SQLCHAR
*) "manager",
53 (SQLSMALLINT
) strlen (""));
56 if(SQL_SUCCEEDED(ret
))
58 printf("\nConnected to the Data Source successfully..\n");
65 printf("error in connection\n");
66 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
67 checkrc(ret
,__LINE__
);
69 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
70 checkrc(ret
,__LINE__
);
75 //******************************************************************
77 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
78 checkrc(ret
,__LINE__
);
81 "CREATE TABLE T1(F1 INT,F2 INT)";
82 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
83 checkrc(ret
,__LINE__
);
84 ret
= SQLExecute(stmt
);
85 checkrc(ret
,__LINE__
);
86 printf("\nTABLE CREATED\n");
87 //****************************************************************
88 // insert tuple into table
89 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(100,200)",SQL_NTS
);
90 checkrc(ret
,__LINE__
);
92 ret
= SQLExecute(stmt
);
93 checkrc(ret
,__LINE__
);
94 //*****************************************************************
95 // FETCHING ROWS FROM TABLE
98 ret
= SQLPrepare(stmt
,(unsigned char*)"SELECT * FROM T1",SQL_NTS
);
99 checkrc(ret
,__LINE__
);
101 ret
= SQLBindCol(stmt
,1,SQL_C_LONG
,&f1
,0,NULL
);
102 checkrc(ret
,__LINE__
);
104 ret
= SQLBindCol(stmt
,2,SQL_C_LONG
,&f2
,0,NULL
);
105 checkrc(ret
,__LINE__
);
108 ret
= SQLExecute(stmt
);
109 checkrc(ret
,__LINE__
);
111 //close the connection
112 ret
= SQLDisconnect(dbc
);
113 checkrc(ret
,__LINE__
);
115 // after closing call fetch.
117 while(SQL_SUCCEEDED(ret
=SQLFetch(stmt
)))
121 printf("After disconnect, Fetch failed\n");
126 printf("Number of rows are fetched=%d\n",count
);
129 // *****************************************************************
130 // again connect for drop the table
133 ret
= SQLConnect (dbc
,
134 (SQLCHAR
*) "DSN=mycsql;MODE=ADAPTER;SERVER=localhost;PORT=5678;", (SQLSMALLINT
) strlen ("DSN=mycsql;MODE=ADAPTER;SERVER=localhost;PORT=5678;"),
136 (SQLSMALLINT
) strlen ("root"),
137 (SQLCHAR
*) "manager",
138 (SQLSMALLINT
) strlen (""));
141 if(SQL_SUCCEEDED(ret
))
143 printf("\nagain Connected to the Data Source successfully..\n");
150 printf("error in connection\n");
151 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
152 checkrc(ret
,__LINE__
);
154 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
155 checkrc(ret
,__LINE__
);
160 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
161 checkrc(ret
,__LINE__
);
164 ret
= SQLPrepare(stmt
,(unsigned char*)"drop table T1;",SQL_NTS
);
165 checkrc(ret
,__LINE__
);
167 ret
= SQLExecute(stmt
);
168 checkrc(ret
,__LINE__
);
169 printf("Table dropped\n");
172 //****************************************************************
173 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
174 checkrc(ret
,__LINE__
);
176 ret
= SQLDisconnect(dbc
);
177 checkrc(ret
,__LINE__
);
179 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
180 checkrc(ret
,__LINE__
);
182 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
183 checkrc(ret
,__LINE__
);
184 if(rettype
== 0) return 1;
185 printf("After closing the connection, fetch does not work\n");