×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Sep 2008
    Posts
    290

    Database include problem...

    Hi,

    my database header file looks something like this...

    Code:
    #ifndef DATABASE_H
    #define DATABASE_H
    
    
    // INCLUDES
    #include <e32std.h>
    #include <e32base.h>
    #include <d32dbms.h>
    #include <bautils.h>
    //include table classes
    #include "TableUser.h"
    #include "TableOption.h"
    #include "TableTask.h"
    #include "TableTaskVM.h"
    #include "TableVoiceMail.h"
    ...
    
    CTableUser* iTblUser;
    CTableOption* iTblOption;
    CTableVoiceMail*  iTblVoiceMail; 
    CTableTask*  iTblTask;
    CTableTaskVM* iTblTaskVM;
    ...
    RDbNamedDatabase iDatabase;
    	
    //File server session...
    RFs iFSession;
    //Represents a session with the DBMS Server...
    RDbs iDBSession;

    my ccp file

    Code:
    ...TBool CDatabase::ConnectToDatabase( void )
    	{
    	if ( !DBServerSessionConnected )
    		{
    		return EFalse;
    		}
    	
    	TInt Err = iFSession.Connect();
    	
    	if (Err != KErrNone )
    		{
    		iState = EError;
    		return EFalse;
    		}
    	
    	iState = EServerReady;
    	
    	if ( !BaflUtils::FileExists( iFSession, *iDatabasename ) )
    		{
    		//no database file so create a new one
    		Err = iDatabase.Create( iFSession, *iDatabasename );
    		if ( Err == KErrNone )
    			{
    			iState = ECreate;
    			iDatabase.Close();
    			}
    		}
    
    	iFSession.Close();
    	
    	Err = iDatabase.Open( iDBSession, *iDatabasename  );
    	if ( Err == KErrNone )
    		{
    		iState = EOpen;
    		return ETrue;
    		}		
    	
    	iState = EError;
    	return EFalse;
    	}
    ...

    i have three view where i need some information from my Database.
    If i put my Database.h into my document class i have no clue how to call the function from my view classes.
    If i include my databasae.h file in my view files then i open my database read information from or write information to my tables.


    Greetz
    Franky

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: Database include problem...

    There are at least 3 possible solutions
    1) You can always pass around a pointer to a single database-accessor object
    2) CCoeEnv::Static()->AppUi() gives you a pointer to your AppUi anywhere. Then you can cast it to your AppUi type, and do whatever you want - including the usage of AppUi's variables, or accessing its Document
    3) Or you can use CCoeStatic and implement a singleton object.

  3. #3
    Super Contributor
    Join Date
    Mar 2004
    Location
    Bangalore,India
    Posts
    2,146

    Re: Database include problem...

    Quote Originally Posted by wizard_hu_ View Post
    There are at least 3 possible solutions
    1) You can always pass around a pointer to a single database-accessor object
    2) CCoeEnv::Static()->AppUi() gives you a pointer to your AppUi anywhere. Then you can cast it to your AppUi type, and do whatever you want - including the usage of AppUi's variables, or accessing its Document
    3) Or you can use CCoeStatic and implement a singleton object.
    Implementing the singleton pattern sounds like the best approach to me personally.

    Code:
    in the .h file
    
    class MyClass
       {
       public:
    
    	static MyClass* GetInstanceL();
        private:
            static 	MyClass* m_Class;
       };
    
    and then in the .cpp
    
    MyClass* MyClass::m_Class = 0;
    
    MyClass* MyClass::GetInstanceL()
        {
        if (m_Class == NULL)
    	{
    	m_Class = new (ELeave) MyClass();
    	}
        return m_Class ;
       }
    The ctor remains private, so that GetInstanceL is the only way to get an instance of your class, in this case singleton.
    Cheers,
    Mayank

  4. #4
    Regular Contributor
    Join Date
    Sep 2008
    Posts
    290

    Re: Database include problem...

    Hi,


    my singelton approach goes like this...


    Code:
    CDatabase* CDatabase::NewL()
    	{
    	
    	static CDatabase* instance;
    	if (instance == NULL)
    		{
    		instance = CDatabase::NewLC();
    		CleanupStack::Pop(); // instance;
    		}
    	
    	return instance;
    	}
    NewL is static and this should return a database instance.

    Quote Originally Posted by wizard_hu_
    There are at least 3 possible solutions
    1) You can always pass around a pointer to a single database-accessor object
    2) CCoeEnv::Static()->AppUi() gives you a pointer to your AppUi anywhere. Then you can cast it to your AppUi type, and do whatever you want - including the usage of AppUi's variables, or accessing its Document
    3) Or you can use CCoeStatic and implement a singleton object.
    i will test this in my app...

    Greetz
    Franky

  5. #5
    Regular Contributor
    Join Date
    Sep 2008
    Posts
    290

    Re: Database include problem...

    Hi,


    i put
    Code:
    if ( ((CGobasAppUi*)CCoeEnv::Static()->AppUi())->DB )
    into my file where this is my header file

    Code:
    ...
    class CSettings : public CAknForm
    ...
    but


    Code:
    if ( ((CMyAppUi*)CCoeEnv::Static()->AppUi())->DB )
    {
    }
    but my views are part of my AppUi Class. (Like any other project..)

    Code:
    ...
    #include "GobasApplication.h"
    #include "OpenTasksView.h"
    #include "ClosedTasksView.h"
    #include "InformationView.h"
    #include "RecorderView.h"
    #include "SettingsView.h"
    #include "DetailOpenTasksView.h"
    #include "DetailClosedTasksView.h"
    ...
    So i dont think it is wise to include my AppUi header in my Settings.h file which contains

    my desired functions...


    Code:
    CSettings::LoadFromDataL()

    Code:
    TBool CSettings::SaveFormDataL()

    Greetz
    Franky

  6. #6
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: Database include problem...

    Include files are invented to be #include-d, so there is nothing wrong with #include-ing the AppUi after all. It works.

    Anyway, the singleton is the most elegant solution indeed. In case of "pure C++" singletons, do not forget deleting them when you are exiting the application. For CCoeStatic (http://wiki.forum.nokia.com/index.ph..._in_Symbian_OS) it happens automatically, though CCoeStatic is usable in GUI apps only.
    And in both cases consider renaming the getter method to GetInstanceL, that better describes a singleton than "NewL".

Similar Threads

  1. Problem in View Architecture .please help
    By Abhishek_karmakar in forum Symbian User Interface
    Replies: 4
    Last Post: 2008-09-24, 14:13
  2. Form Problem....
    By vineetbillorey in forum Symbian
    Replies: 28
    Last Post: 2008-09-24, 05:58
  3. problem in mms
    By Chinu in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2007-12-11, 17:55
  4. Problem with SEikControlInfo
    By Zefa in forum Symbian User Interface
    Replies: 4
    Last Post: 2007-06-13, 07:43
  5. open a view by clicking the listbox item
    By kirtij in forum Symbian Tools & SDKs
    Replies: 5
    Last Post: 2006-02-24, 15:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×