fixing compilation issue
[csql.git] / tmptest / winodbctest / odbctestcsql.cpp
blob2d4e5d778c5fad1a68def84a6ba0bbd61d08b28b
1 // odbctestcsql.cpp : Defines the entry point for the console application.
2 //
4 int mainfn();
6 int main(int argc, char* argv[])
8 return mainfn();
10 #include <stdio.h>
11 #include <string.h>
12 #include<stdlib.h>
13 #include<sql.h>
14 #include<sqlext.h>
15 #include<time.h>
17 void checkrc(int rc,int line)
19 if(rc)
21 printf("Error %d at line %d\n", rc, line);
22 exit(1);
26 int mainfn()
28 SQLHENV env;
29 SQLHDBC dbc;
30 SQLHSTMT stmt;
31 SQLRETURN ret;
32 SQLCHAR outstr[1024];
33 SQLSMALLINT outstrlen;
34 printf("STARTED\n");
36 //Allocate an environment handle
37 ret = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env);
38 checkrc(ret,__LINE__);
39 printf("Allocate env handle\n");
41 //we need ODBC3 support
42 SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
43 printf("Allocate setenv handle\n");
45 //Allocate a Connection handle
46 ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);
47 checkrc(ret,__LINE__);
48 printf("Allocate conn handle\n");
50 char dsn[1024];
51 strcpy(dsn, "DSN=mycsql;MODE=csql;SERVER=192.168.1.3;PORT=5678;");
52 //connect to Data source
53 ret = SQLConnect (dbc,
54 (SQLCHAR *) dsn, (SQLSMALLINT) strlen (dsn),
55 (SQLCHAR *) "root",
56 (SQLSMALLINT) strlen ("root"),
57 (SQLCHAR *) "manager",
58 (SQLSMALLINT) strlen ("manager"));
59 printf("connect called\n");
61 //connect using unixODBC Driver Manager
62 ret = SQLDriverConnect(dbc, NULL, (SQLCHAR*)
63 "DSN=mycsql;USER=root;PASSWORD=manager;", SQL_NTS,
64 outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
68 if(SQL_SUCCEEDED(ret))
70 printf("Connected to CSQL\n");
72 else
74 printf("error in connection\n");
75 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
76 checkrc(ret,__LINE__);
77 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
78 checkrc(ret,__LINE__);
79 return 2;
82 // Allocation of statement handle for DDL nad DML Operation
83 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
84 checkrc(ret,__LINE__);
86 // create table 'T1' with two fields, F1 INTEGER AND F2 CHAR.
87 SQLCHAR table[200]= "CREATE TABLE T1(F1 INT,F2 CHAR(20))";
89 ret = SQLPrepare(stmt,table,SQL_NTS);
90 checkrc(ret,__LINE__);
92 ret = SQLExecute(stmt);
93 checkrc(ret,__LINE__);
95 printf("Table T1 created\n");
97 //Insert 10 Tuples into the table 'T1'
98 int id=10;
99 char name[20]="THIRU";
100 char names[10][20]={"Gopal", "Aruna", "Kanchana", "Vijay", "Ganga",
101 "XieLiang", "Rajesh", "Steve", "Veda", "Jitendra" };
104 SQLINTEGER slen = SQL_NTS;
106 ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(?,?);",SQL_NTS);
107 checkrc(ret,__LINE__);
109 ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&id,0,NULL);
110 checkrc(ret,__LINE__);
112 ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,196,0,(void*)name,0,&slen);
113 checkrc(ret,__LINE__);
115 int i,count=0;
116 for(i=0;i<10;i++)
118 id++;
119 strcpy(name,names[i]);
121 ret = SQLExecute(stmt);
122 checkrc(ret,__LINE__);
124 ret = SQLTransact(env,dbc,SQL_COMMIT);
125 checkrc(ret,__LINE__);
126 count++;
128 printf("%d Rows inserted\n",count);
131 //Fetch rows from the table 'T1'
132 int id1=10;
134 ret = SQLPrepare(stmt,(unsigned char*)"SELECT * FROM T1;",SQL_NTS);
135 checkrc(ret,__LINE__);
137 ret = SQLBindCol(stmt,1,SQL_C_SLONG,&id1,0,NULL);
138 checkrc(ret,__LINE__);
140 ret = SQLBindCol(stmt,2,SQL_C_CHAR,name,sizeof(name),NULL);
141 checkrc(ret,__LINE__);
143 count=0;
144 ret = SQLExecute(stmt);
145 checkrc(ret,__LINE__);
147 printf("Fetching starts on table T1 :\n");
148 while(SQL_SUCCEEDED(ret=SQLFetch(stmt)))
150 printf("F1:%d\tF2:%s\n",id1,name);
151 count++;
153 ret = SQLCloseCursor(stmt);
154 checkrc(ret,__LINE__);
156 ret = SQLTransact(env,dbc,SQL_COMMIT);
157 checkrc(ret,__LINE__);
159 printf("%d rows fetched\n",count);
161 //Delete all the rows from the table 'T1'
162 ret = SQLPrepare(stmt,(unsigned char*)"DELETE FROM T1 WHERE F1=?;",SQL_NTS);
163 checkrc(ret,__LINE__);
165 ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&id1,0,NULL);
166 checkrc(ret,__LINE__);
169 count=0;
170 for(i=0;i<10;i++)
172 id++;
173 ret = SQLExecute(stmt);
174 checkrc(ret,__LINE__);
175 count++;
178 ret = SQLTransact(env,dbc,SQL_COMMIT);
179 checkrc(ret,__LINE__);
180 printf("%d Rows deleted\n",count);
183 // drop the table 'T1'
184 SQLCHAR drop[50]="DROP TABLE T1";
185 ret = SQLPrepare(stmt,drop,SQL_NTS);
186 checkrc(ret,__LINE__);
188 ret = SQLExecute(stmt);
189 checkrc(ret,__LINE__);
190 printf("Table T1 dropped\n");
192 //Free the statement handle
193 ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
194 checkrc(ret,__LINE__);
196 //Disconnect from the Data source
197 ret = SQLDisconnect(dbc);
198 checkrc(ret,__LINE__);
199 printf("Disconnected from CSQL\n");
201 //Free the connection handle
202 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
203 checkrc(ret,__LINE__);
205 //Free the environment handle
206 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
207 checkrc(ret,__LINE__);
209 return 0;