cancel
Showing results for 
Search instead for 
Did you mean: 

Need one or two example programs using SQLDBC

Former Member
0 Kudos

We have an application which we are porting to support MaxDB as back end. Currently the application supports MySQL,Sybase,Oracle and DB2.

The application is using only external libraries of C format for all other databases and there is established framework for using this. So, for MaxDB also we are planning to use the sqldbc_c.so and it's C format functions.

However one problem i am facing as a developer is, there are hardly any examples available on the web using SQLDBC and C/C++. The only one i could get my hands on is the one shipped with the product: HelloMaxDB.cpp.

Can someone help me with few example programs.

Regards

Raj

Accepted Solutions (0)

Answers (1)

Answers (1)

TTK
Employee
Employee
0 Kudos

Hello Raj

Did you consider to use ODBC? If you intend to have some generic code, which can be shared between different implementations for different databases, surely ODBC is the better choice. Actually ODBC applications can be very generic.

For SQLDBC some example code snippets are in the documentation for Statement, PreparedStatement and ResultSet.

The HelloWorld example demonstrates how to connect, sending an SQL-query and reading data from the result set. Of course, the example is very simple (e.g. releasing resources is not included).

HTH & regards Thomas

Former Member
0 Kudos

Hi Thomas,

Yes we did consider using ODBC. But historically our application has always used the native libraries provided by specific DB's for such work, like we use native OCI libraries for oracle. Also we prefer using the C standard library version over C++. These are the reasons for which we have decided to use libSQLDBC_C over libSQLDBC.

The example file HelloWorld.cpp provided with community edition of MaxDB is using the C++ standard library libSQLDBC and hence not serving our purpose.

However, i am trying to modify this example program as per my requirement by following the SQLDBC_C.h file where all the function call signatures are present.

Regards

Raj

Edited by: Raja Panda on Sep 5, 2008 10:55 AM

TTK
Employee
Employee
0 Kudos

Hello Raj

The C-version of SQLDBC is mainly, that the name of the class is added as prefix to the method name, and the pointer to the object is the first argument of the method call. I.e., that the SQLDBC documentation serves for the C-version, too.

Surely you have discovered this in examining the header file.

FYI, the ODBC driver (77ff) is only a thin layer over SQLDBC. Therefore, you could still consider ODBC as C-api.

Regards Thomas

Former Member
0 Kudos

Well,

I did some trial and error and have been successful in converting the SQLDBC - Helloworld.cpp example to use the SQLDB_C library. This ensures that it uses C standard functions instead of the default C++ standard.

Please remember you have to change the Makefile too, to link to the SQLDBC_C instead of SQLDBC and the header file too becomes SQLDBC_C.h instead of SQLDBC.h

I am putting the modified program here, hope it will help someone someday:

#include "SQLDBC_C.h"

#include "stdio.h"

#include "string.h"

typedef struct ConnectArgsT {

char * username;

char * password;

char * dbname;

char * host;

} ConnectArgsT;

static void parseArgs (ConnectArgsT * connectArgs, int argc, char **argv);

// org - using namespace SQLDBC;

/*

  • Let start your program with a main function

*/

int main(int argc, char *argv[])

