1 /* create table Ti with one integer field and one char field. insert some records into it.
2 delete from t1 where f1=10.
3 sqlrowcount() should return 1.
5 delete from t1 where f1 > 900
6 sqlrowcount shpuld return 100.
8 insert one record with values(1001,'1000')
9 sqlrowcount should return 1.
11 update T1 set f2='thousand' where f1<10.
12 sqlrowcount() should return 9
15 AUTHOR : Jitendra Lenka
25 inline void checkrc(int rc
,int line
)
29 printf("ERROR %d at line %d\n",rc
,line
);
34 //*************************************************************************
35 int InsertTest(SQLHANDLE env
,SQLHANDLE dbc
,SQLHANDLE stmt
)
40 char f2
[15]="jitendra";//f2 field
41 SQLINTEGER slen
=SQL_NTS
;
43 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(?,?)",SQL_NTS
);
44 checkrc(ret
,__LINE__
);
48 ret
= SQLBindParameter(stmt
,1,SQL_PARAM_INPUT
,SQL_C_LONG
,SQL_INTEGER
,0,0,&f1
,0,NULL
);
49 checkrc(ret
,__LINE__
);
51 ret
= SQLBindParameter(stmt
,2,SQL_PARAM_INPUT
,SQL_C_CHAR
,SQL_CHAR
,196,0,(void*)f2
,0,&slen
);
52 checkrc(ret
,__LINE__
);
62 ret
= SQLExecute(stmt
);
63 checkrc(ret
,__LINE__
);
65 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
66 checkrc(ret
,__LINE__
);
69 printf("Total row inserted=%d\n",count
);
72 //***********************************************************************
84 SQLSMALLINT outstrlen
;
86 // Aloocate an environment handle
87 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
88 checkrc(ret
,__LINE__
);
90 //we need odbc3 support
91 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
93 //ALLOCATE A Connection handle
94 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
95 checkrc(ret
,__LINE__
);
97 // connect to the DSN mydsn
98 ret
= SQLConnect (dbc
,
99 (SQLCHAR
*) "test", (SQLSMALLINT
) strlen ("test"),
101 (SQLSMALLINT
) strlen ("root"),
102 (SQLCHAR
*) "manager",
103 (SQLSMALLINT
) strlen (""));
106 if(SQL_SUCCEEDED(ret
))
108 printf("\nConnected to the Data Source..\n");
115 printf("error in connection\n");
116 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
117 checkrc(ret
,__LINE__
);
119 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
120 checkrc(ret
,__LINE__
);
126 //******************************************************************
128 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
129 checkrc(ret
,__LINE__
);
132 "CREATE TABLE T1(F1 INT,F2 char(30))";
133 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
134 checkrc(ret
,__LINE__
);
136 ret
= SQLExecute(stmt
);
137 checkrc(ret
,__LINE__
);
138 printf("\nTABLE CREATED\n");
139 //****************************************************************
142 InsertTest(env
,dbc
,stmt
);
143 //*****************************************************************
145 printf("delete from T1 where F1=10\n");
146 SQLCHAR delete1
[50]="DELETE FROM T1 WHERE F1=10;";
148 ret
= SQLPrepare(stmt
,delete1
,SQL_NTS
);
149 checkrc(ret
,__LINE__
);
151 ret
= SQLExecute(stmt
);
152 checkrc(ret
,__LINE__
);
157 ret
= SQLRowCount(stmt
,&nor
);
158 printf("sqlrowcount() returns=%d\n",nor
); // nor should return 1.
160 ret
= SQLFreeStmt(stmt
,SQL_RESET_PARAMS
);
161 checkrc(ret
,__LINE__
);
162 //************************************************
164 printf("\ndelete from T1 where F1 > 900\n");
165 ret
= SQLPrepare(stmt
,(unsigned char*)"DELETE FROM T1 WHERE F1 > 900",SQL_NTS
);
166 checkrc(ret
,__LINE__
);
168 ret
= SQLExecute(stmt
);
169 checkrc(ret
,__LINE__
);
172 ret
= SQLRowCount(stmt
,&nor1
);
173 printf("sqlrowcount() returns=%d\n",nor1
);
174 if(nor1
!=100)return 1;
176 ret
= SQLFreeStmt(stmt
,SQL_RESET_PARAMS
);
177 checkrc(ret
,__LINE__
);
178 //*************************************************************
179 printf("\ninsert into T1 values(1001,'1000')\n");
180 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(1001,'1000')",SQL_NTS
);
181 checkrc(ret
,__LINE__
);
183 ret
= SQLExecute(stmt
);
184 checkrc(ret
,__LINE__
);
187 ret
= SQLRowCount(stmt
,&nor2
);
188 checkrc(ret
,__LINE__
);
190 printf("sqlrowcount() returns=%d\n",nor2
);
192 ret
= SQLFreeStmt(stmt
,SQL_RESET_PARAMS
);
194 //*****************************************************
195 printf("\nUPDATE T1 SET F2='THOUSAND' WHERE F1<10\n");
196 ret
= SQLPrepare(stmt
,(unsigned char*)"UPDATE T1 SET F2='THOUSAND' WHERE F1<10",SQL_NTS
);
197 checkrc(ret
,__LINE__
);
199 ret
= SQLExecute(stmt
);
200 checkrc(ret
,__LINE__
);
203 ret
= SQLRowCount(stmt
,&nor3
);
205 checkrc(ret
,__LINE__
);
206 printf("sqlrowcount() returns=%d\n",nor3
);
208 /*ret = SQLFreeStmt(stmt,SQL_RESET_PARAMS);
209 checkrc(ret,__LINE__);*/
210 //***********************************************
212 //FetchTest(env,dbc,stmt);
214 //****************************************************************
216 ret
= SQLPrepare(stmt
,(unsigned char*)"drop table T1;",SQL_NTS
);
217 checkrc(ret
,__LINE__
);
219 ret
= SQLExecute(stmt
);
220 checkrc(ret
,__LINE__
);
221 printf("Table 'T1' dropped\n");
223 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
224 checkrc(ret
,__LINE__
);
226 ret
= SQLDisconnect(dbc
);
227 checkrc(ret
,__LINE__
);
229 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
230 checkrc(ret
,__LINE__
);
232 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
233 checkrc(ret
,__LINE__
);