RSqlStatement Class Reference

class RSqlStatement

Represents an SQL statement.

An object of this type can be used to execute all types of SQL statements; this includes SQL statements with parameters.

If a SELECT statament is passed to RSqlStatement::Prepare(), then the returned record set is forward only, non-updateable.

There are a number of ways that this object is used; here are some examples.

CASE 1 - the execution of a SQL statement, which does not return record set:

RSqlDatabase database;
.........
RSqlStatement stmt;
TInt err = stmt.Prepare(database, _L("INSERT INTO Tbl1(Fld1) VALUES(:Val)"));
TInt paramIndex = stmt.ParameterIndex(_L(":Val"));
for(TInt i=1;i<=10;++i)
	{
	err = stmt.BindInt(paramIndex, i);
	err = stmt.Exec();
	err = stmt.Reset();
	}
stmt.Close();

The following pseudo code shows the general pattern:

<RSqlStatement::Prepare()>
[begin:]
<RSqlStatement::Bind<param_type>()>
<RSqlStatement::Exec()>
[<RSqlStatement::Reset()>]
[<RSqlStatement::Bind<param_type>()>]
[<Goto :begin>]

CASE 2 - the execution of a SQL statement, which returns a record set:

RSqlDatabase database;
.........
RSqlStatement stmt;
TInt err = stmt.Prepare(database, _L("SELECT Fld1 FROM Tbl1 WHERE Fld1 > :Val"));
TInt paramIndex = stmt.ParameterIndex(_L(":Val"));
err = stmt.BindInt(paramIndex, 5);
TInt columnIndex = stmt.ColumnIndex(_L("Fld1"));
while((err = stmt.Next()) == KSqlAtRow)
	{
	TInt val = stmt.ColumnInt(columnIndex);
	RDebug::Print(_L("val=%d\n"), val);
	}
if(err == KSqlAtEnd)
	<OK - no more records>;
else
	<process the error>;
stmt.Close();

The following pseudo code shows the general pattern:

<RSqlStatement::Prepare()>
[begin:]
<while (RSqlStatement::Next() == KSqlAtRow)>
	<do something with the records>
if(err == KSqlAtEnd)
	<OK - no more records>;
else
	<process the error>;
[<RSqlStatement::Reset()>]
[<RSqlStatement::Bind<param_type>()>]
[<Goto begin>]

CASE 3.1 - SELECT statements: large column data processing, where the data is copied into a buffer supplied by the client:

RSqlDatabase database;
.........
RSqlStatement stmt;
TInt err = stmt.Prepare(database, _L("SELECT BinaryField FROM Tbl1"));
TInt columnIndex = stmt.ColumnIndex(_L("BinaryField"));
while((err = stmt.Next()) == KSqlAtRow)
	{
	TInt size = stmt. ColumnSize(columnIndex);
	HBufC8* buf = HBufC8::NewL(size);
	err = stmt.ColumnBinary(columnIndex, buf->Ptr());
	<do something with the data>;
	delete buf;
	}
if(err == KSqlAtEnd)
	<OK - no more records>;
else
	<process the error>;
stmt.Close();

CASE 3.2 - SELECT statements: large column data processing, where the data is accessed by the client without copying:

RSqlDatabase database;
.........
RSqlStatement stmt;
TInt err = stmt.Prepare(database, _L("SELECT BinaryField FROM Tbl1"));
TInt columnIndex = stmt.ColumnIndex(_L("BinaryField"));
while((err = stmt.Next()) == KSqlAtRow)
	{
	TPtrC8 data = stmt.ColumnBinaryL(columnIndex);
	<do something with the data>;
	}
if(err == KSqlAtEnd)
	<OK - no more records>;
else
	<process the error>;
stmt.Close();

CASE 3.3 - SELECT statements, large column data processing (the data is accessed by the client without copying), leaving-safe processing:

RSqlDatabase database;
.........
RSqlStatement stmt;
TInt err = stmt.Prepare(database, _L("SELECT BinaryField FROM Tbl1"));
TInt columnIndex = stmt.ColumnIndex(_L("BinaryField"));
while((err = stmt.Next()) == KSqlAtRow)
	{
	TPtrC8 data;
	TInt err = stmt.ColumnBinary(columnIndex, data);
	if(err == KErrNone)
		{
		<do something with the data>;
		}
	}
