一、主要的类:
RDbStoreDatabase
RDbNamedDatabase
TDbCol
RDbView
.......
二、创建数据库:
RDbStoreDatabase:不共享,客户端专有
RDbNamedDatabase:共享,客户端专有与客户/服务器访问
//-------------
RDbStoreDatabase:
class CBookDb : public CBase
{
...
private: // Member data
RFs iFsSession;
RDbStoreDatabase iBookDb;
CFileStore* iFileStore;
...
};
TInt CBookDb::CreateDb(const TFileName& aNewBookFile)
{
Close();
// Create empty database file.
TRAPD(error,
iFileStore = CPermanentFileStore::ReplaceL(iFsSession,
aNewBookFile, EFileRead|EFileWrite);
iFileStore->SetTypeL(iFileStore->Layout());// Set file store type
TStreamId id = iBookDb.CreateL(iFileStore);// Create stream object
iFileStore->SetRootL(id);// Keep database ID as root of store
iFileStore->CommitL();// Complete creation by committing
// Create Book tables and indexes
CreateBooksTableL();
CreateBooksIndexL();
);
//-----------------
_LIT(KDbName,"C://Private//<SID>//DBMS.dat");
_LIT(KDbName,"C://Data//<Some_path_element>//DBMS.dat");
const int KTitleMaxLength = 60;
_LIT(KBooksTable, "Books");
_LIT(KBooksAuthorCol, "Author");
_LIT(KBooksTitleCol, "Title");
_LIT(KBooksDescriptionCol, "Description");
RFs iFsSession;
RDbNamedDatabase iBookDb;
...
TInt CBookDb::CreateDb(const TFileName& aNewBookFile)
{
Close();
TInt error=iBookDb.Replace(iFsSession, aNewBookFile);
if(error!=KErrNone)
{
return error;
}
// Specify columns for Books table
TDbCol authorCol(KBooksAuthorCol, EDbColText); // Default length
TDbCol titleCol(KBooksTitleCol, EDbColText, KTitleMaxLength);
titleCol.iAttributes = TDbCol::ENotNull;
// Stream Data
TDbCol descriptionCol(KBooksDescriptionCol, EDbColLongText);
// Create columnset
CDbColSet* bookColSet = CDbColSet::NewLC();
bookColSet->AddL(authorCol);
bookColSet->AddL(titleCol);
bookColSet->AddL(descriptionCol);
// Create the Books table
User::LeaveIfError(iBookDb.CreateTable(KBooksTable,
*bookColSet));
CleanupStack::PopAndDestroy(bookColSet);
}
三、打开数据库:
iItemsDatabase.Close();
User::LeaveIfError(iItemsDatabase.Open(iFsSession,DBFileName));
四、读数据
_LIT(KViewSql,"SELECT * FROM Books"
RDbView view;
iBookDb.Begin();
User::LeaveIfError(view.Prepare(iBookDb, TDbQuery(KViewSql),view.EReadOnly));
CleanupClosePushL(view);
User::LeaveIfError(view.EvaluateAll());
for (view.FirstL(); view.AtRow(); view.NextL())
{
view.GetL(); // Fetch a cached copy of current row
... // perform operations for the row
view.ColInt(1);
view.ColDes(2);
view.ColTime(3);
}
CleanupStack::PopAndDestroy(); // This also closes the view
五、删除数据
for (view.FirstL(); view.AtRow(); view.NextL())
{
view.DeleteL();
}
iBookDB.Commit();
iBookDB.Compact();
六、更新数据
for (view.FirstL(); view.AtRow(); view.NextL())
{
view.UpdateL();
view.SetColL(2,aName);
view.SetColL(3,aTime);
......
view.PutL();
}
RDbStoreDatabase
RDbNamedDatabase
TDbCol
RDbView
.......
二、创建数据库:
RDbStoreDatabase:不共享,客户端专有
RDbNamedDatabase:共享,客户端专有与客户/服务器访问
//-------------
RDbStoreDatabase:
class CBookDb : public CBase
{
...
private: // Member data
RFs iFsSession;
RDbStoreDatabase iBookDb;
CFileStore* iFileStore;
...
};
TInt CBookDb::CreateDb(const TFileName& aNewBookFile)
{
Close();
// Create empty database file.
TRAPD(error,
iFileStore = CPermanentFileStore::ReplaceL(iFsSession,
aNewBookFile, EFileRead|EFileWrite);
iFileStore->SetTypeL(iFileStore->Layout());// Set file store type
TStreamId id = iBookDb.CreateL(iFileStore);// Create stream object
iFileStore->SetRootL(id);// Keep database ID as root of store
iFileStore->CommitL();// Complete creation by committing
// Create Book tables and indexes
CreateBooksTableL();
CreateBooksIndexL();
);
//-----------------
_LIT(KDbName,"C://Private//<SID>//DBMS.dat");
_LIT(KDbName,"C://Data//<Some_path_element>//DBMS.dat");
const int KTitleMaxLength = 60;
_LIT(KBooksTable, "Books");
_LIT(KBooksAuthorCol, "Author");
_LIT(KBooksTitleCol, "Title");
_LIT(KBooksDescriptionCol, "Description");
RFs iFsSession;
RDbNamedDatabase iBookDb;
...
TInt CBookDb::CreateDb(const TFileName& aNewBookFile)
{
Close();
TInt error=iBookDb.Replace(iFsSession, aNewBookFile);
if(error!=KErrNone)
{
return error;
}
// Specify columns for Books table
TDbCol authorCol(KBooksAuthorCol, EDbColText); // Default length
TDbCol titleCol(KBooksTitleCol, EDbColText, KTitleMaxLength);
titleCol.iAttributes = TDbCol::ENotNull;
// Stream Data
TDbCol descriptionCol(KBooksDescriptionCol, EDbColLongText);
// Create columnset
CDbColSet* bookColSet = CDbColSet::NewLC();
bookColSet->AddL(authorCol);
bookColSet->AddL(titleCol);
bookColSet->AddL(descriptionCol);
// Create the Books table
User::LeaveIfError(iBookDb.CreateTable(KBooksTable,
*bookColSet));
CleanupStack::PopAndDestroy(bookColSet);
}
三、打开数据库:
iItemsDatabase.Close();
User::LeaveIfError(iItemsDatabase.Open(iFsSession,DBFileName));
四、读数据
_LIT(KViewSql,"SELECT * FROM Books"
RDbView view;
iBookDb.Begin();
User::LeaveIfError(view.Prepare(iBookDb, TDbQuery(KViewSql),view.EReadOnly));
CleanupClosePushL(view);
User::LeaveIfError(view.EvaluateAll());
for (view.FirstL(); view.AtRow(); view.NextL())
{
view.GetL(); // Fetch a cached copy of current row
... // perform operations for the row
view.ColInt(1);
view.ColDes(2);
view.ColTime(3);
}
CleanupStack::PopAndDestroy(); // This also closes the view
五、删除数据
for (view.FirstL(); view.AtRow(); view.NextL())
{
view.DeleteL();
}
iBookDB.Commit();
iBookDB.Compact();
六、更新数据
for (view.FirstL(); view.AtRow(); view.NextL())
{
view.UpdateL();
view.SetColL(2,aName);
view.SetColL(3,aTime);
......
view.PutL();
}