基于Symbian OS的通讯录操作总结

用于记录通讯信息的数据库,存在于“/system/data/”目录下,文件名为“contacts.cdb”。主要操作有:

1、打开数据库:

CContactDatabase* database=CContactDatabase::OpenL( );

CleanupStack::PushL(database);……..CleanupStack::PopAndDestroy(database);

2、添加新记录

  1、建立一个新记录:

        CContactItem* sys_template=

database->CreateContactCardTemplateL(_L(“Contacts”),EFalse);        

CContactCard* newCard=CContactCard::NewL(sys_template);

   //database中复制一个模板到新记录中

        设置字段组引用变量:

CContactItemFieldSet& currentFieldSet=newCard->CardFields( );

        求得字段数:

const Tuint fieldCount=currentFieldSet.Count( );

        设置字段引用变量:

CContactItemField& field=currentFieldSet[n];

        设置字段类型:

const CContentType& type=field.ContentType( );

type.ContentsFieldType(KUidContactFieldGivenName)

        向字段存入数据:

CContactTextField* textStorage=field.TextStorage( );

textStorage->SetTextL(buf);

        向数据库中插入新记录:

             cardId=database->AddNewContactL(*newCard);

      2// 字符串声明

_LIT(KForenameLabel,"Forename");

_LIT(KSurnameLabel,"Surname");

_LIT(KWorkPhoneLabel,"Work Phone");

_LIT(KForename,"Steve");

_LIT(KOtherForename,"Bob");

_LIT(KSurname,"Wilkinson");

_LIT(KWorkPhone,"+441617779700");

//打开默认数据库

CContactDatabase* contactsDb = CContactDatabase::OpenL();

CleanupStack::PushL(contactsDb);

// 建立一个新记录,并为该记录添加字段:

CContactItem* contact = CContactCard::NewLC();

CContactItemField* field =

CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);

field->SetMapping(KUidContactFieldVCardMapUnusedN);

             //建立与vCard的映射

field->SetLabelL(KSurnameLabel);//设置字段标签

field->TextStorage()->SetTextL(KSurname);//设置字段值

contact->AddFieldL(*field);//把该字段加入到记录中

      

CleanupStack::Pop();

 

//添加givenname字段

field=CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);

field->SetMapping(KUidContactFieldVCardMapUnusedN);

field->SetLabelL(KForenameLabel);

field->TextStorage()->SetTextL(KForename);

contact->AddFieldL(*field);

 

CleanupStack::Pop();

 

//添加phonenumber字段

field=CContactItemField::NewLC(KStorageTypeText, KUidContactFieldPhoneNumber);

field->SetMapping(KUidContactFieldVCardMapTEL);

field->SetLabelL(KWorkPhoneLabel);

field->TextStorage()->SetTextL(KWorkPhone);

contact->AddFieldL(*field);

 

CleanupStack::Pop();

 

//把建立的新记录添加到数据库中

contactsDb->AddNewContactL(*contact);

contactsDb->SetOwnCardL(*contact);

CleanupStack::PopAndDestroy(2); // contact contactsDb

3、从数据库中读出数据

   CContactDatabase* contactsDb = CContactDatabase::OpenL();

CleanupStack::PushL(contactsDb);

// Get the ID of the own card and open the contact:

TContactItemId ownCardId = contactsDb->OwnCardId();

CContactItem* ownCard = contactsDb->ReadContactL(ownCardId);

CleanupStack::PushL(ownCard);

TInt count = ownCard->CardFields().Count();

contactsDb->CloseContactL(ownCard->Id());

CleanupStack::PopAndDestroy(); // ownCard

3、编辑数据库中的记录

   ownCard = contactsDb->OpenContactL(ownCardId);

CleanupStack::PushL(ownCard);

TInt index =

 ownCard->CardFields().Find(KUidContactFieldGivenName);

ownCard->CardFields()[index].TextStorage()

->SetTextL(KOtherForename);

contactsDb->CommitContactL(*ownCard);//提交所做的修改

CleanupStack::PopAndDestroy(2);

 4、遍历数据库中的所有记录

CContactDatabase* contactsDb = CContactDatabase::OpenL();

CleanupStack::PushL(contactsDb);

TContactIter iter(*contactsDb);//建立遍历对象

TContactItemId cardId;

while( ( cardId = iter.NextL() ) != KNullContactId )

{

CContactItem* card = contactsDb->ReadContactL(cardId);

CleanupStack::PushL(card);

TInt nField =

card->CardFields().Find(KUidContactFieldGivenName);

TInt fField =

card->CardFields().Find(KUidContactFieldFamilyName);

if( nField!=KErrNotFound && fField!=KErrNotFound )

   {   //读出数据

TPtrC name =

card->CardFields()[nField].TextStorage()->Text();

TPtrC fName =

card->CardFields()[fField].TextStorage()->Text();

iEikonEnv->InfoWinL(name, fName);

}

contactsDb->CloseContactL(card->Id());

CleanupStack::PopAndDestroy(); // card

}

CleanupStack::PopAndDestroy(); // contactsDb

5、过滤数据库中的记录

CContactDatabase* contactsDb = CContactDatabase::OpenL();

CleanupStack::PushL(contactsDb);

CCntFilter *filter = CCntFilter::NewLC();//建立过滤对象

filter->SetContactFilterTypeALL(EFalse);

filter->SetContactFilterTypeCard(ETrue);

_LIT(KFilterDate,"20040101:");

filter->SetFilterDateTime( TTime(KFilterDate) );

filter->SetIncludeModifiedContacts(ETrue);

contactsDb->FilterDatabaseL(*filter);

TInt count = filter->iIds->Count();

CleanupStack::PopAndDestroy(2); // filter contactsDb

注:在某些symbian手机中,若没有设定DateTime可能会出现错误。

6、数据库查询

    所用的函数有:

CContactDatabase::FindLC()

      CContactDatabase::FindInTextDefLC().

FindInTextDefLC()

FindInTextDefAsyncL()

例1、    FindInTextDefAsyncL()的应用:

                iContactsDb = CContactDatabase::OpenL();

iFieldDef = new (ELeave)CContactItemFieldDef();

iFieldDef->AppendL(KUidContactFieldGivenName);

iFieldDef->AppendL(KUidContactFieldFamilyName);

_LIT(KFindToken, "Bond");

iFinder =

iContactsDb->FindAsyncL( KFindToken, iFieldDef, this);

if(iFinder->IsComplete())

                  {

if(iFinder->Error() == KErrNone)

                       {

CContactIdArray* result = iFinder->TakeContactIds();

for(TInt i=0; i<result->Count(); i++)

                            {

TInt cardId = (*result)[i];

// do something with the card

                             }

delete result;

                        }

delete iFinder;

delete iFieldDef;

delete iContactsDb;

}

你可能感兴趣的:(数据库,filter,OS,database,Symbian,通讯)