1 // odbctestcsql.cpp : Defines the entry point for the console application.
6 int main(int argc
, char* argv
[])
17 void checkrc(int rc
,int line
)
21 printf("Error %d at line %d\n", rc
, line
);
33 SQLSMALLINT outstrlen
;
36 //Allocate an environment handle
37 ret
= SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
38 checkrc(ret
,__LINE__
);
39 printf("Allocate env handle\n");
41 //we need ODBC3 support
42 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
43 printf("Allocate setenv handle\n");
45 //Allocate a Connection handle
46 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
47 checkrc(ret
,__LINE__
);
48 printf("Allocate conn handle\n");
51 strcpy(dsn
, "DSN=mycsql;MODE=csql;SERVER=192.168.1.3;PORT=5678;");
52 //connect to Data source
53 ret
= SQLConnect (dbc
,
54 (SQLCHAR
*) dsn
, (SQLSMALLINT
) strlen (dsn
),
56 (SQLSMALLINT
) strlen ("root"),
57 (SQLCHAR
*) "manager",
58 (SQLSMALLINT
) strlen ("manager"));
59 printf("connect called\n");
61 //connect using unixODBC Driver Manager
62 ret = SQLDriverConnect(dbc, NULL, (SQLCHAR*)
63 "DSN=mycsql;USER=root;PASSWORD=manager;", SQL_NTS,
64 outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
68 if(SQL_SUCCEEDED(ret
))
70 printf("Connected to CSQL\n");
74 printf("error in connection\n");
75 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
76 checkrc(ret
,__LINE__
);
77 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
78 checkrc(ret
,__LINE__
);
82 // Allocation of statement handle for DDL nad DML Operation
83 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
84 checkrc(ret
,__LINE__
);
86 // create table 'T1' with two fields, F1 INTEGER AND F2 CHAR.
87 SQLCHAR table
[200]= "CREATE TABLE T1(F1 INT,F2 CHAR(20))";
89 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
90 checkrc(ret
,__LINE__
);
92 ret
= SQLExecute(stmt
);
93 checkrc(ret
,__LINE__
);
95 printf("Table T1 created\n");
97 //Insert 10 Tuples into the table 'T1'
99 char name
[20]="THIRU";
100 char names
[10][20]={"Gopal", "Aruna", "Kanchana", "Vijay", "Ganga",
101 "XieLiang", "Rajesh", "Steve", "Veda", "Jitendra" };
104 SQLINTEGER slen
= SQL_NTS
;
106 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(?,?);",SQL_NTS
);
107 checkrc(ret
,__LINE__
);
109 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&id
,0,NULL
);
110 checkrc(ret
,__LINE__
);
112 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_CHAR
,SQL_CHAR
,196,0,(void*)name
,0,&slen
);
113 checkrc(ret
,__LINE__
);
119 strcpy(name
,names
[i
]);
121 ret
= SQLExecute(stmt
);
122 checkrc(ret
,__LINE__
);
124 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
125 checkrc(ret
,__LINE__
);
128 printf("%d Rows inserted\n",count
);
131 //Fetch rows from the table 'T1'
134 ret
= SQLPrepare(stmt
,(unsigned char*)"SELECT * FROM T1;",SQL_NTS
);
135 checkrc(ret
,__LINE__
);
137 ret
= SQLBindCol(stmt
,1,SQL_C_SLONG
,&id1
,0,NULL
);
138 checkrc(ret
,__LINE__
);
140 ret
= SQLBindCol(stmt
,2,SQL_C_CHAR
,name
,sizeof(name
),NULL
);
141 checkrc(ret
,__LINE__
);
144 ret
= SQLExecute(stmt
);
145 checkrc(ret
,__LINE__
);
147 printf("Fetching starts on table T1 :\n");
148 while(SQL_SUCCEEDED(ret
=SQLFetch(stmt
)))
150 printf("F1:%d\tF2:%s\n",id1
,name
);
153 ret
= SQLCloseCursor(stmt
);
154 checkrc(ret
,__LINE__
);
156 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
157 checkrc(ret
,__LINE__
);
159 printf("%d rows fetched\n",count
);
161 //Delete all the rows from the table 'T1'
162 ret
= SQLPrepare(stmt
,(unsigned char*)"DELETE FROM T1 WHERE F1=?;",SQL_NTS
);
163 checkrc(ret
,__LINE__
);
165 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_SLONG
,SQL_INTEGER
,0,0,&id1
,0,NULL
);
166 checkrc(ret
,__LINE__
);
173 ret
= SQLExecute(stmt
);
174 checkrc(ret
,__LINE__
);
178 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
179 checkrc(ret
,__LINE__
);
180 printf("%d Rows deleted\n",count
);
183 // drop the table 'T1'
184 SQLCHAR drop
[50]="DROP TABLE T1";
185 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
186 checkrc(ret
,__LINE__
);
188 ret
= SQLExecute(stmt
);
189 checkrc(ret
,__LINE__
);
190 printf("Table T1 dropped\n");
192 //Free the statement handle
193 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
194 checkrc(ret
,__LINE__
);
196 //Disconnect from the Data source
197 ret
= SQLDisconnect(dbc
);
198 checkrc(ret
,__LINE__
);
199 printf("Disconnected from CSQL\n");
201 //Free the connection handle
202 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
203 checkrc(ret
,__LINE__
);
205 //Free the environment handle
206 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
207 checkrc(ret
,__LINE__
);