>From: Max Bian <[EMAIL PROTECTED]>
>
>This is an interesting programming error.  I'd bet the professors would 
>love to
>see students making this kind of mistake in CSI 102. J/K :)
>
>After your first call to OpenDB, global variable dbPtr contains the 
>reference
>to the first opened database.
>
>When your code calls it the second time, look carefully into the OpenDB
>function, it basically does nothing since dbPtr is not NULL.  The old 
>reference
>is returned.
>
>To solve this, you can:
>1. set the golbal dbPtr to NULL after you finish with the first database
>2. use a different approach: let OpenDB does only what it supposed to do:
>openning a database.  Don't check the dbPtr.  It is global! Make your 
>calling
>code check the global and take actions accordingly.
>
>Max


Hi Max,

I have set the pointer of the database to NULL.
I went to read the all the records from the database A, set the pointer to 
NULL and went to read all the records from database B and set NULL to the 
same pointer as well. It works but

When I decide to open the database A again to add all the records to the 
database A, I received a NULL value for the databasepointer. Why the pointer 
could not point back to the database A again?

Thanks alot...I really need your help as I am been stuck here for a whole 
day.


static DmOpenRef OpenDB(Char *DatabaseName)
{


        if(dbPtr == NULL)
        {
                //find database
                  LocalID dbID = DmFindDatabase( DB_CARDNO , DatabaseName  );

                if(!dbID)
                {  //can't find , create a new database

                  Err err = DmCreateDatabase( DB_CARDNO , DatabaseName , DB_CREATOR 
, DB_TYPE , false );
                   if(err)
                        PrintString("got err creating database");


                          // able to find
                          dbID = DmFindDatabase( DB_CARDNO, DatabaseName );
                          if(! dbID)
                             return NULL;

                }

                //open the database
                dbPtr = DmOpenDatabase( DB_CARDNO , dbID , dmModeReadWrite );
                if (dbPtr == NULL)
                    return NULL;
                }


return dbPtr;
}






<<This is my AddRecord()>>
Err AddRecord(Char *DatabaseName,Char *Rec)
{
    MemHandle h;
        void *ptr;
        Err err;

        dbPtr = OpenDB(DatabaseName);

        if (dbPtr == NULL)
                        PrintString("dbPtr in the AddRecord return by OpenDB is null");
        else
        {
                Int16 Index = dmMaxRecordIndex; //Get the max record

                UInt32 size = StrLen(Rec);

                h = DmNewRecord(dbPtr,&Index,size + 1);

                if(!h)
                        return 1;
                else
                {
                   ptr = MemHandleLock(h);
                   DmWrite(ptr, 0 , (Char *) Rec , size + 1 );
                   MemHandleUnlock(h);
                   DmReleaseRecord(dbPtr,Index,true);
                }
          }


return 0;
}



From
Low Pui Kuen







_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to