modified to accept hostname and connect to the network
[csql.git] / examples / odbc / ODBCman3.c
blob45860c541c1c700b887e05b23f6154fcc6d761a6
1 // Connect to the Data source
2 // insert 10 rows in it.
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__);
59 // inserting records into the table.
61 int eid1 = 1000;
62 char ename1[20]="jitu";
63 float salary1 = 5500;
64 char ename2[10][20]= {"Praba","Kishor","Jitu","Sanjit","Sanjay","Bisi","Suman","Vikrant","Eti","Suba"};
65 SQLINTEGER slen=SQL_NTS;
67 ret = SQLPrepare(stmt,(unsigned char*)"insert into emp values(?,?,?);",SQL_NTS);
68 checkrc(ret,__LINE__);
70 ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&eid1,0,NULL);
71 checkrc(ret,__LINE__);
73 ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,196,0,(void*)ename1,0,&slen);
74 checkrc(ret,__LINE__);
76 ret = SQLBindParameter(stmt,3,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_REAL,0,0,&salary1,0,NULL);
77 checkrc(ret,__LINE__);
78 int i;
79 int count=0;
80 for( i=0;i<10;i++)
82 eid1++;
83 salary1 = salary1 + 1000;
84 strcpy(ename1,ename2[i]);
85 ret = SQLExecute(stmt);
86 checkrc(ret,__LINE__);
88 ret = SQLTransact(env,dbc,SQL_COMMIT);
89 checkrc(ret,__LINE__);
91 count++;
93 printf("%d rows inserted in 'emp' table\n",count);
95 ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
96 checkrc(ret,__LINE__);
98 ret - SQLDisconnect(dbc);
99 checkrc(ret,__LINE__);
101 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
102 checkrc(ret,__LINE__);
104 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
105 checkrc(ret,__LINE__);
106 return 0;