×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Using Symbian SQL API with SQL statements which do not return data

From Nokia Developer Wiki
Jump to: navigation, search
debjit.roy (Talk | contribs)
(Debjit.roy -)
lpvalente (Talk | contribs)
m (Lpvalente -)
 
(7 intermediate revisions by one user not shown)
Line 1: Line 1:
[[Category:Symbian C++]][[Category:Code Examples]][[Category:Files/Data]][[Category:S60 3rd Edition, Feature Pack 2]][[Category:Code Snippet]]
+
[[Category:Symbian C++]][[Category:Code Snippet]][[Category:Files/Data]][[Category:S60 3rd Edition FP2]][[Category:Code Snippet]]
__NOTOC__
+
{{Archived|timestamp=20120313131234|user=roy.debjit| }}
__NOEDITSECTION__
+
{{KBCS}}
+
{{ArticleMetaData
+
|id=CS000927
+
|platform=S60 3rd Edition, FP2
+
|devices=Nokia 6220 Classic
+
|category=Symbian C++
+
|subcategory=Files/Data
+
|creationdate=May 2, 2008
+
|keywords=RSqlDatabase, RSqlStatement, RSqlDatabase::Open(), RSqlDatabase::Exec(), RSqlDatabase::Close() RSqlStatement::Prepare(), RSqlStatement::ParameterIndex(), RSqlStatement::BindInt(), RSqlStatement::BindText(), RSqlStatement::Reset(), RSqlStatement::Close()
+
  
 +
{{ArticleMetaData <!-- v1.2 -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
+
|devices= Nokia 6220 Classic
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|platform= S60 3rd Edition, FP2
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|author=[[User:Aknyman]]
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= RSqlDatabase, RSqlStatement, RSqlDatabase::Open(), RSqlDatabase::Exec(), RSqlDatabase::Close() RSqlStatement::Prepare(), RSqlStatement::ParameterIndex(), RSqlStatement::BindInt(), RSqlStatement::BindText(), RSqlStatement::Reset(), RSqlStatement::Close()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20080424
 +
|author= [[User:Aknyman]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= Files/Data
 +
|id= CS000927
 
}}
 
}}
{{Archived|timestamp=20120313131234|user=roy.debjit| }}
 
 
    
 
    
 
==Overview==
 
==Overview==
  
The <tt>RSqlStatement</tt> class provides functionality to execute all types of SQL statements when using the SQL API. <tt>RSqlStatement</tt> encapsulates an SQL statement and the data that it returns. <tt>RSqlStatement</tt> statements can be executed with the methods <tt>Exec()</tt> and <tt>Next()</tt>. The method <tt>Exec()</tt> is used when the statement does not return any data and <tt>Next()</tt> when the statement returns data. This snippet gives some examples on how to use the <tt>Exec()</tt> method.
+
{{Abstract|The {{Icode|RSqlStatement}} class provides functionality to execute all types of SQL statements when using the SQL API. {{Icode|RSqlStatement}} encapsulates an SQL statement and the data that it returns. {{Icode|RSqlStatement}} statements can be executed with the methods {{Icode|Exec()}} and {{Icode|Next()}}. The method {{Icode|Exec()}} is used when the statement does not return any data and {{Icode|Next()}} when the statement returns data. This snippet gives some examples on how to use the {{Icode|Exec()}} method.}}
  
Statements use so-called placeholders (:) in descriptors to mark positions where dynamic data is inserted at runtime. The user can bind the actual data to placeholders with different <tt>Bind()</tt> methods, such as BindText(). It is good practise to resolve the placeholder index with the <tt>RSqlStatement::ParameterIndex()</tt> method, and not to use hardcoded values.
+
Statements use so-called placeholders (:) in descriptors to mark positions where dynamic data is inserted at runtime. The user can bind the actual data to placeholders with different {{Icode|Bind()}} methods, such as BindText(). It is good practise to resolve the placeholder index with the {{Icode|RSqlStatement::ParameterIndex()}} method, and not to use hardcoded values.
  
 
'''NOTE:
 
'''NOTE:
SQL statements can also be executed using the <tt>RSqlDatabase::Exec()</tt> methods, but the behaviour is more limited because the <tt>RSqlDatabase::Exec()</tt> method is not able to return any records.
+
SQL statements can also be executed using the {{Icode|RSqlDatabase::Exec()}} methods, but the behaviour is more limited because the {{Icode|RSqlDatabase::Exec()}} method is not able to return any records.
  
If asyncronous functionality is required, <tt>RSqlDatabase</tt> and <tt>RSqlStatement</tt> classes have also asyncronous versions of <tt>Exec()</tt> methods.
+
If asyncronous functionality is required, {{Icode|RSqlDatabase}} and {{Icode|RSqlStatement}} classes have also asyncronous versions of {{Icode|Exec()}} methods.
  
 
This snippet can be self-signed.
 
This snippet can be self-signed.
Line 41: Line 47:
  
 
<code>
 
<code>
LIBRARY euser.lib
+
LIBRARY euser.lib
LIBRARY sqldb.lib
+
LIBRARY sqldb.lib
 
</code>
 
</code>
  
Line 55: Line 61:
 
==Preconditions==
 
==Preconditions==
  
Databases nonsecure.db and [UID3]secure.db need to be created before executing this code snippet. See [[CS000925 - Using SQL API for creating non-secure and secure databases]].
+
Databases nonsecure.db and [UID3]secure.db need to be created before executing this code snippet. See [[Archived:Using SQL API for creating non-secure and secure databases on Symbian]].
  
 
<!--
 
<!--
Line 194: Line 200:
 
==Postconditions==
 
==Postconditions==
  
The <tt>RSqlStatement</tt> class is used to insert a new row into the table ([UID3]secure.db) and to update the existing row (nonsecure.db). The <tt>RSqlDatabase</tt> is used to delete one row from the table ([UID3]secure.db).
+
The {{Icode|RSqlStatement}} class is used to insert a new row into the table ([UID3]secure.db) and to update the existing row (nonsecure.db). The {{Icode|RSqlDatabase}} is used to delete one row from the table ([UID3]secure.db).
  
 
==See also==
 
==See also==
  
* [[CS000925 - Using SQL API for creating non-secure and secure databases]]
+
* [[Archived:Using SQL API for creating non-secure and secure databases on Symbian]]
* [[CS000926 - Using SQL API for attaching and detaching databases]]
+
* [[Archived:Using Symbian SQL API for attaching and detaching databases]]
* [[CS000928 - Using SQL API with SQL statements which return data]]
+
* [[Archived:Using Symbian SQL API with SQL statements which return data]]
* [[CS000929 - Using SQL API with scalar queries]]
+
* [[Archived:Using Symbian SQL API with scalar queries]]
* [[CS000930 - Using SQL API with data streams]]
+
* [[Archived:Using Symbian SQL API with data streams]]

Latest revision as of 20:34, 20 August 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Compatibility
Platform(s): S60 3rd Edition, FP2
S60 3rd Edition FP2
Article
Keywords: RSqlDatabase, RSqlStatement, RSqlDatabase::Open(), RSqlDatabase::Exec(), RSqlDatabase::Close() RSqlStatement::Prepare(), RSqlStatement::ParameterIndex(), RSqlStatement::BindInt(), RSqlStatement::BindText(), RSqlStatement::Reset(), RSqlStatement::Close()
Created: aknyman (24 Apr 2008)
Last edited: lpvalente (20 Aug 2012)

Contents

[edit] Overview

The RSqlStatement class provides functionality to execute all types of SQL statements when using the SQL API. RSqlStatement encapsulates an SQL statement and the data that it returns. RSqlStatement statements can be executed with the methods Exec() and Next(). The method Exec() is used when the statement does not return any data and Next() when the statement returns data. This snippet gives some examples on how to use the Exec() method.

Statements use so-called placeholders (:) in descriptors to mark positions where dynamic data is inserted at runtime. The user can bind the actual data to placeholders with different Bind() methods, such as BindText(). It is good practise to resolve the placeholder index with the RSqlStatement::ParameterIndex() method, and not to use hardcoded values.

NOTE: SQL statements can also be executed using the RSqlDatabase::Exec() methods, but the behaviour is more limited because the RSqlDatabase::Exec() method is not able to return any records.

If asyncronous functionality is required, RSqlDatabase and RSqlStatement classes have also asyncronous versions of Exec() methods.

This snippet can be self-signed.


[edit] MMP file

The following libraries are required:

LIBRARY euser.lib
LIBRARY sqldb.lib


The following capabilities are needed to test the example secure database:

CAPABILITY ReadUserData
CAPABILITY WriteUserData

[edit] Preconditions

Databases nonsecure.db and [UID3]secure.db need to be created before executing this code snippet. See Archived:Using SQL API for creating non-secure and secure databases on Symbian.


[edit] Source file

#include <e32base.h>
#include <SqlDb.h>
 
void ExecStatementsL()
{
RSqlDatabase database;
 
_LIT(KSecureDbName, "[E80000AF]secure.db");
 
//== Insert into table statement==
TInt error = database.Open(KSecureDbName);
 
if (error == KErrNone)
{
CleanupClosePushL(database);
 
RSqlStatement sqlInsertIntoBooksStatement;
 
_LIT(KSqlInsertIntoBooksTable, "INSERT INTO BOOKS(ID, TITLE, AUTHOR)
VALUES(:Val1, :Val2, :Val3)"
);
TInt ret = sqlInsertIntoBooksStatement.Prepare(database, KSqlInsertIntoBooksTable);
 
if(ret == KErrNone)
{
CleanupClosePushL(sqlInsertIntoBooksStatement);
 
TInt paramIndex1 = sqlInsertIntoBooksStatement.ParameterIndex(_L(":Val1"));
TInt paramIndex2 = sqlInsertIntoBooksStatement.ParameterIndex(_L(":Val2"));
TInt paramIndex3 = sqlInsertIntoBooksStatement.ParameterIndex(_L(":Val3"));
 
User::LeaveIfError(sqlInsertIntoBooksStatement.BindInt(paramIndex1, 10));
User::LeaveIfError(sqlInsertIntoBooksStatement.BindText(paramIndex2,
_L("RSqlStatementTitle")));
User::LeaveIfError(sqlInsertIntoBooksStatement.BindText(paramIndex3,
_L("RSqlStatementAuthor")));
 
User::LeaveIfError(sqlInsertIntoBooksStatement.Exec());
 
//It is possible to reuse statement after reset call
User::LeaveIfError(sqlInsertIntoBooksStatement.Reset());
//Bind new values here...
 
CleanupStack::PopAndDestroy(1); //KSqlInsertIntoBooksStatement
 
}
else
{
//prepare sql statement failed
}
CleanupStack::PopAndDestroy(1); //database
}
else
{
//open database failed
}
 
//== Update statement==
 
_LIT(KNonSecureDbName, "\\nonsecure.db");
 
error = database.Open(KNonSecureDbName);
 
if (error == KErrNone)
{
CleanupClosePushL(database);
 
RSqlStatement sqlUpdateMoviesRowStatement;
 
_LIT(KSqlUpdateMoviesRow, "UPDATE MOVIES SET TITLE=:Val1 WHERE ID=:Val2");
TInt ret = sqlUpdateMoviesRowStatement.Prepare(database, KSqlUpdateMoviesRow);
 
if(ret == KErrNone)
{
CleanupClosePushL(sqlUpdateMoviesRowStatement);
 
TInt paramIndex1 = sqlUpdateMoviesRowStatement.ParameterIndex(_L(":Val1"));
TInt paramIndex2 = sqlUpdateMoviesRowStatement.ParameterIndex(_L(":Val2"));
 
User::LeaveIfError(sqlUpdateMoviesRowStatement.BindText(paramIndex1,
_L("RSqlStatementUpdatedTitle")));
User::LeaveIfError(sqlUpdateMoviesRowStatement.BindInt(paramIndex2, 1));
 
User::LeaveIfError(sqlUpdateMoviesRowStatement.Exec());
 
//It is possible to reuse statement after reset call
User::LeaveIfError(sqlUpdateMoviesRowStatement.Reset());
//Bind new values here...
 
CleanupStack::PopAndDestroy(1); //sqlUpdateMoviesRowStatement
 
}
else
{
//prepare sql statement failed
}
CleanupStack::PopAndDestroy(1); //database
}
else
{
//open database failed
}
 
//== Delete statement==
 
/* Note: It is possible to use a RSqlStatement object to delete rows from table,
* but it is computationally more expensive and it is not recommended.
* Use RSqlDatabase::Exec() method instead */

 
error = database.Open(KSecureDbName);
 
if (error == KErrNone)
{
_LIT(KSqlDeleteFromBooksTable, "DELETE FROM BOOKS WHERE ID > 9;");
TInt ret = database.Exec(KSqlDeleteFromBooksTable);
 
database.Close();
}
else
{
//open database failed
}
 
}

[edit] Postconditions

The RSqlStatement class is used to insert a new row into the table ([UID3]secure.db) and to update the existing row (nonsecure.db). The RSqlDatabase is used to delete one row from the table ([UID3]secure.db).

[edit] See also

This page was last modified on 20 August 2012, at 20:34.
73 page views in the last 30 days.