modified to accept hostname and connect to the network
[csql.git] / examples / odbc / ODBCman4.c
blobdf4ba4f47edf31ef5e8d070d1f3c263d6e533e62
1 // Connect to the Data source
2 // select all the rows from the table 'emp'
4 #include<stdio.h>
5 #include<stdlib.h>
6 #include<sql.h>
7 #include<sqlext.h>
8 #include<string.h>
9 inline void checkrc(int rc, int line)
11 if(rc)
13 printf("ERROR %d at line %d\n",rc,line);
14 exit(1);
19 int main()
21 SQLHENV env;
22 SQLHDBC dbc;
23 SQLHSTMT stmt;
24 SQLRETURN ret;
25 SQLCHAR outstr[1024];
26 SQLSMALLINT outstrlen;
28 ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
29 checkrc(ret,__LINE__);
31 ret = SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
32 checkrc(ret,__LINE__);
34 ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);
35 checkrc(ret,__LINE__);
37 ret = SQLConnect (dbc,
38 (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"),
39 (SQLCHAR *) "root",
40 (SQLSMALLINT) strlen ("root"),
41 (SQLCHAR *) "manager",
42 (SQLSMALLINT) strlen (""));
44 if(SQL_SUCCEEDED(ret))
46 printf("\nConnect to the data source successfully\n");
48 else
50 printf("Failed to connect\n");
51 return 2;
55 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
56 checkrc(ret,__LINE__);
58 // fetch the rows from the table
60 int eid1=100;
61 char ename1[20]="jitu";
62 float salary1 = 2000;
64 ret = SQLPrepare(stmt,(unsigned char*)"select * from emp;",SQL_NTS);
65 checkrc(ret,__LINE__);
67 ret = SQLBindCol(stmt,1,SQL_C_SLONG,&eid1,0,NULL);
68 checkrc(ret,__LINE__);
70 ret = SQLBindCol(stmt,2,SQL_C_CHAR,ename1,sizeof(ename1),NULL);
71 checkrc(ret,__LINE__);
73 ret = SQLBindCol(stmt,3,SQL_C_FLOAT,&salary1,0,NULL);
74 checkrc(ret,__LINE__);
76 int j,count=0;
77 ret = SQLExecute(stmt);
78 checkrc(ret,__LINE__);
80 printf("Fetching starts on 'emp' table\n ");
81 while(SQL_SUCCEEDED(ret = SQLFetch(stmt)))
83 printf("eid=%d\tename=%s\tsalary=%f\n",eid1,ename1,salary1);
84 count++;
86 ret = SQLCloseCursor(stmt);
87 checkrc(ret,__LINE__);
89 ret =SQLTransact(env,dbc,SQL_COMMIT);
90 checkrc(ret,__LINE__);
92 printf("Total row fetched=%d\n",count);
94 ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
95 checkrc(ret,__LINE__);
97 ret - SQLDisconnect(dbc);
98 checkrc(ret,__LINE__);
100 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
101 checkrc(ret,__LINE__);
103 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
104 checkrc(ret,__LINE__);
105 return 0;