codes:

typedef struct 
{
        Char   dbName[32];
        UInt32 dbCreator;
        UInt32 dbType;
        UInt32 dbSize;
        
}TestSubType;

typedef struct 
{
        Char * path;
        Char * name;
        UInt16 dbNum;//数据库数目
        TestSubType *testSubList;
        
}TestType;

static Err AddNewRecord(DmOpenRef dbP)
{
        Err     err = errNone;
        UInt16 u16Index = DmNumRecords (dbP);
        TestType testType;
        
        //initial the testType
        testType.name = (Char*)MemPtrNew (StrLen ("test a database") + 1);
        testType.path = (Char*)MemPtrNew (StrLen ("test path") + 1);
        testType.testSubList = (TestSubType*)MemPtrNew (sizeof (TestSubType)*2);
        
        if (testType.name == NULL || testType.path == NULL || 
testType.testSubList == NULL)
        {
                if (testType.name)
                {
                        MemPtrFree (testType.name);
                }
                
                if (testType.path)
                {
                        MemPtrFree (testType.path);
                }
                
                if (testType.testSubList)
                {
                        MemPtrFree (testType.testSubList);
                }
                return 1;
        }
        
        StrCopy (testType.name, "test a database");
        StrCopy (testType.path, "test path");
        
        testType.testSubList[0].dbCreator = 'test';
        testType.testSubList[0].dbType = 'appl';
        testType.testSubList[0].dbSize = 144111;
        
        
        testType.testSubList[1].dbCreator = 'test';
        testType.testSubList[1].dbType = 'appl';
        testType.testSubList[1].dbSize = 1444557;
        
        
        MemHandle recordH = DmNewRecord (dbP, &u16Index,  sizeof (testType));
        if (recordH == NULL)
        {
                return DmGetLastErr ();
        }
        
        TestType * testTypePtr = (TestType*)MemHandleLock (recordH);
        

        DmWrite (testTypePtr, 0, &testType, sizeof(testType) );
        
        MemHandleUnlock (recordH);
        DmReleaseRecord (dbP, u16Index, true );
        
        return err;
}

static MemHandle GetRecordApp (DmOpenRef dbP, Char *name)
{
if (!name)
        {
                return NULL;
        }
        
        UInt16 recordNum = DmNumRecords (dbP);
        
        for (UInt16 i = 0; i < recordNum; i++)
        {
                MemHandle recordH = DmQueryRecord (dbP, i);
                TestType *testPtr = (TestType *)MemHandleLock (recordH);
                if (StrCompare (testPtr->name, name) == 0)
                {
                        MemHandleUnlock (recordH);
                        return recordH;
                }
                MemHandleUnlock (recordH);
        }
        
        return NULL;
}


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

Reply via email to