Adding new files for oci and sqlnetwork
[csql.git] / examples / odbc / ODBCman6.c
blob8597cae3a4c312517141961ac486ed7ed3080c54
1 // Connect to the Data source
2 // delete rows from the table
3 #include<stdio.h>
4 #include<stdlib.h>
5 #include<sql.h>
6 #include<sqlext.h>
7 #include<string.h>
8 inline void checkrc(int rc, int line)
10 if(rc)
12 printf("ERROR %d at line %d\n",rc,line);
13 exit(1);
18 int main()
20 SQLHENV env;
21 SQLHDBC dbc;
22 SQLHSTMT stmt;
23 SQLRETURN ret;
24 SQLCHAR outstr[1024];
25 SQLSMALLINT outstrlen;
27 ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
28 checkrc(ret,__LINE__);
30 ret = SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
31 checkrc(ret,__LINE__);
33 ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);
34 checkrc(ret,__LINE__);
36 ret = SQLConnect (dbc,
37 (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"),
38 (SQLCHAR *) "root",
39 (SQLSMALLINT) strlen ("root"),
40 (SQLCHAR *) "manager",
41 (SQLSMALLINT) strlen (""));
43 if(SQL_SUCCEEDED(ret))
45 printf("\nConnect to the data source successfully\n");
47 else
49 printf("Failed to connect\n");
50 return 2;
54 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
55 checkrc(ret,__LINE__);
57 // delete 5 rows from the table with parameter.
59 int eid1;
61 ret = SQLPrepare(stmt,(unsigned char*)"delete from emp where eid = ?;",SQL_NTS);
62 checkrc(ret,__LINE__);
64 ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&eid1,0,NULL);
65 checkrc(ret,__LINE__);
67 int i,count=0;
69 eid1=5000;
70 for(i=0;i<5;i++)
72 eid1++;
73 ret = SQLExecute(stmt);
74 checkrc(ret,__LINE__);
76 ret = SQLTransact(env,dbc,SQL_COMMIT);
77 checkrc(ret,__LINE__);
79 ret = SQLTransact(env,dbc,SQL_COMMIT);
80 count++;
82 printf("Deleted first %d updated rows in 'emp' table\n",count);
84 // delete all the rows from the table
85 ret = SQLPrepare(stmt,(unsigned char*)"delete from emp;",SQL_NTS);
86 checkrc(ret,__LINE__);
88 ret = SQLExecute(stmt);
89 checkrc(ret,__LINE__);
91 ret = SQLTransact(env,dbc,SQL_COMMIT);
92 checkrc(ret,__LINE__);
94 printf("All the rows are deleted \n");
96 // drop the table from the table
98 SQLCHAR drop[30]="drop table emp;";
100 ret = SQLExecDirect(stmt,drop,SQL_NTS);
101 checkrc(ret,__LINE__);
104 printf("Table 'emp'dropped successfully\n");
108 ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
109 checkrc(ret,__LINE__);
111 ret - SQLDisconnect(dbc);
112 checkrc(ret,__LINE__);
114 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
115 checkrc(ret,__LINE__);
117 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
118 checkrc(ret,__LINE__);
119 return 0;