1 // Connect to the Data source
2 // update 5 rows in the table
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 // update rows in the table
60 ret
= SQLPrepare(stmt
,(unsigned char*)"update emp set eid=?,salary=? where eid = ?;",SQL_NTS
);
61 checkrc(ret
,__LINE__
);
67 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&eid1
,0,NULL
);
68 checkrc(ret
,__LINE__
);
70 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_FLOAT
,SQL_REAL
,0,0,&salary1
,0,NULL
);
71 checkrc(ret
,__LINE__
);
73 ret
= SQLBindParameter(stmt
,3,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&eid2
,0,NULL
);
82 salary1
= 20500+(500*i
);
84 ret
- SQLExecute(stmt
);
85 checkrc(ret
,__LINE__
);
87 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
88 checkrc(ret
,__LINE__
);
92 printf("%d rows updated in 'emp' table\n",count
);
96 char ename1
[10]="jitu";
98 ret
= SQLPrepare(stmt
,(unsigned char*)"select * from emp where salary > 20000;",SQL_NTS
);
99 checkrc(ret
,__LINE__
);
101 ret
= SQLBindCol(stmt
,1,SQL_C_SLONG
,&eid1
,0,NULL
);
102 checkrc(ret
,__LINE__
);
104 ret
= SQLBindCol(stmt
,2,SQL_C_CHAR
,ename1
,sizeof(ename1
),NULL
);
105 checkrc(ret
,__LINE__
);
107 ret
= SQLBindCol(stmt
,3,SQL_C_FLOAT
,&salary1
,0,NULL
);
108 checkrc(ret
,__LINE__
);
113 ret
= SQLExecute(stmt
);
114 checkrc(ret
,__LINE__
);
116 printf("select updated rows\n");
117 while(SQL_SUCCEEDED(ret
=SQLFetch(stmt
)))
119 printf("eid=%d\tename=%s\tsalary=%f\n",eid1
,ename1
,salary1
);
123 ret
= SQLCloseCursor(stmt
);
124 checkrc(ret
,__LINE__
);
126 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
127 checkrc(ret
,__LINE__
);
129 ret
- SQLDisconnect(dbc
);
130 checkrc(ret
,__LINE__
);
132 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
133 checkrc(ret
,__LINE__
);
135 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
136 checkrc(ret
,__LINE__
);