All:
   After perusing the archives for a while, I was unable to find where
anyone had posted a working example for SQLDBC usage. I also had trouble
finding anything useful in the docs. As follows is a simple class that works
like a charm.

Sean

sapdb.h
___________________

//Header file for SAPDB Access

//(c) 2004 Sean Mollet

//You are free to use these as you like

//Just please give me some credit

#include "WINDOWS.H"

#include "sql.h"

#include "sqlext.h"

class dbaccess{

public:

dbaccess();

~dbaccess();

int connect();

int execute(const char *statement,...);

int checkerror();

int fetchrow();

UCHAR DSN[SQL_MAX_DSN_LENGTH]; // Data Source Name buffer

UCHAR UID[10]; // User ID buffer

UCHAR Passwd[10]; // Password buffer

HENV hEnv; // Env Handle from SQLAllocEnv()

HDBC hDBC; // Connection handle

HSTMT hStmt; // Statement handle

char **resultset; //result set

int currentrow; //currently active row

int columncount; //number of columbs in result set

RETCODE retcode; // Return code

SQLCHAR *error; // Buffer to hold error text

SQLCHAR *sql;

};



sapdb.cpp

_____________________

//Class file for SapDB access

//(c) 2004 Sean Mollet

#include "sapdb.h"

#include "stdio.h"

#include "stdlib.h"

#include "stdarg.h"

//Constructor

dbaccess::dbaccess(){

// Allocate memory for ODBC Environment handle

SQLAllocEnv (&hEnv);

// Allocate memory for the connection handle

SQLAllocConnect (hEnv, &hDBC);

//Allocate memory for our error string

error = (SQLCHAR *)malloc(255);

//clear these variables

currentrow=0;

columncount=0;

}

//Destructor

dbaccess::~dbaccess(){

// Free the allocated statement handle

if(hStmt != NULL){

SQLFreeStmt (hStmt, SQL_DROP);

}

// Disconnect from datasource

SQLDisconnect (hDBC);

// Free the allocated connection handle

SQLFreeConnect (hDBC);

// Free the allocated ODBC environment handle

SQLFreeEnv (hEnv);

//Free our error buffer

free(error);

//free the resultset

for(int a=1;a<=columncount;a++){

free(resultset[a]);

}

free(resultset);

}

// Connect to the data source

int dbaccess::connect(){

retcode = SQLConnect (hDBC, DSN,SQL_NTS, UID, SQL_NTS, Passwd, SQL_NTS);

return(retcode);

};

//Execute a SQL statement

int dbaccess::execute(const char *statement,...){

va_list ap;

va_start(ap,statement);


sql = (SQLCHAR *) malloc(5000);

vsprintf((char *)sql,statement,ap);

// Allocate memory for the statement handle

retcode = SQLAllocStmt (hDBC, &hStmt);

// Prepare the SQL statement by assigning it to the statement handle

retcode = SQLPrepare (hStmt,sql, SQL_NTS);

free(sql); // we don't need this anymore

if(retcode!=0){

return retcode;

}

// Execute the resulting handle

retcode = SQLExecute (hStmt);

if(retcode==0){//statement executed properly

SQLNumResultCols(hStmt,(SQLSMALLINT *) &columncount);

}

//create the results array

resultset = (char **) malloc(sizeof(char *)*columncount);

for(int a=1;a<=columncount;a++){

resultset[a]=(char *) malloc(sizeof(char *)*255);

retcode = SQLBindCol (hStmt, a, SQL_C_CHAR, resultset[a], 255,NULL);

}

return retcode;

}

int dbaccess::fetchrow(){

return SQLFetch(hStmt);

}



//Handle any errors that might come along

int dbaccess::checkerror(){

if(retcode!=0){//there was an error

SQLError(hEnv,hDBC,hStmt,NULL,NULL,error,255,NULL);

return 1;

}

return 0;

}





-- 
MaxDB Discussion Mailing List
For list archives: http://lists.mysql.com/maxdb
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to