{

ConnectArgsT connectArgs;

parseArgs (&connectArgs, argc, argv);

char errorText[200];

/*

  • Every application has to initialize the SQLDBC library by getting a

  • reference to the ClientRuntime and calling the SQLDBC_Environment constructor.

*/

SQLDBC_IRuntime *runtime;

// org - runtime = SQLDBC::GetClientRuntime(errorText, sizeof(errorText));

runtime = ClientRuntime_GetClientRuntime(errorText, (SQLDBC_Int4) sizeof(errorText));

if (!runtime) {

fprintf(stderr, "Getting instance of the ClientRuntime failed %s", errorText);

return (1);

}

// org - SQLDBC_Environment env(runtime);

SQLDBC_Environment* env = SQLDBC_Environment_new_SQLDBC_Environment(runtime);

if (!env) {

fprintf(stderr, "Getting Environment of the SQLDBC failed %s", errorText);

return (1);

}

/*

  • Create a new connection object and open a session to the database.

*/

// org - SQLDBC_Connection *conn = env.createConnection();

SQLDBC_Connection *conn = SQLDBC_Environment_createConnection(env);

if (!conn) {

fprintf(stderr, "Getting Connection object failed ");

return (1);

}

SQLDBC_Retcode rc;

// org - rc = conn->connect(connectArgs.host ,connectArgs.dbname, connectArgs.username, connectArgs.password);

// For encoding

SQLDBC_StringEncodingType_Encoding encodAsciiType = SQLDBC_StringEncodingType_Encoding_Ascii;

SQLDBC_ConnectProperties* connpros = SQLDBC_ConnectProperties_new_SQLDBC_ConnectProperties();

rc = SQLDBC_Connection_connect(conn, connectArgs.host, (SQLDBC_Length) strlen(connectArgs.host),

connectArgs.dbname, (SQLDBC_Length) strlen(connectArgs.dbname),

connectArgs.username, (SQLDBC_Length) strlen(connectArgs.username),

connectArgs.password, (SQLDBC_Length) strlen(connectArgs.password),

encodAsciiType,connpros);

if(SQLDBC_OK != rc) {

//fprintf(stderr, "Connecting to the database failed %s", conn->error().getErrorText());

SQLDBC_ErrorHndl *errHndl = SQLDBC_Connection_getError(conn);

fprintf(stderr, "Connecting to the database failed %s", SQLDBC_ErrorHndl_getErrorText(errHndl));

return (1);

}

/*

  • Create a new statment object and execute it.

*/

// org - SQLDBC_Statement *stmt = conn->createStatement();

SQLDBC_Statement *stmt = SQLDBC_Connection_createStatement(conn);

// org - rc = stmt->execute("SELECT 'Hello SAPDB' from DUAL");

rc = SQLDBC_Statement_execute(stmt,"SELECT 'Hello SAPDB' from DUAL", sizeof("SELECT 'Hello SAPDB' from DUAL"),encodAsciiType);

if(SQLDBC_OK != rc) {

// org - fprintf(stderr, "Execution failed %s", stmt->error().getErrorText());

fprintf(stderr, "Execution failed ");

return (1);

}

/*

  • Check if the SQL command return a resultset and get a result set object.

*/

SQLDBC_ResultSet *result;

// org - result = stmt->getResultSet();

result = SQLDBC_Statement_getResultSet(stmt);

if(!result) {

// org - fprintf(stderr, "SQL command doesn't return a result set %s", stmt->error().getErrorText());

fprintf(stderr, "SQL command doesn't return a result set");

return (1);

}

/*

  • Position the curors within the resultset by doing a fetch next call.

*/

//org - rc = result->next();

rc = SQLDBC_ResultSet_next(result);

if(SQLDBC_OK != rc) {

//fprintf(stderr, "Error fetching data %s", stmt->error().getErrorText());

fprintf(stderr, "Error fetching data ");

return (1);

}

char szString[30];

SQLDBC_Length ind;

/*

  • Get a string value from the column.

*/

// org - rc = result->getObject(1, SQLDBC_HOSTTYPE_ASCII, szString, &ind, sizeof(szString));

rc = SQLDBC_ResultSet_getObject(result, 1, SQLDBC_HOSTTYPE_ASCII, szString,&ind, sizeof(szString),SQLDBC_TRUE);

if(SQLDBC_OK != rc) {

// org - fprintf(stderr, "Error getObject %s", stmt->error().getErrorText());

return (1);

}

printf("%s\n", szString);

/*

  • Finish your program with a returncode.

*/

return 0;

}

static void parseArgs (ConnectArgsT * connectArgs, int argc, char **argv)

{

/*

  • setting defaults for demo database

*/

connectArgs->username = "MONA";

connectArgs->password = "RED";

connectArgs->dbname = "MAXDB1";

connectArgs->host = "";

/*

  • use values from command line

*/

if (argc > 4) {

connectArgs->host = argv [4];

}

if (argc > 3) {

connectArgs->dbname = argv [3];

}

if (argc > 2) {

connectArgs->password = argv [2];

}

if (argc > 1) {

connectArgs->username = argv [1];

}

}

Former Member
0 Kudos

Hi Thomas,

Yup. I just saw your reply too. Thanks for the help.