#include <sqlext.h>
#include <stdio.h>
#pragma comment (lib, "libodbc32.a")
int main(void)
{
short anzcols;
HENV hEnv = NULL;
// Env Handle from SQLAllocEnv()
HDBC hDBC = NULL;
// Connection handle
HSTMT hStmt = NULL;
// Statement handle
//UCHAR szDSN[200] = "michelDB";
UCHAR szDSNless[200] = "DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=.\\DLDB.mdb;";
printf( "'%s'\n", szDSNless );
// Data Source Name buffer
//UCHAR* szUID = NULL;
// User ID buffer
//UCHAR* szPasswd = NULL;
// Password buffer
UCHAR cols[10][128];
// Model buffer
SDWORD cbModel;
// Model buffer bytes recieved
char szSqlStr[250];
// SQL string
RETCODE retcode;
// Return code
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "db97" using userid and password.
//retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);
retcode = SQLDriverConnect(hDBC, NULL, (SQLCHAR *)szDSNless,
SQL_NTS, (SQLCHAR *)szDSNless, sizeof(szDSNless), NULL, SQL_DRIVER_NOPROMPT);
printf( "retcode: %ld, SQL_SUCCESS: %ld\n", retcode, SQL_SUCCESS );
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
while ( 1 ){
printf ( "SQL> " );
memset( szSqlStr, 0, sizeof szSqlStr );
gets( szSqlStr );
if( !strcmp( szSqlStr, "by" ) ){
return 0;
}
// Allocate memory for the statement handle
retcode = SQLAllocStmt (hDBC, &hStmt);
retcode = SQLPrepare (hStmt, (unsigned char*)szSqlStr, sizeof (szSqlStr));
// Execute the SQL statement handle
retcode = SQLExecute (hStmt);
if( retcode ){
printf( "execute retcode %ld\n", retcode );
}
if( strstr( szSqlStr, "select" ) ){
SQLNumResultCols( hStmt, &anzcols );
printf( "Anzahl der Spalten %ld\n", anzcols );
memset( cols, 0, sizeof cols );
for( int i=0;i<anzcols;i++ ){
// Project only column 1 which is the models
SQLBindCol (hStmt, i+1, SQL_C_CHAR, cols[i], sizeof(cols[i]), &cbModel);
}
// Get row of data from the result set defined above in the statement
retcode = SQLFetch (hStmt);
while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
for( int i=0;i<anzcols;i++ ){
printf("\t%s",cols[i]);
// Print row (model)
}
memset( cols, 0, sizeof cols );
retcode = SQLFetch (hStmt);
// Fetch next row from result set
printf( "\n" );
}
}
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);
}
// Disconnect from datasource
SQLDisconnect (hDBC);
}
// Free the allocated connection handle
SQLFreeConnect (hDBC);
// Free the allocated ODBC environment handle
SQLFreeEnv (hEnv);
return 0;
}