×
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
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
Line 2: Line 2:
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 
{{KBCS}}
 
{{KBCS}}
{{CodeSnippet
+
{{ArticleMetaData
 
|id=CS000930
 
|id=CS000930
 
|platform=S60 3rd Edition, FP2
 
|platform=S60 3rd Edition, FP2
Line 10: Line 10:
 
|creationdate=May 2, 2008
 
|creationdate=May 2, 2008
 
|keywords=RSqlDatabase,RSqlStatement,RSqlParamWriteStream, RSqlColumnReadStream, RSqlParamWriteStream::BindInt(), RSqlParamWriteStream::BindText(), RSqlParamWriteStream::BindBinary(), RSqlParamWriteStream::Close(), RSqlColumnReadStream::ColumnText() RSqlColumnReadStream::ColumnBinary() RSqlColumnReadStream::ColumnClose()
 
|keywords=RSqlDatabase,RSqlStatement,RSqlParamWriteStream, RSqlColumnReadStream, RSqlParamWriteStream::BindInt(), RSqlParamWriteStream::BindText(), RSqlParamWriteStream::BindBinary(), RSqlParamWriteStream::Close(), RSqlColumnReadStream::ColumnText() RSqlColumnReadStream::ColumnBinary() RSqlColumnReadStream::ColumnClose()
 +
 +
|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]]) -->
 +
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
 +
|author=[[User:Aknyman]]
 
}}
 
}}
  

Revision as of 11:25, 24 June 2011


Template:KBCS

Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Compatibility
Platform(s): S60 3rd Edition, FP2
S60 3rd Edition FP2
Platform Security
Capabilities: )
Article
Keywords: RSqlDatabase,RSqlStatement,RSqlParamWriteStream, RSqlColumnReadStream, RSqlParamWriteStream::BindInt(), RSqlParamWriteStream::BindText(), RSqlParamWriteStream::BindBinary(), RSqlParamWriteStream::Close(), RSqlColumnReadStream::ColumnText() RSqlColumnReadStream::ColumnBinary() RSqlColumnReadStream::ColumnClose()
Created: aknyman (02 May 2008)
Last edited: hamishwillee (24 Jun 2011)

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.

MMP file

The following libraries are required:

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


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

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.

See also

56 page views in the last 30 days.
×