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
);
71 //***********************************************************************
83 SQLSMALLINT outstrlen
;
85 // Aloocate an environment handle
86 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
87 checkrc(ret
,__LINE__
);
89 //we need odbc3 support
90 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
92 //ALLOCATE A Connection handle
93 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
94 checkrc(ret
,__LINE__
);
96 // connect to the DSN mydsn
97 ret
= SQLConnect (dbc
,
98 (SQLCHAR
*) "test", (SQLSMALLINT
) strlen ("test"),
100 (SQLSMALLINT
) strlen ("root"),
101 (SQLCHAR
*) "manager",
102 (SQLSMALLINT
) strlen (""));
105 if(SQL_SUCCEEDED(ret
))
107 printf("\nConnected to the Data Source..\n");
114 printf("error in connection\n");
115 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
116 checkrc(ret
,__LINE__
);
118 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
119 checkrc(ret
,__LINE__
);
125 //******************************************************************
127 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
128 checkrc(ret
,__LINE__
);
131 "CREATE TABLE T1(F1 INT,F2 char(30))";
132 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
133 checkrc(ret
,__LINE__
);
135 ret
= SQLExecute(stmt
);
136 checkrc(ret
,__LINE__
);
137 printf("\nTABLE CREATED\n");
138 //****************************************************************
141 InsertTest(env
,dbc
,stmt
);
142 //*****************************************************************
144 printf("delete from T1 where F1=10\n");
145 SQLCHAR delete1
[50]="DELETE FROM T1 WHERE F1=10;";
147 ret
= SQLPrepare(stmt
,delete1
,SQL_NTS
);
148 checkrc(ret
,__LINE__
);
150 ret
= SQLExecute(stmt
);
151 checkrc(ret
,__LINE__
);
156 ret
= SQLRowCount(stmt
,&nor
);
157 printf("sqlrowcount() returns=%d\n",nor
); // nor should return 1.
159 ret
= SQLFreeStmt(stmt
,SQL_RESET_PARAMS
);
160 checkrc(ret
,__LINE__
);
161 //************************************************
163 printf("\ndelete from T1 where F1 > 900\n");
164 ret
= SQLPrepare(stmt
,(unsigned char*)"DELETE FROM T1 WHERE F1 > 900",SQL_NTS
);
165 checkrc(ret
,__LINE__
);
167 ret
= SQLExecute(stmt
);
168 checkrc(ret
,__LINE__
);
171 ret
= SQLRowCount(stmt
,&nor1
);
172 printf("sqlrowcount() returns=%d\n",nor1
);
173 if(nor1
!=100)return 1;
175 ret
= SQLFreeStmt(stmt
,SQL_RESET_PARAMS
);
176 checkrc(ret
,__LINE__
);
177 //*************************************************************
178 printf("\ninsert into T1 values(1001,'1000')\n");
179 ret
= SQLPrepare(stmt
,(unsigned char*)"INSERT INTO T1 VALUES(1001,'1000')",SQL_NTS
);
180 checkrc(ret
,__LINE__
);
182 ret
= SQLExecute(stmt
);
183 checkrc(ret
,__LINE__
);
186 ret
= SQLRowCount(stmt
,&nor2
);
187 checkrc(ret
,__LINE__
);
189 printf("sqlrowcount() returns=%d\n",nor2
);
191 ret
= SQLFreeStmt(stmt
,SQL_RESET_PARAMS
);
193 //*****************************************************
194 printf("\nUPDATE T1 SET F2='THOUSAND' WHERE F1<10\n");
195 ret
= SQLPrepare(stmt
,(unsigned char*)"UPDATE T1 SET F2='THOUSAND' WHERE F1<10",SQL_NTS
);
196 checkrc(ret
,__LINE__
);
198 ret
= SQLExecute(stmt
);
199 checkrc(ret
,__LINE__
);
202 ret
= SQLRowCount(stmt
,&nor3
);
204 checkrc(ret
,__LINE__
);
205 printf("sqlrowcount() returns=%d\n",nor3
);
207 /*ret = SQLFreeStmt(stmt,SQL_RESET_PARAMS);
208 checkrc(ret,__LINE__);*/
209 //***********************************************
211 //FetchTest(env,dbc,stmt);
213 //****************************************************************
215 ret
= SQLPrepare(stmt
,(unsigned char*)"drop table T1;",SQL_NTS
);
216 checkrc(ret
,__LINE__
);
218 ret
= SQLExecute(stmt
);
219 checkrc(ret
,__LINE__
);
220 printf("Table 'T1' dropped\n");
222 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
223 checkrc(ret
,__LINE__
);
225 ret
= SQLDisconnect(dbc
);
226 checkrc(ret
,__LINE__
);
228 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
229 checkrc(ret
,__LINE__
);
231 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
232 checkrc(ret
,__LINE__
);