diff -r 4816d766a08a -r f345bda72bc4 Symbian3/PDK/Source/GUID-CCB9C61B-FB28-5CD9-A366-4A9584097897.dita --- a/Symbian3/PDK/Source/GUID-CCB9C61B-FB28-5CD9-A366-4A9584097897.dita Tue Mar 30 11:42:04 2010 +0100 +++ b/Symbian3/PDK/Source/GUID-CCB9C61B-FB28-5CD9-A366-4A9584097897.dita Tue Mar 30 11:56:28 2010 +0100 @@ -1,27 +1,71 @@ - - - - - -Inserting Data into a Table

This tutorial describes how to insert a row into a table.

Introduction

You can insert a single row or several rows in a table. To insert several rows the SQL statement needs to be called in a loop until the condition is satisfied.

Basic procedure

The high level steps for inserting a row into a table are shown here:

  1. Configure the SQL statement.

  2. Execute the statement.

Detailed steps

An INSERT query adds data to the database. The example shown here inserts a new record into the countries database.

Configure the SQL statement

  1. Declare the necessary literals as shown and instantiate an object of the RSqlStatement class.

    _LIT(kQueryString,"INSERT INTO countries (name, population) -VALUES (:value1, :value2);"); -_LIT(kValue1,":value1); -_LIT(kValue2,":value2"); -_LIT(kItaly,"Italy"); - -RSqlStatement myStatement;
  2. Prepare the SQL statement for execution.

    TInt err; -err = myStatement.Prepare(countriesDatabase,kQueryString); // prepare
  3. Get the index of each of the parameters with the given name and bind it with the text and integer value respectively.

    TInt index1 = myStatement.ParameterIndex(kValue1); // index -TInt index2 = myStatement.ParameterIndex(kValue2); -err = myStatement.BindText(index1,kItaly); -err = myStatement.BindInt(index2,59); // bind

Execute the statement

Note

To add several rows to a data table you need to call an INSERT statement within a loop. The following example inserts rows containing numbers 1 to 10 into the countries database. Steps 1 and 2 of the above list remain the same. The remaining steps are as shown below:

  • Get the index of the parameter.

    TInt index1 = myStatement.ParameterIndex(kValue1); // index
  • Iterate the loop 10 times by binding the integer value of the variable i with the parameter. Then execute the INSERT statement to insert a new record into the table. Finally, call the Reset() function to reset the SQL statement and make it ready for the next execution.

    for(TInt i=1;i<=10;i++) - { - err = myStatement.BindInt(index1,i); - err = myStatement.Exec(); // execute - err = myStatement.Reset() - }
See also

Querying a Database

Inserting a Row into a Table - This document

Deleting Rows from a Table

Reading to a Buffer

Reading to Memory

Reading to a Data Stream

Writing to a Data Stream

Performing Scalar Queries

\ No newline at end of file + + + + + +Inserting +Data into a TableThis tutorial describes how to insert a row into a table. +
Introduction

You +can insert a single row or several rows in a table. To insert several rows +the SQL statement needs to be called in a loop until the condition is satisfied.

+
Basic procedure

The +high level steps for inserting a row into a table are shown here:

    +
  1. Configure the SQL statement.

  2. +
  3. Execute the statement.

  4. +
+
Detailed steps

An INSERT query +adds data to the database. The example shown here inserts a new record into +the countries database.

Configure +the SQL statement

    +
  1. Declare the necessary +literals as shown and instantiate an object of the RSqlStatement class.

    _LIT(kQueryString,"INSERT INTO countries (name, population) +VALUES (:value1, :value2);"); +_LIT(kValue1,":value1); +_LIT(kValue2,":value2"); +_LIT(kItaly,"Italy"); + +RSqlStatement myStatement;
  2. +
  3. Prepare the SQL statement +for execution.

    TInt err; +err = myStatement.Prepare(countriesDatabase,kQueryString); // prepare
  4. +
  5. Get the index of each +of the parameters with the given name and bind it with the text and integer +value respectively.

    TInt index1 = myStatement.ParameterIndex(kValue1); // index +TInt index2 = myStatement.ParameterIndex(kValue2); +err = myStatement.BindText(index1,kItaly); +err = myStatement.BindInt(index2,59); // bind
  6. +

Execute +the statement

Execute the SQL statement to insert a record.

err = myStatement.Exec();// execute

Close +the SQL statement.

err = myStatement.Close();// close

Note

To +add several rows to a data table you need to call an INSERT statement within +a loop. The following example inserts rows containing numbers 1 to 10 into +the countries database. Steps 1 and 2 of the above list remain the same. The +remaining steps are as shown below:

    +
  • Get the index of the +parameter.

    TInt index1 = myStatement.ParameterIndex(kValue1); // index
  • +
  • Iterate the loop 10 +times by binding the integer value of the variable i with the parameter. Then +execute the INSERT statement to insert a new record into +the table. Finally, call the Reset() function to reset the +SQL statement and make it ready for the next execution.

    for(TInt i=1;i<=10;i++) + { + err = myStatement.BindInt(index1,i); + err = myStatement.Exec(); // execute + err = myStatement.Reset() + }
  • +
+
See also

Querying a database

Inserting +a Row into a Table - This document

Deleting +Rows from a Table

Reading +to a Buffer

Reading +to Memory

Reading +to a Data Stream

Writing +to a Data Stream

Performing +Scalar Queries

+
\ No newline at end of file