Week 32 contribution of PDK documentation content. See release notes for details. Fixes bug Bug 3582
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License
"Eclipse Public License v1.0" which accompanies this distribution,
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
Nokia Corporation - initial contribution.
Contributors:
-->
<!DOCTYPE concept
PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-CCB9C61B-FB28-5CD9-A366-4A9584097897" xml:lang="en"><title>Inserting
Data into a Table</title><shortdesc>This tutorial describes how to insert a row into a table.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<section id="GUID-370ABA19-07EB-40D6-B320-0104A8BAD168"><title>Introduction</title> <p>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. </p> </section>
<section id="GUID-9048FC48-5224-45EC-9A06-04CF472C497D"><title>Basic procedure</title> <p>The
high level steps for inserting a row into a table are shown here: </p> <ol id="GUID-342E5527-AFF4-52BF-8BCC-248ED712FAE4">
<li id="GUID-3CBB4CA6-2150-5FC2-B3EF-7F57E063770C"><p>Configure the SQL statement. </p> </li>
<li id="GUID-4F74DDD2-EC0E-5A74-AE52-B71F718F1134"><p>Execute the statement. </p> </li>
</ol> </section>
<section id="GUID-8F63ABF9-7661-481B-B7EA-CB14D42D7418"><title>Detailed steps</title> <p>An <codeph>INSERT</codeph> query
adds data to the database. The example shown here inserts a new record into
the countries database. </p> <p><b> Configure
the SQL statement</b> </p> <ol id="GUID-A9FA4D02-07F6-50B6-B165-8B35B2560E69">
<li id="GUID-81E2024D-AD8E-5A7A-8413-9941F0EA68B5"><p>Declare the necessary
literals as shown and instantiate an object of the <xref href="GUID-0176BF07-DF94-3259-8F90-DE030E35CE9A.dita"><apiname>RSqlStatement</apiname></xref> class. </p> <codeblock id="GUID-791EF6F6-E93E-51C7-B9C0-4FA8DB5ABDF5" xml:space="preserve">_LIT(kQueryString,"INSERT INTO countries (name, population)
VALUES (:value1, :value2);");
_LIT(kValue1,":value1);
_LIT(kValue2,":value2");
_LIT(kItaly,"Italy");
RSqlStatement myStatement;</codeblock> </li>
<li id="GUID-98A5A03E-D0F8-5B9A-AE45-B2CB8617B463"><p>Prepare the SQL statement
for execution. </p> <codeblock id="GUID-71E96B9E-C0E0-539C-A4B7-2BD13EB81943" xml:space="preserve">TInt err;
err = myStatement.Prepare(countriesDatabase,kQueryString); // prepare</codeblock> </li>
<li id="GUID-1781CA11-0439-5A6D-8562-52DB2681334B"><p>Get the index of each
of the parameters with the given name and bind it with the text and integer
value respectively. </p> <codeblock id="GUID-4098A56A-DFB1-5EF8-A493-334A6EA8D7A8" xml:space="preserve">TInt index1 = myStatement.ParameterIndex(kValue1); // index
TInt index2 = myStatement.ParameterIndex(kValue2);
err = myStatement.BindText(index1,kItaly);
err = myStatement.BindInt(index2,59); // bind</codeblock> </li>
</ol> <p><b> Execute
the statement</b> </p><p>Execute the SQL statement to insert a record.</p><codeblock xml:space="preserve">err = myStatement.Exec();// execute</codeblock><p>Close
the SQL statement.</p><codeblock xml:space="preserve">err = myStatement.Close();// close</codeblock> <p><b>Note</b> </p> <p>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: </p> <ul>
<li id="GUID-23B7CDC9-A56B-5A66-A177-594216092DA3"><p>Get the index of the
parameter. </p> <codeblock id="GUID-DCADC824-6EEC-55A1-BF9B-FDA8AEDB9B9C" xml:space="preserve">TInt index1 = myStatement.ParameterIndex(kValue1); // index</codeblock> </li>
<li id="GUID-B25426C0-A1EC-5A51-9718-F84D3E7FD91F"><p>Iterate the loop 10
times by binding the integer value of the variable i with the parameter. Then
execute the <codeph>INSERT</codeph> statement to insert a new record into
the table. Finally, call the <codeph>Reset()</codeph> function to reset the
SQL statement and make it ready for the next execution. </p> <codeblock id="GUID-4B406DB1-220F-5347-84C8-C4071FD7F59E" xml:space="preserve">for(TInt i=1;i<=10;i++)
{
err = myStatement.BindInt(index1,i);
err = myStatement.Exec(); // execute
err = myStatement.Reset()
}</codeblock> </li>
</ul> </section>
<section id="GUID-B67C9BA7-372F-4FE8-8362-BF8CD566FE12"><title>See also</title> <p><xref href="GUID-C60DAE3D-7FB9-5619-9E5D-476A430705AF.dita">Querying a database</xref> </p> <p>Inserting
a Row into a Table - This document </p> <p><xref href="GUID-B61EA8C5-0966-51DE-AC73-01DD34C7D3CC.dita">Deleting
Rows from a Table</xref> </p> <p> <xref href="GUID-72511204-FC90-54AA-9E2E-833318020318.dita">Reading
to a Buffer</xref> </p> <p> <xref href="GUID-3CCA6503-54DA-5558-85DC-93A22A81F565.dita"> Reading
to Memory </xref> </p> <p> <xref href="GUID-183280EE-0C57-54FE-8ABB-E1CC3BDE525B.dita">Reading
to a Data Stream</xref> </p> <p> <xref href="GUID-B9A3B17E-BDEB-5F66-968C-080335A721AC.dita">Writing
to a Data Stream</xref> </p> <p> <xref href="GUID-C474376E-1766-5781-B5BF-3786C5B4D72E.dita">Performing
Scalar Queries</xref> </p> </section>
</conbody></concept>