WangRain wrote:
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);
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 );
Your problem is that you are only copying the contents of TestType into
the database. The TestType struct doesn't contain any of the sublist,
path, or name information. It just contains pointers to this information.
These pointers will become invalid when your app exits or when you do
MemPtrFree().
In order to do what you are attempting to do, you will need to define
a way to store the data as a sequence of bytes. Then you will need to
write a function to read from a TestType struct and write all the data
(including strings) as a sequence of bytes. You will also need a
corresponding function to read a sequence of bytes and create a TestType
structure from it.
- Logan
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/