odbc01Example_main.c

TBD: Update GenDefines.txt with a $DESCRIPTION for this example. This example needs a compiled schema, hello_worldODBC.sdl.

/*
** HELLO WORLD SQL
** ---------------
** This document describes the process to create a simple database, insert
** a record containing a text field, read the text field from database and
** print it out.
*/
#include "sqlrext.h" /* The RDM SQL API. */
/*lint -e838 */
#include "hello_worldODBC_cat.h" /* Contains the database definition */
#include "rdmtfsapi.h"
#include "rdmstartupapi.h"
#include <stdio.h>
#define SQL_EMPSTR ((SQLCHAR *) "") /* any string */
int32_t main_hello_world_ODBCTutorial (int32_t argc, const char *const *argv)
{
SQLRETURN rc; /* holds return value, 0 is success */
SQLCHAR sz[32];
SQLHDBC hCon; /* connection handle */
SQLHSTMT hStmt; /* statement handle */
SQLHENV hEnv; /* environment handle */
SQLLEN iLen = 0;
RDM_UNREF (argc);
RDM_UNREF (argv);
/* Allocate an environment handle */
if (SQL_SUCCEEDED (rc))
{
/* Set the version of ODBC - RDM ODBC currently only supports
ODBC version 3 */
(void) SQLSetEnvAttr (
/* Allocate a connection handle */
rc = SQLAllocHandle (SQL_HANDLE_DBC, hEnv, &hCon);
if (SQL_SUCCEEDED (rc))
{
/* RDM-specific ODBC extension to specify the catalog to be used
when opening the database */
RDM_TFS hTFS;
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS (&hTFS);
if (rc == sOKAY)
{
rc = rdm_tfsInitialize (hTFS);
{
(SQLPOINTER) hello_worldODBC_cat, SQL_NTS);
/* Connect to server */
rc = SQLConnect (
if (SQL_SUCCEEDED (rc))
{
/* Allocate a statement handle */
rc = SQLAllocHandle (SQL_HANDLE_STMT, hCon, &hStmt);
if (SQL_SUCCEEDED (rc))
{
/* Open the database */
hStmt, (SQLCHAR *) "USE \"hello_worldODBC\"",
if (SQL_SUCCEEDED (rc))
{
/* Create a Hello World record */
hStmt,
(SQLCHAR *) "INSERT INTO info(myChar) "
"VALUES('Hello World!')",
}
if (SQL_SUCCEEDED (rc))
{
/* Commit the insert, transaction is started by
the first insert statement. */
rc = SQLEndTran (
if (!SQL_SUCCEEDED (rc))
{
fprintf (
stderr,
"Sorry, I can't commit my changes "
"to the database.");
}
}
else
{
(void) SQLEndTran (
}
if (SQL_SUCCEEDED (rc))
{
/* Query the database for the record created. */
hStmt,
(SQLCHAR *) "SELECT myChar FROM info",
if (SQL_SUCCEEDED (rc))
{
/* Bind SQL fields to program variables. */
(void) SQLBindCol (
hStmt, 1, SQL_C_CHAR, sz, sizeof (sz),
&iLen);
/* Fetch data from database into program
* variables. */
if (SQLFetch (hStmt) == SQL_SUCCESS)
{
printf ("%s\n", sz);
}
else
{
fprintf (
stderr,
"Sorry, I can't fetch any rows "
"of data from the database.");
}
}
else
{
fprintf (
stderr,
"Sorry, I can't execute the query "
"statement.");
}
}
}
/* Free the SQL statement handle. */
(void) SQLFreeHandle (SQL_HANDLE_STMT, hStmt);
/* Close the database. */
(void) SQLDisconnect (hCon);
}
else
{
fprintf (
stderr,
"Sorry, I can't allocate a SQL statement handle.");
}
}
(void) rdm_tfsFree (hTFS);
}
}
else
{
fprintf (stderr, "Sorry, I can't open the hello_world database.");
}
/* Free the database handles. */
(void) SQLFreeHandle (SQL_HANDLE_DBC, hCon);
(void) SQLFreeHandle (SQL_HANDLE_ENV, hEnv);
}
else
{
fprintf (stderr, "Sorry, I can't allocate a database handle.");
}
return SQL_SUCCEEDED (rc) ? 0 : 1;
}
RDM_STARTUP_EXAMPLE (hello_world_ODBCTutorial)
#define SQL_OV_ODBC3
Definition: sqlext.h:70
RDBC_EXPORT SQLRETURN SQLFetch(SQLHSTMT StatementHandle)
Fetches the next rowset of data from the result set and returns data for all bound columns.
#define SQL_ATTR_ODBC_VERSION
Definition: sqlext.h:47
void * SQLPOINTER
Definition: sqltypes.h:66
unsigned char SQLCHAR
Definition: sqltypes.h:53
RDBC_EXPORT SQLRETURN SQLSetConnectAttr(SQLHDBC ConnectionHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER StringLength)
Set an attribute of the SQL API connection.
RDM_RETCODE rdm_rdmAllocTFS(RDM_TFS *phTFS)
Allocate a TFS handle.
#define SQL_ATTR_RDM_TFS_HANDLE
Definition: sqlrext.h:119
RDBC_EXPORT SQLRETURN SQLConnect(SQLHDBC ConnectionHandle, const SQLCHAR *ServerName, SQLSMALLINT NameLength1, const SQLCHAR *UserName, SQLSMALLINT NameLength2, const SQLCHAR *Authentication, SQLSMALLINT NameLength3)
Establishes connections to a driver and a data source.
RDBC_EXPORT SQLRETURN SQLSetEnvAttr(SQLHENV EnvironmentHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER StringLength)
Obtain an attribute of an SQL API environment.
RDBC_EXPORT SQLRETURN SQLAllocHandle(SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE *OutputHandle)
Allocates an environment, connection, statement, or descriptor handle.
#define SQL_HANDLE_ENV
Definition: sql.h:82
#define SQL_COMMIT
Definition: sql.h:286
#define SQL_NTS
Definition: sql.h:64
RDBC_EXPORT SQLRETURN SQLExecDirect(SQLHSTMT StatementHandle, const SQLCHAR *StatementText, SQLINTEGER TextLength)
Executes a preparable statement, using the current values of the parameter marker variables if any pa...
RDBC_EXPORT SQLRETURN SQLDisconnect(SQLHDBC ConnectionHandle)
Closes the connection associated with a specific connection handle.
@ sOKAY
Definition: rdmretcodetypes.h:97
#define SQL_SUCCEEDED(rc)
Definition: sql.h:58
Header for the Transactional File Server (TFS) API.
#define RDM_STARTUP_EXAMPLE(name)
Definition: rdmstartuptypes.h:73
RDBC_EXPORT SQLRETURN SQLBindCol(SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLPOINTER TargetValue, SQLINTEGER BufferLength, SQLINTEGER *StrLen_or_Ind)
Binds application data buffers to columns in the result set.
RDBC_EXPORT SQLRETURN SQLEndTran(SQLSMALLINT HandleType, SQLHANDLE Handle, SQLSMALLINT CompletionType)
Requests a commit or rollback operation for all active operations on all statements associated with a...
#define RDM_UNREF(a)
Definition: psptypes.h:45
#define SQL_HANDLE_DBC
Definition: sql.h:83
#define SQL_SUCCESS
SQL API Return Code.
Definition: sql.h:43
RDBC_EXPORT SQLRETURN SQLFreeHandle(SQLSMALLINT HandleType, SQLHANDLE Handle)
Frees resources associated with a specific environment, connection, statement, or descriptor handle.
#define SQL_HANDLE_STMT
Definition: sql.h:84
Header for the Raima SQL API Extensions.
struct RDM_TFS_S * RDM_TFS
RDM TFS Handle.
Definition: rdmtfstypes.h:21
#define SQL_IS_POINTER
Definition: sqlext.h:243
RDM_RETCODE rdm_tfsFree(RDM_TFS hTFS)
Terminate a TFS service.
#define SQL_ATTR_RDM_CAT_BUFFER
RDM specific connection attributes.
Definition: sqlrext.h:107
#define SQL_C_CHAR
Definition: sqlext.h:445
#define SQL_EMPSTR
Definition: sqlrtypes.h:27
#define SQL_NULL_HANDLE
Definition: sql.h:301
int16_t SQLRETURN
Definition: sqltypes.h:67
#define SQL_ROLLBACK
Definition: sql.h:287
Internal RDM Startup API used by startup macros.
RDM_RETCODE rdm_tfsInitialize(RDM_TFS tfs)
Initialize a RDM_TFS instance.
#define SQLLEN
Definition: sqltypes.h:82