×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Using Symbian SQL API with data streams

From Nokia Developer Wiki
Jump to: navigation, search
tanjaluodes (Talk | contribs)
m
lpvalente (Talk | contribs)
m (Lpvalente -)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Symbian C++]][[Category:Code Snippet]][[Category:Files/Data]][[Category:S60 3rd Edition FP2]][[Category:Code Snippet]]
__NOEDITSECTION__
+
{{Archived|timestamp=20120313131201|user=roy.debjit| }}
{{KBCS}}
+
{{CodeSnippet
+
|id=CS000930
+
|platform=S60 3rd Edition, FP2
+
|devices=Nokia 6220 Classic
+
|category=Symbian C++
+
|subcategory=Files/Data
+
|creationdate=May 2, 2008
+
|keywords=RSqlDatabase,RSqlStatement,RSqlParamWriteStream, RSqlColumnReadStream, RSqlParamWriteStream::BindInt(), RSqlParamWriteStream::BindText(), RSqlParamWriteStream::BindBinary(), RSqlParamWriteStream::Close(), RSqlColumnReadStream::ColumnText() RSqlColumnReadStream::ColumnBinary() RSqlColumnReadStream::ColumnClose()
+
}}
+
  
 +
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|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]]) -->
 +
|devices= Nokia 6220 Classic
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= S60 3rd Edition, FP2
 +
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|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,RSqlParamWriteStream, RSqlColumnReadStream, RSqlParamWriteStream::BindInt(), RSqlParamWriteStream::BindText(), RSqlParamWriteStream::BindBinary(), RSqlParamWriteStream::Close(), RSqlColumnReadStream::ColumnText() RSqlColumnReadStream::ColumnBinary() RSqlColumnReadStream::ColumnClose()
 +
|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= CS000930
 +
}}
 +
 
 
==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 <tt>RSqlParamWriteStream</tt> object and call one of its <tt>Bind()</tt> methods, for example,  <tt>BindText()</tt>. Reading data from a database to a data stream is possible by declaring an <tt>RSqlColumnReadStream</tt> object and call one of its Column() methods, such as <tt>ColumnBinary()</tt>.
+
{{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 23: Line 40:
  
 
<code>
 
<code>
LIBRARY euser.lib
+
LIBRARY euser.lib
LIBRARY sqldb.lib
+
LIBRARY sqldb.lib
LIBRARY estor.lib //RSqlColumnReadStream, RSqlParamWriteStream
+
LIBRARY estor.lib //RSqlColumnReadStream, RSqlParamWriteStream
 
</code>
 
</code>
  
Line 193: Line 210:
 
==Postconditions==
 
==Postconditions==
  
The example database streams.db has been created and the test rows have been inserted into tables using <tt>RSqlParamWriteStream</tt> streams. After that <tt>RSqlColumnReadStream</tt> streams are used to fetch the test data.
+
The example database streams.db has been created and the test rows have been inserted into tables using {{Icode|RSqlParamWriteStream}} streams. After that {{Icode|RSqlColumnReadStream}} streams are used to fetch the test data.
  
 
==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]]
* [[CS000927 - Using SQL API with SQL statements which do not return data]]
+
* [[Archived:Using Symbian SQL API with SQL statements which do not return data]]
* [[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]]
 
+
[[Category:Symbian C++]][[Category:Code Examples]][[Category:Files/Data]][[Category:S60 3rd Edition, Feature Pack 2]][[Category:Code Snippet]]
+

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

×