1 // Connect to the Data source
2 // select all the rows from the table 'emp'
9 inline void checkrc(int rc
, int line
)
13 printf("ERROR %d at line %d\n",rc
,line
);
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"),
40 (SQLSMALLINT
) strlen ("root"),
41 (SQLCHAR
*) "manager",
42 (SQLSMALLINT
) strlen (""));
44 if(SQL_SUCCEEDED(ret
))
46 printf("\nConnect to the data source successfully\n");
50 printf("Failed to connect\n");
55 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
56 checkrc(ret
,__LINE__
);
58 // fetch the rows from the table
61 char ename1
[20]="jitu";
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__
);
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
);
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__
);