×
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
Forum Nokia KB (Talk | contribs)
Forum Nokia KB (Talk | contribs)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 
+
{{KBCS}}
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
 
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
 
|-
 
|-
|'''ID''' ||  
+
|'''ID''' || CS000927
|'''Creation date''' || April 24, 2008
+
|'''Creation date''' || May 2, 2008
 
|-
 
|-
 
|'''Platform''' || S60 3rd Edition, FP2
 
|'''Platform''' || S60 3rd Edition, FP2
Line 22: Line 22:
 
==Overview==
 
==Overview==
  
The <tt>RSqlStatement</tt> class provides functionality to execute all types of SQL statements when using SQL API. <tt>RSqlStatement</tt> encapsulates a SQL statement and the data that it returns. <tt>RSqlStatement</tt> statements can be executed with methods: <tt>Exec()</tt> and <tt>Next()</tt>. The method <tt>Exec()</tt> is used when statement do not return any data and <tt>Next()</tt> when statement do. This snippet shows some examples how to use the <tt>Exec()</tt> method.
+
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.
  
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 e.g. BindText() is one of them. It is a good practise to resolve the placeholder index with <tt>RSqlStatement::ParameterIndex()</tt> method, and not 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 <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.
  
 
'''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 <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.
 
+
If asyncronous functionality is required, <tt>RSqlDatabase</tt> and <tt>RSqlStatement</tt> classes have also asyncronous versions of Exec() methods.
+
  
 +
If asyncronous functionality is required, <tt>RSqlDatabase</tt> and <tt>RSqlStatement</tt> classes have also asyncronous versions of <tt>Exec()</tt> methods.
  
 
This snippet can be self-signed.
 
This snippet can be self-signed.
Line 54: Line 53:
 
==Preconditions==
 
==Preconditions==
  
Databases nonsecure.db and [UID3]secure.db needs to be created before this code snippet.
+
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]].
 
+
See [[Using_SQL_API_for_creating_non-secure_and_secure_databases]]
+
  
 
<!--
 
<!--
Line 171: Line 168:
 
//== Delete statement==
 
//== Delete statement==
  
   /* Note: It is possible to use RSqlStatement object to delete rows from table,
+
   /* Note: It is possible to use a RSqlStatement object to delete rows from table,
   * but it is computationally more expensive and it is not recommented.
+
   * but it is computationally more expensive and it is not recommended.
 
   * Use RSqlDatabase::Exec() method instead */
 
   * Use RSqlDatabase::Exec() method instead */
  
Line 199: Line 196:
 
==See also==
 
==See also==
  
* [[Using_SQL_API_for_creating_non-secure_and_secure_databases]]
+
* [[CS000925 - Using SQL API for creating non-secure and secure databases]]
* [[Using_SQL_API_for_attaching_and_detaching_databases]]
+
* [[CS000926 - Using SQL API for attaching and detaching databases]]
* [[Using_SQL_API_with_SQL_statements_which_return_data]]
+
* [[CS000928 - Using SQL API with SQL statements which return data]]
* [[Using_SQL_API_with_scalar_queries]]
+
* [[CS000929 - Using SQL API with scalar queries]]
* [[Using_SQL_API_with_data_streams]]
+
* [[CS000930 - Using SQL API with data streams]]
  
[[Category:Symbian C++]][[Category:Code Examples]][[Category:Files/Data]]
+
[[Category:Symbian C++]][[Category:Code Examples]][[Category:Files/Data]][[Category:S60 3rd Edition, Feature Pack 2]]

Revision as of 10:40, 2 May 2008


Template:KBCS

ID CS000927 Creation date May 2, 2008
Platform S60 3rd Edition, FP2 Tested on devices Nokia 6220 Classic
Category Symbian C++ Subcategory Files/Data


Keywords (APIs, classes, methods, functions): RSqlDatabase, RSqlStatement, RSqlDatabase::Open(), RSqlDatabase::Exec(), RSqlDatabase::Close() RSqlStatement::Prepare(), RSqlStatement::ParameterIndex(), RSqlStatement::BindInt(), RSqlStatement::BindText(), RSqlStatement::Reset(), RSqlStatement::Close()

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.


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

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.


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
}
 
}

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).

See also

109 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×