Symbian3/SDK/Source/GUID-CCB9C61B-FB28-5CD9-A366-4A9584097897.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 21 Jan 2010 18:18:20 +0000
changeset 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of Documentation_content according to Feature bug 1266 bug 1268 bug 1269 bug 1270 bug 1372 bug 1374 bug 1375 bug 1379 bug 1380 bug 1381 bug 1382 bug 1383 bug 1385

<?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 xml:lang="en" id="GUID-CCB9C61B-FB28-5CD9-A366-4A9584097897"><title>Inserting Data into a Table</title><prolog><metadata><keywords/></metadata></prolog><conbody><p>This tutorial describes how to insert a row into a table. </p> <section><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><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><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><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&lt;=10;i++)
       {
       err = myStatement.BindInt(index1,i);
       err = myStatement.Exec(); // execute
       err = myStatement.Reset()
       }</codeblock> </li> </ul> </section> <section><title>See also</title> <p> <xref href="GUID-F36E5978-29FC-57E8-8A45-2003E7767193.dita">Querying a Database</xref>  </p> <p>Inserting a Row into a Table - This document </p> <p> <xref href="GUID-3ACF6C11-A9CC-517C-8C7D-578F41F3DDF7.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>