test cases for trie index
[csql.git] / test / odbc / Adapter / odbcconnect6d.c
blob357f32b73728643f895c1d79d690098e20d457a0
1 //connect to the DATA SOURCE with the connection string "DSN=mycsql;MODE=ADAPTER;SERVER=127.0.0.1;PORT=5678;"
2 // close the connection and then call fetch,it should fail.
4 // AUTHOR : Jitendra Lenka
6 #include<stdio.h>
7 #include<stdlib.h>
8 #include<sql.h>
9 #include<sqlext.h>
10 #include<string.h>
12 //*************************************************************************
14 inline void checkrc(int rc,int line)
16 if(rc)
18 printf("ERROR %d at line %d\n",rc,line);
19 exit(1);
23 //*************************************************************************
27 int main()
29 SQLHENV env;
30 SQLHDBC dbc;
31 SQLHSTMT stmt;
32 SQLRETURN ret;
33 SQLCHAR outstr[1024];
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=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=ADAPTER;SERVER=127.0.0.1;PORT=5678;"),
50 (SQLCHAR *) "root",
51 (SQLSMALLINT) strlen ("root"),
52 (SQLCHAR *) "manager",
53 (SQLSMALLINT) strlen (""));
56 if(SQL_SUCCEEDED(ret))
58 printf("\nConnected to the Data Source successfully..\n");
63 else
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__);
71 return 1;
75 //******************************************************************
76 // TABLE CREATED
77 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
78 checkrc(ret,__LINE__);
80 SQLCHAR table[200]=
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
96 int f1=1;
97 int f2=2;
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__);
107 int j,count=0;
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.
116 int rettype;
117 while(SQL_SUCCEEDED(ret =SQLFetch(stmt)))
120 if(ret )
121 printf("After disconnect, Fetch failed\n");
122 else
123 count++;
125 rettype=ret;
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=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=ADAPTER;SERVER=127.0.0.1;PORT=5678;"),
135 (SQLCHAR *) "root",
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");
148 else
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__);
156 return 1;
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");
186 return 0;