1 /* Use Connection String "DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;" for Connect Data Source
2 * CREATE TABLE "T1" WITH 10 FIELDS.
3 CREATE TABLE "T2" WITH 10 FIELDS.
4 CREATE TABLE "T3" WITH 10 FIELDS.
5 SQLTables() Should return all table information
6 CatalogName="csql", SchemaName="csql", TableType="table"
15 //*************************************************************************
16 // ERROR CHECK FUNCTION
17 inline void checkrc(int rc
,int line
)
21 printf("ERROR %d at line %d\n",rc
,line
);
26 //*************************************************************************
27 // FUNCTION FOR INSERTING ROWS IN IT.
29 int FetchTest(SQLHANDLE env
, SQLHANDLE dbc
, SQLHANDLE stmt
)
38 ret
= SQLTables(stmt
, (SQLCHAR
*)"csql", SQL_NTS
, (SQLCHAR
*)"csql", SQL_NTS
, (SQLCHAR
*)"", SQL_NTS
, (SQLCHAR
*)"Table", SQL_NTS
);
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("\tF1=%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
*) "DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT
) strlen ("DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;"),
93 (SQLSMALLINT
) strlen ("root"),
94 (SQLCHAR
*) "manager",
95 (SQLSMALLINT
) strlen (""));
98 if(SQL_SUCCEEDED(ret
))
100 printf("\nConnected to the Data Source..\n");
107 printf("error in conenction\n");
109 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
110 checkrc(ret
,__LINE__
);
112 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
113 checkrc(ret
,__LINE__
);
118 //*****************************************************
120 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
121 checkrc(ret
,__LINE__
);
124 "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT);";
126 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
127 checkrc(ret
,__LINE__
);
128 ret
= SQLExecute(stmt
);
129 checkrc(ret
,__LINE__
);
130 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"create table t2 (f1 int,primary key(f1));",SQL_NTS
);
131 checkrc(ret
,__LINE__
);
132 ret
= SQLExecute(stmt
);
133 checkrc(ret
,__LINE__
);
134 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"create table t3 (f1 int);",SQL_NTS
);
135 checkrc(ret
,__LINE__
);
136 ret
= SQLExecute(stmt
);
137 checkrc(ret
,__LINE__
);
139 printf("\nTABLE CREATED\n");
140 //***************************
141 FetchTest(env
,dbc
,stmt
);
142 //**************************************************
143 SQLCHAR drop
[100]="DROP TABLE T1";
144 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
145 checkrc(ret
,__LINE__
);
146 ret
= SQLExecute(stmt
);
147 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
149 printf("Statement failed\n");
153 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"drop table t2",SQL_NTS
);
154 checkrc(ret
,__LINE__
);
155 ret
= SQLExecute(stmt
);
156 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
158 printf("Statement failed\n");
163 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"drop table t3",SQL_NTS
);
164 checkrc(ret
,__LINE__
);
165 ret
= SQLExecute(stmt
);
166 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
168 printf("Statement failed\n");
171 printf("Tables dropped successfully\n");
173 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
174 checkrc(ret
,__LINE__
);
176 ret
= SQLDisconnect(dbc
);
177 checkrc(ret
,__LINE__
);
179 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
180 checkrc(ret
,__LINE__
);
182 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
183 checkrc(ret
,__LINE__
);