if(err == KSqlAtEnd)
	<OK - no more records>;
else
	<process the error>;
stmt.Close();

CASE 3.4 - SELECT statements: large column data processing, where the data is accessed by the client using a stream:

RSqlDatabase database;
.........
RSqlStatement stmt;
TInt err = stmt.Prepare(database, _L("SELECT BinaryField FROM Tbl1"));
TInt columnIndex = stmt.ColumnIndex(_L("BinaryField"));
while((err = stmt.Next()) == KSqlAtRow)
	{
	RSqlColumnReadStream stream;
	err = stream.ColumnBinary(stmt, columnIndex);
	<do something with the data in the stream>;
	stream.Close();
	}
if(err == KSqlAtEnd)
	<OK - no more records>;
else
	<process the error>;
stmt.Close();

CASE 4 - the execution of a SQL statement with parameter(s), some of which may be large text or binary values:

RSqlDatabase database;
.........
RSqlStatement stmt;
TInt err = 
	stmt.Prepare(database, _L("UPDATE Tbl1 SET LargeTextField = :LargeTextVal WHERE IdxField = :KeyVal"));
TInt paramIndex1 = stmt.ParameterIndex(_L(":LargeTextVal"));
TInt paramIndex2 = stmt.ParameterIndex(_L(":KeyVal"));
for(TInt i=1;i<=10;++i)
	{
	RSqlParamWriteStream stream;
	err = stream.BindText(stmt, paramIndex1);
	<insert large text data into the stream>;
	stream.Close();
	err = stmt.BindInt(paramIndex2, i);
	err = stmt.Exec();
	stmt.Reset();
	}
stmt.Close();

The following table shows what is returned when the caller uses a specific column data retrieving function on a specific column type.

--------------------------------------------------------------------------------
Column type | ColumnInt() ColumnInt64() ColumnReal() ColumnText() ColumnBinary()
--------------------------------------------------------------------------------
Null........|.0...........0.............0.0..........KNullDesC....KNullDesC8
Int.........|.Int.........Int64.........Real.........KNullDesC....KNullDesC8 
Int64.......|.clamp.......Int64.........Real.........KNullDesC....KNullDesC8 
Real........|.round.......round.........Real.........KNullDesC....KNullDesC8 
Text........|.0...........0.............0.0..........Text.........KNullDesC8   
Binary......|.0...........0.............0.0..........KNullDesC....Binary
--------------------------------------------------------------------------------
Note the following definitions:
  • "clamp": return KMinTInt or KMaxTInt if the value is outside the range that can be represented by the type returned by the accessor function.

  • "round": the floating point value will be rounded up to the nearest integer. If the result is outside the range that can be represented by the type returned by the accessor function, then it will be clamped.

Note that when handling blob and text data over 2Mb in size it is recommended that the RSqlBlobReadStream and RSqlBlobWriteStream classes or the TSqlBlob class is used instead. These classes provide a more RAM-efficient way of reading and writing large amounts of blob or text data from a database.

KMinTInt

KMaxTInt

KNullDesC

KNullDesC8

RSqlBlobReadStream

RSqlBlobWriteStream

TSqlBlob

