1 /* CREATE TABLE "T1" WITH 10 FIELDS.
2 SQLStatistics() Should return all Index information
12 //*************************************************************************
13 // ERROR CHECK FUNCTION
14 inline void checkrc(int rc
,int line
)
18 printf("ERROR %d at line %d\n",rc
,line
);
23 //*************************************************************************
24 // FUNCTION FOR INSERTING ROWS IN IT.
26 int FetchTest(SQLHANDLE env
, SQLHANDLE dbc
, SQLHANDLE stmt
,char *tablename
)
36 ret
= SQLStatistics(stmt
, NULL
, 0, NULL
, SQL_NTS
,
37 (SQLCHAR
*) tablename
, SQL_NTS
, SQL_INDEX_ALL
, SQL_QUICK
);
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
,4,SQL_C_SHORT
,(void*)&f1
,0,NULL
);
46 checkrc(ret
,__LINE__
);
47 ret
= SQLBindCol(stmt
,6,SQL_C_CHAR
,f2
,sizeof(f2
),NULL
);
48 checkrc(ret
,__LINE__
);
49 ret
= SQLBindCol(stmt
,7,SQL_C_SHORT
,(void *)&f3
,0,NULL
);
50 checkrc(ret
,__LINE__
);
51 ret
= SQLBindCol(stmt
,9,SQL_C_CHAR
,f4
,sizeof(f4
),NULL
);
52 checkrc(ret
,__LINE__
);
53 while(SQL_SUCCEEDED(ret
= SQLFetch(stmt
))){
54 printf(" Unique =%hd \tIndexName = %s \t Type=%hd ColumnName = %s \n ",f1
,f2
,f3
,f4
);
56 ret
= SQLCloseCursor(stmt
);
57 checkrc(ret
,__LINE__
);
59 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
60 checkrc(ret
,__LINE__
);
73 SQLSMALLINT outstrlen
;
75 // Aloocate an environment handle
76 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
77 checkrc(ret
,__LINE__
);
79 //we need odbc3 support
80 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
82 //ALLOCATE A Connection handle
83 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
84 checkrc(ret
,__LINE__
);
86 // connect to the DSN mydsn
87 ret
= SQLConnect (dbc
,
88 (SQLCHAR
*) "test", (SQLSMALLINT
) strlen ("test"),
90 (SQLSMALLINT
) strlen ("root"),
91 (SQLCHAR
*) "manager",
92 (SQLSMALLINT
) strlen (""));
95 if(SQL_SUCCEEDED(ret
))
97 printf("\nConnected to the Data Source..\n");
104 printf("error in conenction\n");
106 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
107 checkrc(ret
,__LINE__
);
109 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
110 checkrc(ret
,__LINE__
);
115 //*****************************************************
117 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
118 checkrc(ret
,__LINE__
);
121 "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT, PRIMARY KEY(F2));";
123 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
124 checkrc(ret
,__LINE__
);
125 ret
= SQLExecute(stmt
);
126 checkrc(ret
,__LINE__
);
127 ret
= SQLPrepare(stmt
,(SQLCHAR
*)"CREATE INDEX INDS ON T1(F3) TREE;",SQL_NTS
);
128 checkrc(ret
,__LINE__
);
129 ret
= SQLExecute(stmt
);
130 checkrc(ret
,__LINE__
);
132 printf("\nTABLE CREATED\n");
133 //***************************
134 FetchTest(env
,dbc
,stmt
,"T1");
135 //**************************************************
136 SQLCHAR drop
[100]="DROP TABLE T1";
137 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
138 checkrc(ret
,__LINE__
);
139 ret
= SQLExecute(stmt
);
140 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
142 printf("Statement failed\n");
147 printf("Tables dropped successfully\n");
149 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
150 checkrc(ret
,__LINE__
);
152 ret
= SQLDisconnect(dbc
);
153 checkrc(ret
,__LINE__
);
155 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
156 checkrc(ret
,__LINE__
);
158 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
159 checkrc(ret
,__LINE__
);