Contents Index Accessing and manipulating data with dynamic SQL Data retrieval: SELECT

UltraLite C++ User's Guide
  Understanding UltraLite Development
    Accessing and manipulating data with dynamic SQL

Data manipulation: INSERT, UPDATE and DELETE


To perform SQL Data Manipulation Language operations (INSERT, UPDATE, and DELETE), you carry out the following sequence of operations:

  1. Prepare the statement.

    You can indicate parameters in the statement using the ? character.

  2. Assign values for parameters in the statement.

    For any INSERT, UPDATE or DELETE, each ? is referred to by its ordinal position in the prepared statement.

  3. Execute the statement.

  4. Repeat steps 2 and 3 as required.

To perform INSERT operations using ExecuteStatement:

  1. Declare a PreparedStatement.

    PreparedStatement * prepStmt;
  2. Assign a SQL statement to the PreparedStatement object.

    ULValue sqltext( 
        "INSERT INTO MyTable(MyColumn) values (?)" );
    prepStmt = conn->PrepareStatement( sqltext );
  3. Assign input parameter values for the statement.

    The following code shows a string parameter.

    ULValue newValue( "string-value" );
    prepStmt->SetParameter( 1, newValue );
  4. Execute the statement.

    The return value indicates the number of rows affected by the statement.

    ul_s_long rowsInserted;
    rowsInserted = prepStmt->ExecuteStatement();
  5. Commit the change.

    conn->Commit();

To perform UPDATE operations using ExecuteStatement:

  1. Declare a PreparedStatement.

    PreparedStatement * prepStmt;
  2. Assign a statement to the PreparedStatement object.

    ULValue sqltext(
       "UPDATE MyTable SET MyColumn1 = ? WHERE MyColumn2 = ?" );
    prepStmt = conn->PrepareStatement( sqltext );
  3. Assign input parameter values for the statement.

    ULValue newValue( new-value );
    ULValue oldValue( old-value );
    stmt->SetParameter( 1, newValue );
    stmt->SetParameter( 2, oldValue );
  4. Execute the statement.

    long rowsUpdated = prepStmt->ExecuteStatement();
  5. Commit the change.

    conn->Commit();

To perform DELETE operations using ExecuteStatement:

  1. Declare a PreparedStatement.

    PreparedStatement * prepStmt;
  2. Assign a statement to the PreparedStatement object.

    ULValue sqltext(
       "DELETE FROM MyTable WHERE MyColumn = ?" );
    prepStmt = conn->PrepareStatement( sqltext );
  3. Assign input parameter values for the statement.

    ULValue deleteValue( old-value );
    prepStmt->SetParameter( 1, deleteValue );
  4. Execute the statement.

    long rowsDeleted = prepStmt->ExecuteStatement();
  5. Commit the change.

    conn->Commit();

Contents Index Accessing and manipulating data with dynamic SQL Data retrieval: SELECT