Contents Index Introduction Explanation of the sample program pdf/preface.pdf

UltraLite User's Guide
  Tutorial: Build an Application Using Embedded SQL

Writing source files in embedded SQL


The following sample program establishes a connection with the UltraLite CustDB sample database and executes a select query. Copy the following code into a new file and save it as sample.sqc in your c:\esqltutorial directory, or retype the material into a file.

You can also find this file as Samples\UltraLite\ESQLTutorial\sample.sqc.

#include <stdio.h>
EXEC SQL INCLUDE SQLCA;
main()
{
   /* Declare fields */
   EXEC SQL BEGIN DECLARE SECTION;
      long pid=1;
      long cost;
      char pname[31];   
   EXEC SQL END DECLARE SECTION;   
   /* Before working with data*/
   db_init(&sqlca);
   /* Connect to database */
   EXEC SQL CONNECT "DBA" IDENTIFIED BY "SQL";
   /* Fill table with data first */
   EXEC SQL INSERT INTO ULProduct(
         prod_id, price, prod_name)
      VALUES (1, 400, '4x8 Drywall x100');
   EXEC SQL INSERT INTO ULProduct (
         prod_id, price, prod_name)
      VALUES (2, 3000, '8''2x4 Studs x1000');
   EXEC SQL COMMIT;   
   /* Fetch row from database */
   EXEC SQL SELECT price, prod_name
         INTO :cost, :pname
         FROM ULProduct
         WHERE prod_id= :pid;
   /* Error handling.  If the row does not exist, 
      or if    an error occurs, -1 is returned */
   if((SQLCODE==SQLE_NOTFOUND)||(SQLCODE<0)) {
      return(-1);
   }
   /* Print query results */
   printf("Product id: %ld Price: %ld Product name: %s",
      pid, cost, pname);
   /* Preparing to exit:
   rollback any outstanding changes and disconnect */
   EXEC SQL ROLLBACK;
   EXEC SQL DISCONNECT;
   db_fini(&sqlca);
   return(0);
}
Tip 
You can configure Visual C++ to provide syntax highlighting for .sqc files, by adding ;sqc to the list of file extensions in the following registry location:
HKEY_CURRENT_USER\Software\Microsoft\DevStudio\6.0\
Text Editor\Tabs\Language Settings\C/C++\FileExtensions

Explanation of the sample program

Contents Index Introduction Explanation of the sample program pdf/preface.pdf