Public Member Functions
RSqlStatement()
IMPORT_C TBoolAtRow()
IMPORT_C TIntBindBinary(TInt, const TDesC8 &)
IMPORT_C TIntBindInt(TInt, TInt)
IMPORT_C TIntBindInt64(TInt, TInt64)
IMPORT_C TIntBindNull(TInt)
IMPORT_C TIntBindReal(TInt, TReal)
IMPORT_C TIntBindText(TInt, const TDesC &)
IMPORT_C TIntBindZeroBlob(TInt, TInt)
IMPORT_C voidClose()
IMPORT_C TIntColumnBinary(TInt, TPtrC8 &)
IMPORT_C TIntColumnBinary(TInt, TDes8 &)
IMPORT_C TPtrC8ColumnBinaryL(TInt)
IMPORT_C TIntColumnCount()
IMPORT_C TIntColumnIndex(const TDesC &)
IMPORT_C TIntColumnInt(TInt)
IMPORT_C TInt64ColumnInt64(TInt)
IMPORT_C TIntColumnName(TInt, TPtrC &)
IMPORT_C TRealColumnReal(TInt)
IMPORT_C TIntColumnSize(TInt)
IMPORT_C TIntColumnText(TInt, TPtrC &)
IMPORT_C TIntColumnText(TInt, TDes &)
IMPORT_C TPtrCColumnTextL(TInt)
IMPORT_C TSqlColumnTypeColumnType(TInt)
IMPORT_C TIntDeclaredColumnType(TInt, TSqlColumnType &)
IMPORT_C TIntExec()
IMPORT_C voidExec(TRequestStatus &)
IMPORT_C TBoolIsNull(TInt)
IMPORT_C TIntNext()
IMPORT_C TIntParamName(TInt, TPtrC &)
IMPORT_C TIntParameterIndex(const TDesC &)
IMPORT_C TIntParameterName(TInt, TPtrC &)
IMPORT_C TIntPrepare(RSqlDatabase &, const TDesC &)
IMPORT_C TIntPrepare(RSqlDatabase &, const TDesC8 &)
IMPORT_C voidPrepareL(RSqlDatabase &, const TDesC &)
IMPORT_C voidPrepareL(RSqlDatabase &, const TDesC8 &)
IMPORT_C TIntReset()
Private Member Functions
CSqlStatementImpl &Impl()
Private Attributes
CSqlStatementImpl *iImpl

Constructor & Destructor Documentation

RSqlStatement()

IMPORT_CRSqlStatement()

Member Functions Documentation

AtRow()

IMPORT_C TBoolAtRow()const

BindBinary(TInt, const TDesC8 &)

IMPORT_C TIntBindBinary(TIntaParameterIndex,
const TDesC8 &aParameterData
)

Parameters

TInt aParameterIndex
const TDesC8 & aParameterData

BindInt(TInt, TInt)

IMPORT_C TIntBindInt(TIntaParameterIndex,
TIntaParameterValue
)

Parameters

TInt aParameterIndex
TInt aParameterValue

BindInt64(TInt, TInt64)

IMPORT_C TIntBindInt64(TIntaParameterIndex,
TInt64aParameterValue
)

Parameters

TInt aParameterIndex
TInt64 aParameterValue

BindNull(TInt)

IMPORT_C TIntBindNull(TIntaParameterIndex)

Parameters

TInt aParameterIndex

BindReal(TInt, TReal)

IMPORT_C TIntBindReal(TIntaParameterIndex,
TRealaParameterValue
)

Parameters

TInt aParameterIndex
TReal aParameterValue

BindText(TInt, const TDesC &)

IMPORT_C TIntBindText(TIntaParameterIndex,
const TDesC &aParameterText
)

Parameters

TInt aParameterIndex
const TDesC & aParameterText

BindZeroBlob(TInt, TInt)

IMPORT_C TIntBindZeroBlob(TIntaParameterIndex,
TIntaBlobSize
)

Parameters

TInt aParameterIndex
TInt aBlobSize

Close()

IMPORT_C voidClose()

ColumnBinary(TInt, TPtrC8 &)

IMPORT_C TIntColumnBinary(TIntaColumnIndex,
TPtrC8 &aPtr
)const

Parameters

TInt aColumnIndex
TPtrC8 & aPtr

ColumnBinary(TInt, TDes8 &)

IMPORT_C TIntColumnBinary(TIntaColumnIndex,
TDes8 &aDest
)const

Parameters

TInt aColumnIndex
TDes8 & aDest

ColumnBinaryL(TInt)

IMPORT_C TPtrC8ColumnBinaryL(TIntaColumnIndex)const

Parameters

TInt aColumnIndex

ColumnCount()

IMPORT_C TIntColumnCount()const

ColumnIndex(const TDesC &)

IMPORT_C TIntColumnIndex(const TDesC &aColumnName)const

