1 /* CREATE TABLE "T1" WITH 10 FIELDS.
2 SQLPrimaryKeys() Should return primary key 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
= SQLPrimaryKeys(stmt
, (SQLCHAR
*)"", SQL_NTS
, (SQLCHAR
*)"", SQL_NTS
, (SQLCHAR
*)tablename
,SQL_NTS
);
37 checkrc(ret
,__LINE__
);
39 ret
= SQLNumResultCols (stmt
, &totalFields
);
40 checkrc(ret
,__LINE__
);
41 printf( "No of columns in resultset = %d\n",totalFields
);
43 ret
= SQLBindCol(stmt
,1,SQL_C_CHAR
,f1
,sizeof(f1
),NULL
);
44 checkrc(ret
,__LINE__
);
45 ret
= SQLBindCol(stmt
,2,SQL_C_CHAR
,f2
,sizeof(f2
),NULL
);
46 checkrc(ret
,__LINE__
);
47 ret
= SQLBindCol(stmt
,3,SQL_C_CHAR
,f3
,sizeof(f3
),NULL
);
48 checkrc(ret
,__LINE__
);
49 ret
= SQLBindCol(stmt
,4,SQL_C_CHAR
,f4
,sizeof(f4
),NULL
);
50 checkrc(ret
,__LINE__
);
51 SQLBindCol(stmt
, 5, SQL_C_SHORT
, &f5
, 0, NULL
);
52 checkrc(ret
,__LINE__
);
53 ret
= SQLBindCol(stmt
,6,SQL_C_CHAR
,f6
,sizeof(f6
),NULL
);
54 checkrc(ret
,__LINE__
);
55 while(SQL_SUCCEEDED(ret
= SQLFetch(stmt
))){
56 printf("catalogName%s \t SchemaName=%s \t TableName=%s \t ColumnName = %s \t KEY_SEQ=%hd\t Primarykey = %s \n ",f1
,f2
,f3
,f4
,f5
,f6
);
58 ret
= SQLCloseCursor(stmt
);
59 checkrc(ret
,__LINE__
);
61 ret
= SQLTransact(env
,dbc
,SQL_COMMIT
);
62 checkrc(ret
,__LINE__
);
75 SQLSMALLINT outstrlen
;
77 // Aloocate an environment handle
78 ret
=SQLAllocHandle(SQL_HANDLE_ENV
,SQL_NULL_HANDLE
,&env
);
79 checkrc(ret
,__LINE__
);
81 //we need odbc3 support
82 SQLSetEnvAttr(env
,SQL_ATTR_ODBC_VERSION
,(void*)SQL_OV_ODBC3
,0);
84 //ALLOCATE A Connection handle
85 ret
= SQLAllocHandle(SQL_HANDLE_DBC
,env
,&dbc
);
86 checkrc(ret
,__LINE__
);
88 // connect to the DSN mydsn
89 ret
= SQLConnect (dbc
,
90 (SQLCHAR
*) "test", (SQLSMALLINT
) strlen ("test"),
92 (SQLSMALLINT
) strlen ("root"),
93 (SQLCHAR
*) "manager",
94 (SQLSMALLINT
) strlen (""));
97 if(SQL_SUCCEEDED(ret
))
99 printf("\nConnected to the Data Source..\n");
106 printf("error in conenction\n");
108 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
109 checkrc(ret
,__LINE__
);
111 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
112 checkrc(ret
,__LINE__
);
117 //*****************************************************
119 ret
= SQLAllocHandle(SQL_HANDLE_STMT
,dbc
,&stmt
);
120 checkrc(ret
,__LINE__
);
123 "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));";
125 ret
= SQLPrepare(stmt
,table
,SQL_NTS
);
126 checkrc(ret
,__LINE__
);
127 ret
= SQLExecute(stmt
);
128 checkrc(ret
,__LINE__
);
130 printf("\nTABLE CREATED\n");
131 //***************************
132 FetchTest(env
,dbc
,stmt
,"T1");
133 //**************************************************
134 SQLCHAR drop
[100]="DROP TABLE T1";
135 ret
= SQLPrepare(stmt
,drop
,SQL_NTS
);
136 checkrc(ret
,__LINE__
);
137 ret
= SQLExecute(stmt
);
138 if(ret
!=SQL_SUCCESS
&& ret
!=SQL_SUCCESS_WITH_INFO
)
140 printf("Statement failed\n");
145 printf("Tables dropped successfully\n");
147 ret
= SQLFreeHandle(SQL_HANDLE_STMT
,stmt
);
148 checkrc(ret
,__LINE__
);
150 ret
= SQLDisconnect(dbc
);
151 checkrc(ret
,__LINE__
);
153 ret
= SQLFreeHandle(SQL_HANDLE_DBC
,dbc
);
154 checkrc(ret
,__LINE__
);
156 ret
= SQLFreeHandle(SQL_HANDLE_ENV
,env
);
157 checkrc(ret
,__LINE__
);