Forums | developer.brewmp.com Forums | developer.brewmp.com

Developer

Forums

Forums:

Hi friends,

I am facing a problem in Saving and retrieving integer arrays in the database.

I have a 4 x 4 integer array and another integer number(Say nMoves) which i want to save in the database and later i have to retrieve it.

I am using the following code... Just have a look and give ur valuable comments where am i doing the mistake.

static boolean ArrowGame_SaveGame(ArrowGame* pMe)
{
IDBMgr *pIDBMgr = NULL;
IDatabase *pIDatabase = NULL;
IDBRecord *pIDBRecord = NULL;
AEEDBField dbField[17];
AECHAR wszBuff[12]= {'G','a','m','e',' ','S','a','v','e','d','\0'};
int nFontHeight;
char szNumber[5]="";
int i,j;
int nIndex;

if( SUCCESS != ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_DBMGR, (void**)&pIDBMgr))
return FALSE;

//If already game.db exists, delete that and create a new one.
pIDatabase = IDBMGR_OpenDatabase(pIDBMgr, "game.db", FALSE);
if(pIDatabase != NULL)
{
IDBMGR_Remove(pIDBMgr,"game.db");
}
pIDatabase = IDBMGR_OpenDatabase(pIDBMgr, "game.db", TRUE);
if(pIDatabase == NULL)
{
RELEASEIF(pIDBMgr);
return FALSE;
}

for(i=0,nIndex=0;i<4;i++)
{
for(j=0;j<4;j++,nIndex++)
{
dbField[nIndex].fName = AEEDBFIELD_TEXT;
dbField[nIndex].fType = AEEDB_FT_DWORD;
dbField[nIndex].pBuffer = (int*)pMe->anMatrix[i][j];
dbField[nIndex].wDataLen = sizeof(pMe->anMatrix[i][j]);
DBGPRINTF("%d **** %d",(int*)(dbField[nIndex].pBuffer), pMe->anMatrix[i][j]);
}
}

dbField[nIndex].fName = AEEDBFIELD_TEXT;
dbField[nIndex].fType = AEEDB_FT_DWORD;
dbField[nIndex].pBuffer = (int*)pMe->nMoves;
dbField[nIndex].wDataLen = sizeof(pMe->nMoves);
DBGPRINTF("%d **** %d",(int*)(dbField[nIndex].pBuffer), pMe->nMoves);

if ((pIDBRecord = IDATABASE_CreateRecord(pIDatabase,dbField,17))== NULL)
{
RELEASEIF(pIDatabase);
RELEASEIF(pIDBMgr);
return FALSE;
}
RELEASEIF(pIDBRecord);
RELEASEIF(pIDatabase);
RELEASEIF(pIDBMgr);
return TRUE;

static boolean ArrowGame_LoadGame(ArrowGame* pMe)
{
IDBMgr *pIDBMgr = NULL;
IDatabase *pIDatabase = NULL;
IDBRecord *pIDBRecord = NULL;

char * data = NULL;
AEEDBFieldType fieldType; // stores field type
AEEDBFieldName fieldName; // stores field name
uint16 fieldLen;
dword * pdwRet = NULL;

int nFontHeight;
int i,j;
int nIndex;
DBGPRINTF("******* ArrowGame_LoadGame");
if( SUCCESS != ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_DBMGR, (void**)&pIDBMgr))
return FALSE;
DBGPRINTF("***** Opening the database");
//If already game.db exists, delete that and create a new one.
pIDatabase = IDBMGR_OpenDatabase(pIDBMgr, "game.db", FALSE);
if(pIDatabase == NULL)
{
RELEASEIF(pIDBMgr);
return TRUE;
}
DBGPRINTF("***** Getching records");
for(i=0,nIndex=0;i<4;i++)
{
for(j=0;j<4;j++,nIndex++)
{
pIDBRecord = IDATABASE_GetNextRecord (pIDatabase);
if (pIDBRecord == NULL)
break;
fieldType = IDBRECORD_NextField (pIDBRecord, &fieldName, &fieldLen);
IDBRECORD_GetFieldDWord (pIDBRecord, pdwRet);
pMe->anMatrix[i][j] = *pdwRet;
DBGPRINTF("%d **** %d",*pdwRet, pMe->anMatrix[i][j]);
}
}

pIDBRecord = IDATABASE_GetNextRecord (pIDatabase);
if (pIDBRecord == NULL)
{
RELEASEIF(pIDatabase);
RELEASEIF(pIDBMgr);
return FALSE;
}

fieldType = IDBRECORD_NextField (pIDBRecord, &fieldName, &fieldLen);
data = (char*)IDBRECORD_GetField (pIDBRecord, &fieldName, &fieldType, &fieldLen);
pMe->nMoves = (ATOI)(data);
DBGPRINTF("%d **** %d",(ATOI)(data), pMe->nMoves);

RELEASEIF(pIDBRecord);
RELEASEIF(pIDatabase);
IDBMGR_Remove (pIDBMgr,"game.db"); //Remove the database files.
RELEASEIF(pIDBMgr);

return TRUE;

Release the IDatabase pointer, before removing it, otherwise DB will not be removed and it will create new records each time.
In ur second method..
remove GetNextRecord from inside the loop and place it before it, otherwise this will change the record after 4 feild.
pass a valid pdwRet pointer in
IDBRECORD_GetFieldDWord (pIDBRecord, pdwRet), and Not NULL, it writes back the value.

Release the IDatabase pointer, before removing it, otherwise DB will not be removed and it will create new records each time.
In ur second method..
remove GetNextRecord from inside the loop and place it before it, otherwise this will change the record after 4 feild.
pass a valid pdwRet pointer in
IDBRECORD_GetFieldDWord (pIDBRecord, pdwRet), and Not NULL, it writes back the value.