Products Support Documentation Download
tutorials/HelloWorld/hello_worldTutorial_main.c
/*
** HELLO WORLD
** -----------
** 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 <stdio.h>
#include "rdm.h" /* The RDM API. */
#include "hello_world_structs.h" /* The hello_world database definitions. */
#include "hello_world_cat.h" /* The hello_world database catalog */
/* Read "helloworld" row to database */
static RDM_RETCODE readARow(
RDM_DB db)
{
RDM_TABLE_ID tables[] = { TABLE_INFO };
/* Start an update transaction and lock the table */
rc = rdm_dbStartRead(db, tables, RDM_LEN(tables), NULL);
if (rc == sOKAY)
{
RDM_CURSOR cursor;
rc = rdm_dbAllocCursor(db, &cursor);
if (rc == sOKAY)
{
/* Fill a cursor with all the rows in a table */
rc = rdm_dbGetRows(db, TABLE_INFO, &cursor);
if (rc == sOKAY)
{
/* Move to the first row in the info table */
rc = rdm_cursorMoveToFirst(cursor);
if (rc == sOKAY)
{
INFO infoRead; /* Row buffer */
/* Read the full content of the current record */
rc = rdm_cursorReadRow(cursor, &infoRead, sizeof(infoRead), NULL);
if (rc == sOKAY)
{
printf("%s\n", infoRead.mychar);
}
}
}
/* Free the cursor */
rdm_cursorFree(cursor);
}
/* Free the read lock on the table */
rdm_dbEnd(db);
}
return rc;
}
/* Insert "helloworld" row to database */
static RDM_RETCODE writeARow(
RDM_DB db)
{
RDM_TABLE_ID tables[] = { TABLE_INFO };
/* Start an update transaction and lock the table */
rc = rdm_dbStartUpdate(db, tables, RDM_LEN(tables), NULL, 0, NULL);
if (rc == sOKAY)
{
INFO infoInserted; /* Row buffer */
/* Populate the mychar column in the Row buffer */
strcpy(infoInserted.mychar, "Hello World! - using the embedded TFS");
/* Insert a row into the table */
rc = rdm_dbInsertRow(db, TABLE_INFO, &infoInserted, sizeof(infoInserted), NULL);
if (rc == sOKAY)
{
/* Commit a transaction */
rc = rdm_dbEnd(db);
}
else
{
/* Abort the transaction */
}
}
return rc;
}
int32_t hello_worldTutorial_main(
int32_t argc,
const char *const *argv)
{
RDM_RETCODE rc; /* Status/Error Return Code */
RDM_TFS tfs; /* TFS Handle */
RDM_DB db; /* Database Handle */
RDM_UNREF(argc);
RDM_UNREF(argv);
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS("", &tfs);
if (rc == sOKAY)
{
rc = rdm_tfsDropDatabase(tfs, "hello_world");
if (rc == eNODB)
{
/* It is okay if there wasn't a database to drop */
rc = sOKAY;
}
if (rc == sOKAY)
{
/* Allocate a database hande */
rc = rdm_tfsAllocDatabase(tfs, &db);
if (rc == sOKAY)
{
/* Associate the catalog with the database handle */
rc = rdm_dbSetCatalog(db, hello_world_cat);
if (rc == sOKAY)
{
/* Open the database */
rc = rdm_dbOpen(db, "hello_world", RDM_OPEN_SHARED);
if (rc == sOKAY)
{
/* Insert "helloworld" row to database */
rc = writeARow(db);
if (rc == sOKAY)
{
/* Read "helloworld" row to database */
rc = readARow(db);
}
}
else
{
fprintf(stderr, "\nSorry, I can't open the hello_world database.\n");
}
}
}
}
}
if (rc != sOKAY)
{
printf("There was an error in this Tutorial (%s - %s)\n",
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE_CR(hello_worldTutorial)