Contents Index Lesson 3: Run the UltraLite generator Lesson 5: Build and run your application pdf/preface.pdf

UltraLite User's Guide
  Tutorial: Build an Application Using the C++ API

Lesson 4: Write the application source code


Copy and paste the following source code into a file named sample.cpp in your tutorial directory. You can find this source code in Samples\UltraLite\APITutorial\sample.cpp, although you may have to edit the file to uncomment the inserts.

The code does not contain error checking or other features that you would require in a complete application. It is provided as a simplified application, for illustrative purposes only.

// (1) include headers
#include <stdio.h>
#include "ProductPubAPI.hpp"

void main() {
   // (2) declare variables
   long price;
   ULData db;
   ULConnection conn;
   ULProduct productTable;

   // (3) connect to the UltraLite database
   db.Open() ;
       conn.Open( &db, "dba", "sql" );
   productTable.Open( &conn );

   // (4) insert sample data
   productTable.SetProd_id( 1 );
   productTable.SetPrice( 400 );
   productTable.SetProd_name( "4x8 Drywall x100" );
   productTable.Insert();
   
   productTable.SetProd_id( 2 );
   productTable.SetPrice( 3000 );
   productTable.SetProd_name( "8' 2x4 Studs x1000" );
   productTable.Insert();

   // (5) Write out the price of the items
   productTable.BeforeFirst();
   while( productTable.Next() ) {
      productTable.GetPrice( price );
      printf("Price: %d\n", price );
   }

   // (6) close the objects to finish
   productTable.Close();
   conn.Close();
   db.Close();
}
Explanation 

The numbered comments in the code indicate the main tasks this routine carries out:

  1. Include headers.

    In addition to stdio.h, you need to include the generated header file ProductPubAPI.hpp to include the generated classes describing the Product table. This file in turn includes the UltraLite header file ulapi.h.

  2. Declare variables.

    The UltraLite database is declared as an instance of class ULData, and the connection to the database is an instance of class ULConnection. These classes are included from ulapi.h.

    The table is declared as an instance of class ULProduct, a generated name derived from the name of the table in the reference database.

  3. Connect to the database.

    Opening each of the declared objects establishes access to the data. Opening the database requires no arguments; opening a connection requires a user ID and password, and also the name of the database. Opening the table requires the name of the connection.

  4. Insert sample data.

    In a production application, data is entered into the database by synchronizing. It is a useful practice to insert some sample data during the initial stages of development, and include synchronization at a later stage.

    The method names in the ULProduct class are unique names that reflect the columns of the table in the reference database.

    Synchronization is added to this routine in Lesson 6: Add synchronization to your application.

  5. Write out the price of each item.

    The price is retrieved and written out for each row in the table.

  6. Close the objects.

    Closing the objects used in the program frees the memory associated with them.


Contents Index Lesson 3: Run the UltraLite generator Lesson 5: Build and run your application pdf/preface.pdf