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 */
{
/* Set the version of ODBC - RDM ODBC currently only supports
ODBC version 3 */
(void) SQLSetEnvAttr (
/* Allocate a connection handle */
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);
{
rc = rdm_tfsInitialize (hTFS);
{
(void) SQLSetConnectAttr (
hCon, SQL_ATTR_RDM_CAT_BUFFER,
/* Connect to server */
rc = SQLConnect (
if (SQL_SUCCEEDED (rc))
{
/* Allocate a statement handle */
if (SQL_SUCCEEDED (rc))
{
/* Open the database */
rc = SQLExecDirect (
hStmt, (SQLCHAR *) "USE \"hello_worldODBC\"",
SQL_NTS);
if (SQL_SUCCEEDED (rc))
{
/* Create a Hello World record */
rc = SQLExecDirect (
hStmt,
(SQLCHAR *) "INSERT INTO info(myChar) "
"VALUES('Hello World!')",
SQL_NTS);
}
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. */
rc = SQLExecDirect (
hStmt,
(SQLCHAR *) "SELECT myChar FROM info",
SQL_NTS);
if (SQL_SUCCEEDED (rc))
{
/* Bind SQL fields to program variables. */
(void) SQLBindCol (
&iLen);
/* Fetch data from database into program
* variables. */
{
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. */
/* 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)
RDBC_EXPORT SQLRETURN SQLFreeHandle(SQLSMALLINT HandleType, SQLHANDLE Handle)
Frees resources associated with a specific environment, connection, statement, or descriptor handle.
RDBC_EXPORT SQLRETURN SQLDisconnect(SQLHDBC ConnectionHandle)
Closes the connection associated with a specific connection handle.
RDBC_EXPORT SQLRETURN SQLAllocHandle(SQLSMALLINT HandleType, SQLHANDLE InputHandle, SQLHANDLE *OutputHandle)
Allocates an environment, connection, statement, or descriptor handle.
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 SQLFetch(SQLHSTMT StatementHandle)
Fetches the next rowset of data from the result set and returns data for all bound columns.
Header for the Transactional File Server (TFS) API.
RDBC_EXPORT SQLRETURN SQLSetConnectAttr(SQLHDBC ConnectionHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER StringLength)
Set an attribute of the \RDBC connection.
RDBC_EXPORT SQLRETURN SQLSetEnvAttr(SQLHENV EnvironmentHandle, SQLINTEGER Attribute, SQLPOINTER Value, SQLINTEGER StringLength)
Obtain an attribute of an \RDBC environment.
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 SQLEndTran(SQLSMALLINT HandleType, SQLHANDLE Handle, SQLSMALLINT CompletionType)
Requests a commit or rollback operation for all active operations on all statements associated with a...
Header for the Raima SQL API Extensions.
#define SQL_ATTR_RDM_CAT_BUFFER
RDM specific connection attributes.
Definition: sqlrext.h:107
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.
Internal RDM Startup API used by startup macros.