Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » UltraLite - C and C++ Programming » Tutorial: Building a Windows application using the C++ API


Lesson 2: Inserting data into the database

In this lesson, you add data to a database.


This lesson assumes you have completed all preceding lessons. See Lesson 1: Creating and connecting to a database.

  1. Add the method below to customer.cpp immediately before the main method:

    static bool do_insert( ULConnection * conn ) 
        ULTable * table = conn->OpenTable( "ULCustomer" );
        if( table == UL_NULL ) {
            _tprintf( "Table not found: ULCustomer\n" );
            return false;
        if( table->GetRowCount() == 0 ) {
            _tprintf( "Inserting one row.\n" );
            table->SetString( "cust_name", "New Customer" );
        } else {
            _tprintf( "The table has %lu rows\n", table->GetRowCount() );
        return true;

    This method performs the following tasks.

    • Opens the table using the connection->OpenTable() method. You must open a Table object to perform operations on the table.

    • If the table is empty, adds a row to the table. To insert a row, the code changes to insert mode using the InsertBegin method, sets values for each required column, and executes an insert to add the row to the database.

    • If the table is not empty, reports the number of rows in the table.

    • Closes the Table object to free resources associated with it.

    • Returns a boolean indicating whether the operation was successful.

  2. Call the do_insert method you have created.

    Add the following line to the main() method, immediately before the call to conn->Close.

  3. Compile your application by running nmake.

  4. Run your application by typing customer at a command prompt.


The application runs and you can insert data into the ULCustomer table.