start only if sql server if turned on in csql.conf file
[csql.git] / examples / odbc / ODBCman5.c
blob1d35e7131873d72845e83d7ac0fc31daecf0a2da
1 // Connect to the Data source
2 // update 5 rows in the table
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 // update rows in the table
60 ret = SQLPrepare(stmt,(unsigned char*)"update emp set eid=?,salary=? where eid = ?;",SQL_NTS);
61 checkrc(ret,__LINE__);
63 int eid1;
64 int eid2;
65 float salary1;
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);
74 int i,count=0;
76 eid1=5000;
78 for(i=0;i<5;i++)
80 eid2 = 1001 + i;
81 eid1++;
82 salary1= 20500+(500*i);
84 ret - SQLExecute(stmt);
85 checkrc(ret,__LINE__);
87 ret = SQLTransact(env,dbc,SQL_COMMIT);
88 checkrc(ret,__LINE__);
90 count++;
92 printf("%d rows updated in 'emp' table\n",count);
95 //fetch updated rows
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__);
111 count=0;
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);
120 count++;
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__);
137 return 0;