examples/SysLibs/DBMS/Basics/Basics.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // DBMS Interface to Databases - Basic Creation and Use
00015 // Example to demonstrate basic database creation and use
00016 //
00017 
00018 
00019 #include "CommonToDBMSEx.h"
00020         
00021                                 // Create a database and its structure
00022 static void doMakeDatabaseL(const TDesC& aDatabaseFileName);
00023 
00024                                 // Add some records to the database
00025 static void doAddDataL(const TDesC& aDatabaseFileName);
00026 
00027                                 // Display the rows
00028 static void doShowL(const TDesC& aHeading,RDbRowSet& aSet);
00029 
00030                                 // Create the CDs table
00031 static void doCreateTableL(RDbDatabase& aDatabase);
00032 
00033                                 // Create the CDs index
00034 static void doCreateIndexL(RDbDatabase& aDatabase);
00035 
00036 
00037 //  Do the example
00038 static void doExampleL()
00039     {
00040         _LIT(KDbCreateDb,"dbcreate.db");
00041 
00042                                 // The database dbcreate.db is created as a permanent file store 
00043                                 // in the app's private directory
00044         User::LeaveIfError(fsSession.CreatePrivatePath(RFs::GetSystemDrive()));
00045         TFileName name;
00046         User::LeaveIfError(fsSession.PrivatePath(name));
00047         name.Append(KDbCreateDb);
00048                                 // create database
00049         doMakeDatabaseL(name);
00050                                 // add items
00051         doAddDataL(name);
00052         }
00053 
00054 
00055 static void doMakeDatabaseL(const TDesC& aDatabaseFileName)
00056         {
00057                                 // construct a file store object - the file to contain the
00058                                 // database replaces any existing file of the same name.
00059         CFileStore* store = CPermanentFileStore::ReplaceLC(fsSession,aDatabaseFileName,EFileRead|EFileWrite);
00060                                 // Complete file store creation
00061         store->SetTypeL(store->Layout());
00062 
00063                                 // Create a database in the store
00064         RDbStoreDatabase database;
00065         TStreamId id=database.CreateL(store);
00066                                 // NB. The database won't be closed on failure
00067                                 //     Cleanup can be done; more usually database objects
00068                                 //     are not automatic variables.
00069 
00070                                 // Keep database id as root of store
00071         store->SetRootL(id);
00072 
00073                                 // Complete database creation by commiting the store
00074         store->CommitL();
00075 
00076                                 // create the CDs table
00077         doCreateTableL(database);
00078 
00079                                 // create an index
00080         doCreateIndexL(database);
00081 
00082                                 // close the database
00083         database.Close();
00084 
00085                                 // Do not commit store: database has taken control of commit
00086         CleanupStack::PopAndDestroy();
00087         }
00088 
00089 
00090 static void doAddDataL(const TDesC& aDatabaseFileName)
00091         {
00092         _LIT(KSQLStatement,"select title,artist,price from CDs order by artist,title");
00093         _LIT(KComposition1, "Enigma Variations");
00094         _LIT(KComposer1, "Elgar");
00095         _LIT(KComposition2, "Symphony no. 5");
00096         _LIT(KComposer2, "Beethoven");
00097 
00098                                 // Open the file store
00099         CFileStore* store = CFileStore::OpenLC(fsSession,aDatabaseFileName,EFileRead|EFileWrite);
00100 
00101                                 // open the database from the root stream
00102         RDbStoreDatabase database;
00103         database.OpenL(store,store->Root());
00104 
00105                                 // create a view on the database
00106         RDbView view;
00107         User::LeaveIfError(view.Prepare(database,TDbQuery(KSQLStatement,EDbCompareNormal)));
00108         User::LeaveIfError(view.EvaluateAll());
00109                                 // NB. a similar caveat about cleanup applies as mentioned above
00110 
00111                                 // insert a row
00112         view.InsertL();
00113                                 // column numbers match the order specified in the select statement
00114                                 // data type must match almost exactly (a few exceptions)
00115         
00116         view.SetColL(1,KComposition1);
00117         view.SetColL(2,KComposer1);
00118         view.SetColL(3,1299);
00119                                 // complete the insertion
00120         view.PutL();
00121 
00122                                 // insert another row
00123         view.InsertL();
00124 
00125 
00126         view.SetColL(1,KComposition2);
00127         view.SetColL(2,KComposer2);
00128         view.SetColL(3,1499);
00129                                 // complete the insertion
00130         view.PutL();
00131 
00132                                 // show the records
00133         _LIT(KShowInfo, "Rowset contents...");
00134         doShowL(KShowInfo, view);
00135 
00136                                 // close the view
00137         view.Close();
00138                                 
00139                                 // close the database
00140         database.Close();
00141 
00142                                 // Do not commit store: database has taken control of commit
00143         CleanupStack::PopAndDestroy();
00144         }
00145 
00146 
00147 static void doShowL(const TDesC& aHeading,RDbRowSet& aSet)
00148         {
00149         _LIT(KNewline, "\n") ;
00150         _LIT(KRowHeadingFormatter, "\n%d row(s):");
00151         _LIT(KRowFormatter, "\n  %S, %S:  %d.%02.2d");
00152 
00153         console->Printf(KNewline);
00154         console->Printf(aHeading);
00155 
00156         console->Printf(KRowHeadingFormatter,aSet.CountL());
00157                                 // iterate across the row set
00158         for (aSet.FirstL();aSet.AtRow();aSet.NextL())
00159                 {
00160                                 // retrieve the row
00161                 aSet.GetL();
00162                                 // while the rowset is on this row, can use a TPtrC to
00163                                 // refer to any text columns
00164                 TPtrC title=aSet.ColDes(1);
00165                 TPtrC artist=aSet.ColDes(2);
00166 
00167                                 // simple currency formatting; not locale-dependent
00168                 TInt pricePounds=(aSet.ColInt(3)/100);
00169                 TInt pricePence=(aSet.ColInt(3)-(pricePounds*100));
00170                 console->Printf(KRowFormatter,&artist,&title,pricePounds,pricePence);
00171                 }
00172         console->Printf(KNewline);
00173         }
00174 
00175 
00176 static void doCreateTableL(RDbDatabase& aDatabase)
00177         {
00178         _LIT(KCol1, "Artist");
00179         _LIT(KCol2, "Title");
00180         _LIT(KCol3, "Price");
00181         _LIT(KTable, "CDs");
00182 
00183                                 // Create a table definition
00184         CDbColSet* columns=CDbColSet::NewLC();
00185 
00186                                 // add the columns
00187                                 // text columns default to 50 characters (variable length)
00188         columns->AddL(TDbCol(KCol1,EDbColText));
00189 
00190                                 // specify an alternative length
00191         TDbCol title(KCol2,EDbColText,60);
00192         title.iAttributes=TDbCol::ENotNull;
00193         columns->AddL(title);
00194 
00195         TDbCol price(KCol3,EDbColInt32);
00196 
00197                                 // columns are Nullable by default
00198         price.iAttributes=TDbCol::ENotNull;
00199         columns->AddL(price);
00200 
00201                                 // Create a table
00202         User::LeaveIfError(aDatabase.CreateTable(KTable,*columns));
00203                                 
00204                 // cleanup the column set
00205         CleanupStack::PopAndDestroy();
00206         }
00207 
00208 
00209 static void doCreateIndexL(RDbDatabase& aDatabase)
00210         {
00211         _LIT(KCol1, "Artist");
00212         _LIT(KCol2, "Title");
00213         _LIT(KTable, "CDs");
00214 
00215                                 // create the index key
00216         CDbKey* key=CDbKey::NewLC();
00217 
00218                                 // add the key columns
00219         TDbKeyCol artist(KCol1);
00220         key->AddL(artist);
00221         TDbKeyCol title(KCol2);
00222         key->AddL(title);
00223 
00224                                 // create the index
00225         User::LeaveIfError(aDatabase.CreateIndex(KTable,KTable,*key));
00226 
00227                                 // cleanup the key
00228         CleanupStack::PopAndDestroy();
00229         }

Generated on Thu Jan 21 10:32:55 2010 for TB10.1 Example Applications by  doxygen 1.5.3