Parameters

const TDesC & aColumnName

ColumnInt(TInt)

IMPORT_C TIntColumnInt(TIntaColumnIndex)const

Parameters

TInt aColumnIndex

ColumnInt64(TInt)

IMPORT_C TInt64ColumnInt64(TIntaColumnIndex)const

Parameters

TInt aColumnIndex

ColumnName(TInt, TPtrC &)

IMPORT_C TIntColumnName(TIntaColumnIndex,
TPtrC &aNameDest
)

Parameters

TInt aColumnIndex
TPtrC & aNameDest

ColumnReal(TInt)

IMPORT_C TRealColumnReal(TIntaColumnIndex)const

Parameters

TInt aColumnIndex

ColumnSize(TInt)

IMPORT_C TIntColumnSize(TIntaColumnIndex)const

Parameters

TInt aColumnIndex

ColumnText(TInt, TPtrC &)

IMPORT_C TIntColumnText(TIntaColumnIndex,
TPtrC &aPtr
)const

Parameters

TInt aColumnIndex
TPtrC & aPtr

ColumnText(TInt, TDes &)

IMPORT_C TIntColumnText(TIntaColumnIndex,
TDes &aDest
)const

Parameters

TInt aColumnIndex
TDes & aDest

ColumnTextL(TInt)

IMPORT_C TPtrCColumnTextL(TIntaColumnIndex)const

Parameters

TInt aColumnIndex

ColumnType(TInt)

IMPORT_C TSqlColumnTypeColumnType(TIntaColumnIndex)const

Parameters

TInt aColumnIndex

DeclaredColumnType(TInt, TSqlColumnType &)

IMPORT_C TIntDeclaredColumnType(TIntaColumnIndex,
TSqlColumnType &aColumnType
)const

Parameters

TInt aColumnIndex
TSqlColumnType & aColumnType

Exec()

IMPORT_C TIntExec()

Exec(TRequestStatus &)

IMPORT_C voidExec(TRequestStatus &aStatus)

Parameters

TRequestStatus & aStatus

Impl()

CSqlStatementImpl &Impl()const [private]

IsNull(TInt)

IMPORT_C TBoolIsNull(TIntaColumnIndex)const

Parameters

TInt aColumnIndex

Next()

IMPORT_C TIntNext()

ParamName(TInt, TPtrC &)

IMPORT_C TIntParamName(TIntaParameterIndex,
TPtrC &aNameDest
)

Parameters

TInt aParameterIndex
TPtrC & aNameDest

ParameterIndex(const TDesC &)

IMPORT_C TIntParameterIndex(const TDesC &aParameterName)const

Parameters

const TDesC & aParameterName

ParameterName(TInt, TPtrC &)

IMPORT_C TIntParameterName(TIntaParameterIndex,
TPtrC &aNameDest
)

Parameters

TInt aParameterIndex
TPtrC & aNameDest

Prepare(RSqlDatabase &, const TDesC &)

IMPORT_C TIntPrepare(RSqlDatabase &aDatabase,
const TDesC &aSqlStmt
)

Parameters

RSqlDatabase & aDatabase
const TDesC & aSqlStmt

Prepare(RSqlDatabase &, const TDesC8 &)

IMPORT_C TIntPrepare(RSqlDatabase &aDatabase,
const TDesC8 &aSqlStmt
)

Parameters

RSqlDatabase & aDatabase
const TDesC8 & aSqlStmt

PrepareL(RSqlDatabase &, const TDesC &)

IMPORT_C voidPrepareL(RSqlDatabase &aDatabase,
const TDesC &aSqlStmt
)

Parameters

RSqlDatabase & aDatabase
const TDesC & aSqlStmt

PrepareL(RSqlDatabase &, const TDesC8 &)

IMPORT_C voidPrepareL(RSqlDatabase &aDatabase,
const TDesC8 &aSqlStmt
)

Parameters

RSqlDatabase & aDatabase
const TDesC8 & aSqlStmt

Reset()

IMPORT_C TIntReset()

Member Data Documentation

CSqlStatementImpl * iImpl

CSqlStatementImpl *iImpl[private]