This tutorial shows you how to create a simple SQL database query.
This tutorial shows you how to wrap a SQL query statement into an RSqlStatement object to query a database.
This tutorial uses code from the Basic SQL example application.
Assumptions
You have a database. The database has no tables and therefore no data.
SQL statements
The following SQL statements are used for this example:
SELECT person FROM Pets WHERE cat >= 1
The (SELECT) results of the query will be the value in the 'name' column FROM the 'countries' table WHERE the value of the 'population' column of the same record is > the value specified.
Prepare the statement:
The steps to prepare a SQL statement are shown here.
Set up some constants used by the SQL statement object to define the SQL query:
This defines the query parameters.
Instantiate the RSqlStatement SQL statement:
Prepare the statement:
Creates a parameterised SQL statement executable.
Define the indices to be used in the search:
Execute the SQL query:
The Symbian SQL statement is executed by RSqlStatement::Next().
Do something with the results:
The query is done and you have the results. In this section we look at a simple way to do something with the results and we close the SQL statement object.
The tutorial has demonstrated how to query a Symbian SQL database. Looking through the example application you can work out how easily the query can be changed to meet specific requirements and how the results can be used in different ways.
The following code snippet is from the basic example application.
... ... RSqlStatement stmt; iConsole->Printf(_L("Running Query:\n%S\n"), &aStatement); user::LeaveIfError(stmt.Prepare(iPetDb, aStatement)); CleanupClosePushL(stmt); TInt personIndex = stmt.ColumnIndex(KPerson); TInt rc = KErrNone; while ((rc = stmt.Next()) == KSqlAtRow) { // Do something with the results TPtrC myData = stmt.ColumnTextL(personIndex); // read return data iConsole->Printf(_L("Person=%S\n"), &myData); } if (rc != KSqlAtEnd) { _LIT(KErrSQLError, "Error %d returned from RSqlStatement::Next()."); iConsole->Printf(KErrSQLError, rc); } ... CleanupStack::PopAndDestroy(1); }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.