Contents Index Lesson 1: Connect to the database Lesson 3: Select the rows from the table

UltraLite C++ User's Guide
  Tutorial: An Introductory Application

Lesson 2: Insert data into the database


This lesson shows you how to add data to the database.

To add rows to your database

  1. Add procedure below to customer.cpp, immediately before the main method. This procedure carries out the following tasks:

    bool do_insert( Connection * conn ) {
      Table * table = conn->OpenTable( UL_TEXT("customer") );
      if( table == NULL ) {
         return false;
      }
      if( table->GetRowCount() == 0 ) {
         _tprintf( UL_TEXT("Inserting two rows.\n") );
         table->InsertBegin();
         table->Set( UL_TEXT("fname"), UL_TEXT("Penny") );
         table->Set( UL_TEXT("lname"), UL_TEXT("Stamp") );
         table->Insert();
         CHECK_ERROR();
       table->InsertBegin();
         table->Set( UL_TEXT("fname"), UL_TEXT("Gene") );
         table->Set( UL_TEXT("lname"), UL_TEXT("Poole") );
         table->Insert();
         CHECK_ERROR();
         conn->Commit();
         CHECK_ERROR();
      } else {
         _tprintf( UL_TEXT("The table has %lu rows\n"),
         table->GetRowCount() );
      }
      table->Release();
      return true;
    }
  2. Add the following line to the main() method, immediately after the call to the Customer constructor:

    do_insert(conn);
  3. Compile and run your application, as in Lesson 1: Connect to the database.

    The first time you run the application, it prints the following messages:

    Connected to an existing database.
    Inserting two rows.
    id=1, name=Penny Stamp
    id=2, name=Gene Poole

    Subsequent times, it prints the following messages:

    Connected to an existing database.
    The table has 2 rows.

Contents Index Lesson 1: Connect to the database Lesson 3: Select the rows from the table