| ID | CS000926 | Creation date | May 2, 2008 |
| Platform | S60 3rd Edition, FP2 | Tested on devices | Nokia 6220 Classic |
| Category | Symbian C++ | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): RSqlDatabase, RSqlStatement, RSqlDatabase::Open(), RSqlDatabase::Attach(), RSqlDatabase::Detach() RSqlStatement::Prepare(), RSqlStatement::ColumnIndex(), RSqlStatement::ColumnTextL(), RSqlStatement::Next(), RSqlStatement::Close() |
This code snippet shows how to attach a database to another database. Attaching a database means making it appear to be part of the primary database. This gives an opportunity to temporarily view multiple databases as if they were a single database.
The RSqlDatabase::Attach() method requires two parameters. The first one is the name of the database to be attached and the second one is a logical name to be assigned to the database. If both databases have tables with the same name, the logical database name allows to explicitly scope the right one.
The RSqlDatabase::Detach() method requires the logical name of the database as a parameter, because it is possible to attach multiple databases to one primary database. RSqlDatabase::Detach() needs the information of which database to detach.
This snippet can be self-signed.
The following libraries are required:
LIBRARY euser.lib
LIBRARY sqldb.lib
The following capabilities are needed to test the secure database in this example:
CAPABILITY ReadUserData
CAPABILITY WriteUserData
Databases nonsecure.db and [UID3]secure.db need to be created before executing this code snippet. See CS000925 - Using SQL API for creating non-secure and secure databases.
#include <e32base.h>
#include <SqlDb.h>
void AttachDatabasesL()
{
RSqlDatabase database;
_LIT(KNonSecureDbName, "\\nonsecure.db");
_LIT(KSecureDbName, "[E80000AF]secure.db");
_LIT(KLogicalName,"ATTACHED");
TInt error = database.Open(KNonSecureDbName);
if (error == KErrNone)
{
CleanupClosePushL(database);
User::LeaveIfError(database.Attach(KSecureDbName,KLogicalName));
_LIT(KSqlAttachSelect,"SELECT BOOKS.TITLE FROM ATTACHED.BOOKS WHERE BOOKS.TITLE =
(SELECT TITLE FROM MOVIES)");
RSqlStatement sqlAttachSelectStatement;
TInt ret = sqlAttachSelectStatement.Prepare(database, KSqlAttachSelect);
TInt columnIndex = sqlAttachSelectStatement.ColumnIndex(_L("TITLE"));
if (ret == KErrNone)
{
CleanupClosePushL(sqlAttachSelectStatement);
TInt err = KErrNone;
while((err = sqlAttachSelectStatement.Next()) == KSqlAtRow)
{
//process next record
TPtrC title = sqlAttachSelectStatement.ColumnTextL(columnIndex);
//do something with title...
}
if(err == KSqlAtEnd)
{
//no more records
}
else
{
//error has occured
}
CleanupStack::PopAndDestroy(); //sqlAttachSelectStatement
}
else
{
//prepare sql statement failed
}
database.Detach(KLogicalName);
CleanupStack::PopAndDestroy(); //database
}
else
{
//open database failed
}
}
The database [UID3]secure.db has been attached to nonsecure.db (the primary database) and all 'TITLE' column values from [UID3]secure.db/BOOKS table that exist in the nonsecure.db/MOVIES 'TITLE' column have been fetched. Finally, the other database is detached and the primary database is closed.
No related wiki articles found