×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Using Symbian SQL API with data streams

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Removed protection from "CS000930 - Using SQL API with data streams": Merging into Wiki)
lpvalente (Talk | contribs)
m (Lpvalente -)
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
{{Archived|timestamp=20120313131201|user=roy.debjit| }}
 
 
[[Category:Symbian C++]][[Category:Code Snippet]][[Category:Files/Data]][[Category:S60 3rd Edition FP2]][[Category:Code Snippet]]
 
[[Category:Symbian C++]][[Category:Code Snippet]][[Category:Files/Data]][[Category:S60 3rd Edition FP2]][[Category:Code Snippet]]
 +
{{Archived|timestamp=20120313131201|user=roy.debjit| }}
 +
 +
 
{{ArticleMetaData <!-- v1.2 -->
 
{{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]]) -->
Line 29: Line 31:
 
==Overview==
 
==Overview==
  
This snippet shows how to use two classes that support the streaming functionality with the SQL API. Data streams can be used to write to a database by declaring an {{Icode|RSqlParamWriteStream}} object and call one of its {{Icode|Bind()}} methods, for example,  {{Icode|BindText()}}. Reading data from a database to a data stream is possible by declaring an {{Icode|RSqlColumnReadStream}} object and call one of its Column() methods, such as {{Icode|ColumnBinary()}}.
+
{{Abstract|This snippet shows how to use two classes that support the streaming functionality with the SQL API. Data streams can be used to write to a database by declaring an {{Icode|RSqlParamWriteStream}} object and call one of its {{Icode|Bind()}} methods, for example,  {{Icode|BindText()}}. Reading data from a database to a data stream is possible by declaring an {{Icode|RSqlColumnReadStream}} object and call one of its Column() methods, such as {{Icode|ColumnBinary()}}.}}
  
 
This snippet can be self-signed.
 
This snippet can be self-signed.
Line 212: Line 214:
 
==See also==
 
==See also==
  
* [[Using SQL API for creating non-secure and secure databases on Symbian]]
+
* [[Archived:Using SQL API for creating non-secure and secure databases on Symbian]]
* [[Using Symbian SQL API for attaching and detaching databases]]
+
* [[Archived:Using Symbian SQL API for attaching and detaching databases]]
* [[Using Symbian SQL API with SQL statements which do not return data]]
+
* [[Archived:Using Symbian SQL API with SQL statements which do not return data]]
* [[Using Symbian SQL API with SQL statements which return data]]
+
* [[Archived:Using Symbian SQL API with SQL statements which return data]]
* [[Using Symbian SQL API with scalar queries]]
+
* [[Archived:Using Symbian SQL API with scalar queries]]

Latest revision as of 20:35, 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,RSqlParamWriteStream, RSqlColumnReadStream, RSqlParamWriteStream::BindInt(), RSqlParamWriteStream::BindText(), RSqlParamWriteStream::BindBinary(), RSqlParamWriteStream::Close(), RSqlColumnReadStream::ColumnText() RSqlColumnReadStream::ColumnBinary() RSqlColumnReadStream::ColumnClose()
Created: aknyman (24 Apr 2008)
Last edited: lpvalente (20 Aug 2012)

Contents

[edit] Overview

This snippet shows how to use two classes that support the streaming functionality with the SQL API. Data streams can be used to write to a database by declaring an RSqlParamWriteStream object and call one of its Bind() methods, for example, BindText(). Reading data from a database to a data stream is possible by declaring an RSqlColumnReadStream object and call one of its Column() methods, such as ColumnBinary().

This snippet can be self-signed.

[edit] MMP file

The following libraries are required:

LIBRARY euser.lib
LIBRARY sqldb.lib
LIBRARY estor.lib //RSqlColumnReadStream, RSqlParamWriteStream


[edit] Source file

#include <e32base.h>
#include <SqlDb.h>
 
void ReadAndWriteStreamsL()
{
 
_LIT(KStreamsDbName, "\\streams.db");
 
RSqlDatabase database;
TInt error = database.Create(KStreamsDbName);
 
// == Writing from a data stream ==
 
if(error == KErrNone)
{
CleanupClosePushL(database);
 
// Create a table
_LIT(KSqlCreateTableStreams, "CREATE TABLE STREAMS(ID INTEGER, STREAM1 TEXT,
STREAM2 BLOB);"
);
User::LeaveIfError(database.Exec(KSqlCreateTableStreams));
 
_LIT(KSqlInsertIntoTableStreams, "INSERT INTO STREAMS(ID, STREAM1, STREAM2)
VALUES(:Val1, :Val2, :Val3)"
);
 
RSqlStatement sqlInsertIntoStreamsStatement;
sqlInsertIntoStreamsStatement.Prepare(database, KSqlInsertIntoTableStreams);
CleanupClosePushL(sqlInsertIntoStreamsStatement);
 
TInt paramIndex1 = sqlInsertIntoStreamsStatement.ParameterIndex(_L(":Val1"));
TInt paramIndex2 = sqlInsertIntoStreamsStatement.ParameterIndex(_L(":Val2"));
TInt paramIndex3 = sqlInsertIntoStreamsStatement.ParameterIndex(_L(":Val3"));
 
for(TInt id=1; id<10; id++)
{
//Set value into column ID
User::LeaveIfError(sqlInsertIntoStreamsStatement.BindInt(paramIndex1, id));
 
RSqlParamWriteStream stream1;
RSqlParamWriteStream stream2;
CleanupClosePushL(stream1);
CleanupClosePushL(stream2);
 
User::LeaveIfError(stream1.BindText(sqlInsertIntoStreamsStatement,
paramIndex2));
 
//Generate some text data into column STREAM2
TChar ch = '0' + id;
TBuf<500> idBuf;
idBuf.Fill(ch,500);
stream1.WriteL(idBuf);
 
User::LeaveIfError(stream2.BindBinary(sqlInsertIntoStreamsStatement,
paramIndex3));
 
//Generate some binary data into column STREAM2
TUint8 startValue = '0' + id;
TUint8 endValue = startValue + 100;
 
for(; startValue<endValue; startValue++)
{
stream2 << static_cast <TUint8> (startValue);
}
stream2.CommitL();
 
User::LeaveIfError(sqlInsertIntoStreamsStatement.Exec());
User::LeaveIfError(sqlInsertIntoStreamsStatement.Reset());
 
CleanupStack::PopAndDestroy(2); //stream2, stream1
}
 
CleanupStack::PopAndDestroy(2); //sqlInsertIntoStreamsStatement, database
}
else
{
//open database failed
}
 
// == Reading to a data stream ==
error = database.Open(KStreamsDbName);
 
if (error == KErrNone)
{
CleanupClosePushL(database);
 
RSqlStatement sqlSelectFromStreamsTableStatement;
 
_LIT(KSqlSelectFromStreamsTable, "SELECT * FROM STREAMS");
TInt ret = sqlSelectFromStreamsTableStatement.Prepare(database,
KSqlSelectFromStreamsTable);
 
if(ret == KErrNone)
{
CleanupClosePushL(sqlSelectFromStreamsTableStatement);
 
TInt columnIndex1 = sqlSelectFromStreamsTableStatement.ColumnIndex(_L("STREAM1"));
TInt columnIndex2 = sqlSelectFromStreamsTableStatement.ColumnIndex(_L("STREAM2"));
 
TInt err = KErrNone;
 
while((err = sqlSelectFromStreamsTableStatement.Next()) == KSqlAtRow)
{
RSqlColumnReadStream stream1;
RSqlColumnReadStream stream2;
CleanupClosePushL(stream1);
CleanupClosePushL(stream2);
 
//Get data from text column STREAM1
User::LeaveIfError(stream1.ColumnText(sqlSelectFromStreamsTableStatement,
columnIndex1));
TInt size1 = sqlSelectFromStreamsTableStatement.ColumnSize(columnIndex1);
RBuf buf;
buf.CreateL(size1);
CleanupClosePushL(buf);
 
stream1.ReadL(buf,size1);
//do something with the data...
 
//Get data from binary column STREAM2
User::LeaveIfError(stream2.ColumnBinary(sqlSelectFromStreamsTableStatement,
columnIndex2));
TInt size2 = sqlSelectFromStreamsTableStatement.ColumnSize(columnIndex2);
HBufC8* buf2 = HBufC8::NewLC(size2);
TPtr8 buf2Ptr = buf2->Des();
 
stream2.ReadL(buf2Ptr,size2);
//do something with the data...
CleanupStack::PopAndDestroy(4); //buf2, buf, stream2, stream1
}
if(err == KSqlAtEnd)
{
// OK - no more records
}
else
{
// process the error
}
 
CleanupStack::PopAndDestroy(2); //sqlSelectFromStreamsTableStatement, database
}
else
{
//open database failed
}
}

[edit] Postconditions

The example database streams.db has been created and the test rows have been inserted into tables using RSqlParamWriteStream streams. After that RSqlColumnReadStream streams are used to fetch the test data.

[edit] See also

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