#include "SQLDBC.h"
#include <stdio.h>
typedef struct ConnectArgsT {
char * username;
char * password;
char * dbname;
char * host;
} ConnectArgsT;
static void parseArgs (ConnectArgsT * connectArgs, int argc, char **argv);
using namespace SQLDBC;
int main(int argc, char *argv[])
{
ConnectArgsT connectArgs;
parseArgs (&connectArgs, argc, argv);
char errorText[200];
SQLDBC_IRuntime *runtime;
runtime = SQLDBC::GetClientRuntime(errorText, sizeof(errorText));
if (!runtime) {
fprintf(stderr, "Getting instance of the ClientRuntime failed %s", errorText);
return (1);
}
SQLDBC_Environment env(runtime);
SQLDBC_Connection *conn = env.createConnection();
SQLDBC_Retcode rc;
rc = conn->connect(connectArgs.host, connectArgs.dbname,
connectArgs.username, connectArgs.password);
if(SQLDBC_OK != rc) {
fprintf(stderr, "Connecting to the database failed %s", conn->error().getErrorText());
return (1);
}
printf("Sucessfull connected to %s as user %s\n",
connectArgs.dbname, connectArgs.username);
SQLDBC_Statement *stmt = conn->createStatement();
rc = stmt->execute("SELECT 'Hello world' from DUAL");
if(SQLDBC_OK != rc) {
fprintf(stderr, "Execution failed %s", stmt->error().getErrorText());
return (1);
}
SQLDBC_ResultSet *result;
result = stmt->getResultSet();
if(!result) {
fprintf(stderr, "SQL command doesn't return a result set %s", stmt->error().getErrorText());
return (1);
}
rc = result->next();
if(SQLDBC_OK != rc) {
fprintf(stderr, "Error fetching data %s", stmt->error().getErrorText());
return (1);
}
char szString[30];
SQLDBC_Length ind;
rc = result->getObject(1, SQLDBC_HOSTTYPE_ASCII, szString, &ind, sizeof(szString));
if(SQLDBC_OK != rc) {
fprintf(stderr, "Error getObject %s", stmt->error().getErrorText());
return (1);
}
printf("%s\n", szString);
return 0;
}
static void parseArgs (ConnectArgsT * connectArgs, int argc, char **argv)
{
connectArgs->username = (char*)"TEST";
connectArgs->password = (char*)"TEST";
connectArgs->dbname = (char*)"TST";
connectArgs->host = (char*)"localhost";
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];
}
}