My problem with writing to database is now solved.
However another problem came up -- now I can't read from the DB.
I am getting  "Bus error". The code I am using to read is:

static void drawProc(void *tableP, Int16 row, Int16 column, RectangleType *bounds)
{      
        Int16 y = bounds->topLeft.y + (bounds->extent.y - FntCharHeight()) / 2;
        RectangleType r;
        MemHandle h;
        Airline *air1;
        
        h = DmQueryRecord(gDB, (UInt16) row);
        if (h) 
        {
            air1 = (Airline *) MemHandleLock(h);
            WinDrawTruncChars(air1->airlineName, StrLen(air1->airlineName), 
bounds->topLeft.x + 2, y, bounds->extent.x);
            r = *bounds;
            RctInsetRectangle(&r, 1);
            WinDrawRectangleFrame(rectangleFrame, &r);
            MemHandleUnlock(h);
        }
}

Thanks for your help.



Thomas,

Tuesday, August 5, 2003, 7:23:52 AM, you wrote:

TD> Hello Boris,

TD> this is more fundamental:

TD> 1) It seems, you dont understand the meaning of sizeof(). This function is
TD> processed at compile-time and just inserts the length of a specific
TD> data-type. That means:
TD> sizeof(UInt8) == 1
TD> sizeof(UInt16) == 2
TD> sizeof(Char) == 1
TD> sizeof(Char*) == 4 (its a pointer and all pointers are 32 bit)

TD> what about your struct? : struct Airline
TD> sizeof(Airline) == 6 since its a UInt16 and a Char*

TD> the sizeof-function only works for datatypes not for memory-chunks.

TD> 2) when you assign a new chunk of memory to airlineName, it resides
TD> somewhere in memory but not at the address of air.airlineName.

TD> Thats why you have to create the new record like this:
TD> ulLen=sizeof(air.airlineID)+StrLen(air.airlineName)+1;
TD> h = DmNewRecord(gDB, &index, ulLen);

TD> and write it to the record like this

TD> // The ID; here sizeof() works
TD> err = DmWrite(p, 0, &(air.airlineID), sizeof(air.airlineID));
TD> // The Name; here it does not.
TD> err = DmWrite(p, sizeof(air.airlineID), air.airlineName,
TD> StrLen(air.airlineName)+1);

TD> Hope this helps,

TD> Thomas


TD> "Boris Epshteyn" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
TD> news:[EMAIL PROTECTED]
>>
>> Unfortunately this does not work. :(
>> I've tried to write the record with one DmWrite, without any success:
>> err = DmWrite(p, 0, &air, sizeof(air));
>>
>> Boris
>>
>>
>>
>> TD> Hello Boris,
>>
>> TD> I think your problem is there:
>>
>> >>err = DmWrite(p, sizeof(air.airlineID), &(air.airlineName),
>> TD> sizeof(air.airlineName));
>>
>> TD> it should be like this:
>> TD> err = DmWrite(p, sizeof(air.airlineID), air.airlineName,
>> TD> StrLen(air.airlineName));
>>
>> TD> 1) air.airlineName is already a pointer since you aquired it by
TD> MemPtrNew
>> TD> 2) sizeof(air.airlineName) is wrong since this is only a sizeof(Char*)
TD> which
>> TD> is 4 byte, not the length of your string.
>>
>> TD> Thomas
>>
>> TD> "Boris Epshteyn" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
>> TD> news:[EMAIL PROTECTED]
>> >>
>> >> Hi,
>> >>
>> >> I am trying to create a DB from the static data. In my AppStart, if DB
>> >> does not exists, I create it with four records. The records is defined
>> >> as following:
>> >> typedef struct Airline
>> >> {
>> >>         UInt16 airlineID;
>> >>         char  *airlineName;
>> >> } Airline;
>> >>
>> >> Since my airlineName is variable length, I am allocating memory for
>> >> it, then using StrCopy populating it. Using DmWrite I am writing itto
>> >> the database. But when my temporary object gets released, data from the
>> >> DB also disappears. I would appreciate any advice. Or, may you can
>> >> suggest an application/utility to create DB for initial load from
>> >> static data. I have tried pdbc, but when POSE gives an error when
>> >> trying to load db into POSE. The code for my appStart is:
>> >>
>> >> static char * airlines[AIRLINE_COUNT] = {"Delta", "AA Airline",
>> TD> "Lufthansa", "United"};
>> >>
>> >> static Err AppStart(void)
>> >> {
>> >>    MemHandle han;
>> >>    Err     err = 0;
>> >>
>> >>    gDB = DmOpenDatabaseByTypeCreator('DATA', kCreator,
TD> dmModeReadWrite);
>> >>    if (!gDB) {
>> >>       err = DmGetLastErr();
>> >>       if (err == dmErrCantFind) {
>> >>          err = DmCreateDatabase(0, "AirlineTable-ABCD", kCreator,
TD> 'DATA',
>> TD> false);
>> >>          FrmCustomAlert(alrtNODB, "Created?", NULL, NULL);
>> >>          if (err == 0) {
>> >>             FrmCustomAlert(alrtNODB, NULL, NULL, NULL);
>> >>             gDB = DmOpenDatabaseByTypeCreator('DATA', kCreator,
>> TD> dmModeReadWrite);
>> >>             if (gDB) {
>> >>                UInt16 i;
>> >>                // create initial records
>> >>                for (i = 0;i < AIRLINE_COUNT && err == 0; i++) {
>> >>                    Airline air;
>> >>                    UInt16 index = dmMaxRecordIndex;
>> >>                    MemHandle h;
>> >>                    air.airlineID = i + 110;
>> >>                    air.airlineName = MemPtrNew(StrLen(airlines[i]) +
TD> 1);
>> >>                    if (air.airlineName != NULL)
>> >>                    {
>> >>                       StrCopy(air.airlineName, airlines[i]);
>> >>                    }
>> >>                    h = DmNewRecord(gDB, &index, sizeof(air));
>> >>                    if (!h)
>> >>                    err = DmGetLastErr();
>> >>                    else {
>> >>                         MemPtr p = MemHandleLock(h);
>> >>                         MemPtr tr;
>> >>                         err = DmWrite(p, 0, &(air.airlineID),
>> TD> sizeof(air.airlineID));
>> >>                         err = DmWrite(p, sizeof(air.airlineID),
>> TD> &(air.airlineName), sizeof(air.airlineName));
>> >>                         MemHandleUnlock(h);
>> >>                         DmReleaseRecord(gDB, index, true);
>> >>                    }
>> >>                    MemPtrFree(air.airlineName);
>> >>                }
>> >>             }
>> >>          }
>> >>       }
>> >>    }
>> >>    FrmCustomAlert(alrtNODB, "Exiting Init", NULL, NULL);
>> >>    return err;
>> >> }
>> >>
>> >> --
>> >> Best regards,
>> >>  Boris                          mailto:[EMAIL PROTECTED]
>> >>
>> >>
>> >>
>>
>>
>>
>>
>>                            mailto:[EMAIL PROTECTED]
>>
>>
>>




                           mailto:[EMAIL PROTECTED]


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

Reply via email to