1 /* CREATE TABLE "T1" WITH 10 FIELDS.
2 CREATE TABLE "T2" WITH 10 FIELDS.
3 CREATE TABLE "T3" WITH 10 FIELDS.
4 SQLTables() Should return all table information
5 CatalogName="", SchemaName="", TableType=""
14 //*************************************************************************
15 // ERROR CHECK FUNCTION
16 inline void checkrc(int rc
,int line
)
20 printf("ERROR %d at line %d\n",rc
,line
);
25 //*************************************************************************
26 // FUNCTION FOR INSERTING ROWS IN IT.
28 int FetchTest(SQLHANDLE env
, SQLHANDLE dbc
, SQLHANDLE stmt
)
37 ret
= SQLTables(stmt
, (SQLCHAR
*)"", SQL_NTS
, (SQLCHAR
*)"", SQL_NTS
, (SQLCHAR
*)"", SQL_NTS
, (SQLCHAR
*)"", SQL_NTS
);
38 // ret = SQLTables(stmt, (SQLCHAR*)"Csql", NULL,(SQLCHAR*)"cSQL", NULL, NULL, NULL, (SQLCHAR*)"view", NULL );
39 checkrc(ret
,__LINE__
);
41 ret
= SQLNumResultCols (stmt
, &totalFields
);
42 checkrc(ret
,__LINE__
);
43 printf( "No of columns in resultset = %d\n",totalFields
);
45 ret
= SQLBindCol(stmt
,1,SQL_C_CHAR
,f1
,sizeof(f1
),NULL
);
46 checkrc(ret
,__LINE__
);
47 ret
= SQLBindCol(stmt
,2,SQL_C_CHAR
,f2
,sizeof(f2
),NULL
);
48 checkrc(ret
,__LINE__
);
49 ret
= SQLBindCol(stmt
,3,SQL_C_CHAR
,f3
,sizeof(f3
),NULL
);
50 checkrc(ret
,__LINE__
);
51 ret
= SQLBindCol(stmt
,4,SQL_C_CHAR
,f4
,sizeof(f4
),NULL
);
52 checkrc(ret
,__LINE__
);
53 ret
= SQLBindCol(stmt
,5,SQL_C_CHAR
,f5
,sizeof(f5
),NULL
);
54 checkrc(ret
,__LINE__
);
55 while(SQL_SUCCEEDED(ret
= SQLFetch(stmt
))){
56 printf("F1=%s \t F2=%s \t F3=%s \t F4= %s \t F5=%s \n ",f1
,f2
,f3
,f4
,f5
);
58 ret
= SQLCloseCursor(stmt
);
59 checkrc(ret
,__LINE__
);
61 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
62 checkrc(ret
,__LINE__
);
76 SQLSMALLINT outstrlen
;
78 // Aloocate an environment handle
79 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
80 checkrc(ret
,__LINE__
);
82 //we need odbc3 support
83 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
85 //ALLOCATE A Connection handle
86 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
87 checkrc(ret
,__LINE__
);
89 // connect to the DSN mydsn
90 ret
= SQLConnect (dbc
,
91 (SQLCHAR
*) "test", (SQLSMALLINT
) strlen ("test"),
93 (SQLSMALLINT
) strlen ("root"),
94 (SQLCHAR
*) "manager",
95 (SQLSMALLINT
) strlen (""));
97 // ret = SQLDriverConnect(dbc, NULL, (SQLCHAR*)"DSN=myodbc3", SQL_NTS, outstr, sizeof(outstr), &outstrlen,SQL_DRIVER_NOPROMPT);
99 if(SQL_SUCCEEDED(ret
))
101 printf("\nConnected to the Data Source..\n");
108 printf("error in conenction\n");
110 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
111 checkrc(ret
,__LINE__
);
113 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
114 checkrc(ret
,__LINE__
);
119 //*****************************************************
121 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
122 checkrc(ret
,__LINE__
);
125 "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT);";
127 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
128 checkrc(ret
,__LINE__
);
129 ret
= SQLExecute(stmt
);
130 checkrc(ret
,__LINE__
);
131 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"create table t2 (f1 int,primary key(f1));",SQL_NTS
);
132 checkrc(ret
,__LINE__
);
133 ret
= SQLExecute(stmt
);
134 checkrc(ret
,__LINE__
);
135 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"create table t3 (f1 int);",SQL_NTS
);
136 checkrc(ret
,__LINE__
);
137 ret
= SQLExecute(stmt
);
138 checkrc(ret
,__LINE__
);
140 printf("\nTABLE CREATED\n");
141 //***************************
142 FetchTest(env
,dbc
,stmt
);
143 //**************************************************
144 SQLCHAR drop
[100]="DROP TABLE T1";
145 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
146 checkrc(ret
,__LINE__
);
147 ret
= SQLExecute(stmt
);
148 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
150 printf("Statement failed\n");
154 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"drop table t2",SQL_NTS
);
155 checkrc(ret
,__LINE__
);
156 ret
= SQLExecute(stmt
);
157 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
159 printf("Statement failed\n");
164 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"drop table t3",SQL_NTS
);
165 checkrc(ret
,__LINE__
);
166 ret
= SQLExecute(stmt
);
167 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
169 printf("Statement failed\n");
172 printf("Tables dropped successfully\n");
174 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
175 checkrc(ret
,__LINE__
);
177 ret
= SQLDisconnect(dbc
);
178 checkrc(ret
,__LINE__
);
180 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
181 checkrc(ret
,__LINE__
);
183 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
184 checkrc(ret
,__LINE__
);