×
Namespaces

Variants
Actions
Revision as of 20:35, 20 August 2012 by lpvalente (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:Using Symbian SQL API with scalar queries

From Nokia Developer Wiki
Jump to: navigation, search

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, TSqlScalarFullSelectQuery, RSqlDatabase::Open(), RSqlDatabase::Close(), TSqlScalarFullSelectQuery::SelectIntL(), TSqlScalarFullSelectQuery::SelectTextL()
Created: aknyman (24 Apr 2008)
Last edited: lpvalente (20 Aug 2012)

Contents

Overview

The TSqlScalarFullSelectQuery class is used in situations where the SELECT SQL query refering to several data fields returns only one row with a single column value. When compared to class RSqlStatement, TSqlScalarFullSelectQuery is more efficient and recommended to use when possible. The class instance is created with a parameter that refers to the opened database. TSqlFullSelectQuery has a number of methods that take the SELECT SQL query as a parameter and return values of different data types. This snippet shows how to use methods TSqlScalarFullSelectQuery::SelectIntL() and TSqlScalarFullSelectQuery::SelectTextL().

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 Archived:Using SQL API for creating non-secure and secure databases on Symbian.


Source file

#include <e32base.h>
#include <SqlDb.h>
 
void ScalarQueriesL()
{
RSqlDatabase database;
_LIT(KNonSecureDbName, "\\nonsecure.db");
_LIT(KSecureDbName, "[E80000AF]secure.db");
 
TInt error = database.Open(KNonSecureDbName);
 
if (error == KErrNone)
{
CleanupClosePushL(database);
 
//count how many rows is in the database
_LIT(KSqlSelectCount,"SELECT COUNT (*) FROM MOVIES");
TSqlScalarFullSelectQuery sqlSelectCountQuery(database);
TInt count = sqlSelectCountQuery.SelectIntL(KSqlSelectCount);
 
//get the max year value (the newest movies)
_LIT(KSqlSelectMax,"SELECT MAX(YEAR) FROM MOVIES");
TSqlScalarFullSelectQuery sqlSelectMaxQuery(database);
TInt max = sqlSelectMaxQuery.SelectIntL(KSqlSelectMax);
 
//get the average year value
_LIT(KSqlSelectAvg,"SELECT AVG(YEAR) FROM MOVIES");
TSqlScalarFullSelectQuery sqlSelectAvgQuery(database);
TInt avg = sqlSelectAvgQuery.SelectIntL(KSqlSelectAvg);
 
CleanupStack::PopAndDestroy(); //database
}
else
{
//open database failed
}
 
error = database.Open(KSecureDbName);
 
if (error == KErrNone)
{
CleanupClosePushL(database);
 
_LIT(KSqlSelectFirstBook,"SELECT TITLE FROM BOOKS WHERE ID=1");
 
TSqlScalarFullSelectQuery fullSelectQuery(database);
HBufC* titleBuf = HBufC::NewLC(5);
TPtr title = titleBuf->Des();
TInt ret = fullSelectQuery.SelectTextL(KSqlSelectFirstBook, title);
 
//if ret > 0 buffer reallocation is needed
if(ret > 0)
{
titleBuf = titleBuf->ReAllocL(ret);
CleanupStack::Pop();
CleanupStack::PushL(titleBuf);
title.Set(titleBuf->Des());
ret = fullSelectQuery.SelectTextL(KSqlSelectFirstBook, title);
//do something with the data...
}
else
{
//do something with the data...
}
 
CleanupStack::PopAndDestroy(2);//buf, database
}
else
{
//open database failed
}
 
 
}

Postconditions

The TSqlScalarFullSelectQuery class is used to get COUNT, MAX, and AVG values from the database nonsecure.db and one specific textual value from the dateabase [UID3]secure.db.


See also

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