Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 19:32, 24 June 2008.

Symbian DBMS中的检索

From Forum Nokia Wiki

DBMS方面的应用中检索是必不可少的操作,通常我们习惯使用SQL来进行检索,就像下面这样:

//SQL keywords definitions
 
_LIT(KSelect, "SELECT ");
 
_LIT(KDelete, "DELETE " );
 
_LIT(KFrom, " FROM ");
 
_LIT(KWhere, " WHERE ");
 
_LIT(KEqual, " = ");
 
_LIT(KNotEqual, " <> ");
 
_LIT(KOrderBy, " ORDER BY ");
 
_LIT(KDot, ", ");
 
_LIT(KAnd, " AND " );
 
_LIT(KLike, " LIKE ");
 
_LIT(KQuotation, "'");
 
_LIT( KAsterisk, "*" );
 
 
 
// Implementation specific constants
 
const int KCustomSqlMaxLength = 256;
 
/**
 
* Function:    GetColumnDataByKeyL
 
*
 
* Description: Get a value from appointed table and column by key.
 
*
 
* Returns:     KErrNone or one of the system wide error codes.
 
*/
 
TInt COurDb::GetColumnDataByKeyL( const TDesC &aTable, const TDesC &aCol, const TDesC &aSortCol, 
 
                                 const TDesC &aKeyCol, const TInt aKey, TDes &aVal )
 
{
 
     TBuf<KCustomSqlMaxLength> sqlStr;
 
     sqlStr.Append( KSelect );
 
     sqlStr.Append( aCol );
 
     sqlStr.Append( KFrom );
 
     sqlStr.Append( aTable );
 
     sqlStr.Append( KWhere );
 
     sqlStr.Append( aKeyCol );
 
     sqlStr.Append( KEqual );
 
     sqlStr.AppendNum( aKey );
 
     sqlStr.Append( KOrderBy );
 
     sqlStr.Append( aSortCol );
 
 
 
     // Create a view on the database
 
     RDbView view;
 
     TInt ret( 0 );
 
     ret = view.Prepare( iDb, TDbQuery( sqlStr ), view.EReadOnly );
 
     if( ret != KErrNone )
 
     {
 
         view.Close();
 
         return ret;
 
     }
 
     User::LeaveIfError( view.EvaluateAll() );
 
 
 
     CDbColSet* colSet = view.ColSetL();
 
     CleanupStack::PushL(colSet);
 
 
 
     if( view.FirstL() )
 
     {
 
         ret = KErrNone;
 
         view.GetL();
 
         aVal.Copy( view.ColDes( colSet->ColNo( aCol ) ) );
 
     }
 
     else
 
     {
 
         ret = KErrNotFound;
 
     }
 
 
 
     CleanupStack::PopAndDestroy(colSet);
 
     view.Close();
 
 
 
     return ret;
 
}

上面的方法对程序员来说是最得心应手的,但由于需要组织一长串的SQL语句所以略显冗长,其实Symbian DBMS API提供了检索的功能,可以通过RDbTable:: SeekL()进行单一和复合的查询功能,应用RDbTable:: SeekL()上面的代码就可以变成:

/**
 
* Function:    GetColumnDataByKeyL
 
*
 
* Description: Get a value from appointed table and column by key.
 
*
 
* Returns:     KErrNone or one of the system wide error codes.
 
*/
 
TInt COurDb::GetColumnDataByKeyL( const TDesC &aTable, const TDesC &aCol, const TDesC &aIndex,
 
                                  const TInt aKey, TDes &aVal )
 
{
 
     // Create an read-only database table object
 
     RDbTable table;
 
     TInt ret = table.Open( iDb, aTable, table.EReadOnly );
 
 
 
     if( ret != KErrNone )
 
     {
 
         return ret;
 
     }
 
 
 
     CDbColSet* colSet = table.ColSetL();
 
     CleanupStack::PushL(colSet);
 
 
 
     table.SetIndex( aIndex );   //
 
     if( table.SeekL( TDbSeekKey( aKey ) ) )
 
     {
 
         table.GetL();
 
         aVal .Copy( table.ColDes( colSet->ColNo(  aCol ) ) );
 
         ret = KErrNone;
 
     }
 
     else
 
     {
 
         ret = KErrNotFound;
 
     }
 
 
 
     CleanupStack::PopAndDestroy( colSet );
 
     table.Close();
 
 
 
     return ret;
 
}

使用RDbTable:: SeekL()我们需要为数据表设置当前使用的索引,然后通过构造一个TDbSeekKey查询对象传给RDbTable:: SeekL()进行实际的检索,这么简单就完成了检索,去掉了繁杂字符串组合操作.下面接着介绍一下通过RDbTable:: SeekL()实现复合查询的方法,可能有人看过TDbSeekKey的定义后会被TDbSeekKey::Add()这个方法迷惑,认为通过它为查询对象TDbSeekKey添加键值就行了,可不幸的是我们会在运行时得到一个panic.正确的方法是使用Symbian DBMS提供的另一个查询类TDbSeekMultiKey<TInt S>,它是一个瘦模板类,构造时传入复合查询时key的个数,然后通过Add方法把每个key添加进行就可以了:


TDbSeekMultiKey<2> key;
 
     key.Add( aKey1 );
 
     key.Add( aKey2 );
 
     table.SetIndex( aIndex );   //
 
     if( table.SeekL( key ) )
 
     {
 
         table.GetL();
 
         aVal .Copy( table.ColDes( colSet->ColNo(  aCol ) ) );
 
         ret = KErrNone;
 
     }
 
     else
 
     {
 
         ret = KErrNotFound;
 
     }

有一点需要注意一下:就是在使用RDbTable:: SeekL()进行检索时,当前使用的索引必需包含每个key所在的列.

Related Discussions
Thread Thread Starter Forum Replies Last Post
3rd上的资源问题 xu_caveman Symbian 22 2008-07-01 03:50
Cryptography (cryptalg) API on Series 60? cbrueckner Symbian Networking & Messaging 20 2006-10-31 12:05
Carrier Information SymbianSandy General Symbian C++ 5 2005-04-27 12:41
symbian os c++ yuvajeeva General Symbian C++ 1 2004-01-08 09:10
Is series 60 SDK supporting Symbian 7.0 available ? maarora Symbian Tools & SDKs 9 2003-10-06 11:08
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX