/* This sample program shows how to create a database record. */
/* define PURE_SQL to use a SQL INSERT INTO statement instead of */
/* DBCreateRecord and DBPutRecord. */
#undef PURE_SQL
#include "cvi_db.h"
#include <ansi_c.h>
#include <userint.h>
void ShowError()
{
MessagePopup("Database Error",DBErrorMessage());
}
void main()
{
int hdbc = 0; /* Handle to database connection */
int hmap = 0; /* Handle to map */
int hstmt = 0; /* Handle to SQL statement */
char uutNum[11]; /* Buffer for uut serial number */
int uutStat; /* Status variable for uut number */
double meas1; /* Variable for test measurement 1 */
int meas1Stat; /* Fetch status for measurement 1 */
double meas2; /* Variable for test measurement 2 */
int meas2Stat; /* Fetch tatus for measurement 2 */
int resCode; /* Result code */
/* Connect to data source (in this case dBase files) */
hdbc = DBConnect ("DSN=CVI SQL 2.0 Samples");
if (hdbc <= 0) {ShowError(); goto Error;}
#ifdef PURE_SQL
resCode = DBImmediateSQL(hdbc, "INSERT INTO TESTRES (UUT_NUM, MEAS1, MEAS2) \
VALUES ('2860B567',0.7,1.1)");
#else
/* begin map for constructed SQL statement */
hmap = DBBeginMap (hdbc);
if (hmap <= 0) {ShowError(); goto Error;}
/* specify the columns to be selected and the variables where column */
/* values will be placed. */
resCode = DBMapColumnToChar (hmap, "UUT_NUM", 11, uutNum, &uutStat, "");
if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
resCode = DBMapColumnToDouble (hmap, "MEAS1", &meas1, &meas1Stat);
if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
resCode = DBMapColumnToDouble (hmap, "MEAS2", &meas2, &meas2Stat);
if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
/* Activate the map for table testres. (construct a SQL Select */
/* statement, execute the statement, bind the selected columns to */
/* the previously specified variables.) */
hstmt = DBActivateMap (hmap, "testres");
if (hstmt <= 0) {ShowError(); goto Error;}
/* Create the new record */
resCode = DBCreateRecord (hstmt);
if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
/* Put values into the bound variables */
strcpy(uutNum, "2860B567");
meas1 = 0.7;
meas2 = 1.1;
/* Insert the record into the database */
resCode = DBPutRecord (hstmt);
if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
resCode = DBDeactivateMap (hmap);
if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
#endif
resCode = DBDisconnect (hdbc);
if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
MessagePopup ("Create Record Sample",
"Successfully created record for UUT_NUM 2860B567");
return;
Error:
return;
}