BREW數據庫應用簡介

 

BREW數據庫是一種簡單的關係型數據庫,無多任務,無多用戶,多記錄。它提供給開發者三個接口:IDBMgr,IDatabase,IDBRecord。
IDBMgr接口用於創建、打開、和刪除數據庫。IDatabase接口用戶創建和訪問數據庫中的記錄。IDBRecord接口用戶訪問、更新數據庫記錄中的域。
下面是一些常用的數據庫操作:
1、創建新數據庫代碼:
IDBMgr * pIDBMgr = NULL;
IDatabase * pIDatabase = NULL;
boolean bCreate = TRUE;
ISHELL_CreateInstance(pIShell, AEECLSID_DBMGR, (void **)&pIDBMgr);
if (pIDBMgr == NULL)
return;
if ((pIDatabase = IDBMGR_OpenDatabase (pIDBMgr, pszFile, bCreate)) == NULL)
{
// Opened an already existing database.
}
else
{
// Create an database.
}
2、打開數據庫代碼:
IDBMgr * pIDBMgr = NULL;
IDatabase * pIDatabase = NULL;
boolean bCreate = FALSE;
ISHELL_CreateInstance(pIShell, AEECLSID_DBMGR, (void **)&(*pIDBMgr));
if (pIDBMgr == NULL)
return;
if (((*pIDatabase) = IDBMGR_OpenDatabase ((*pIDBMgr), pszFile, bCreate)) == NULL)
{
// Opened an already existing database.
}
else
{
// Opened an database.
}
3、關閉數據庫代碼:
// IDATABASE_Release closes the open database files, and frees
// any memory associated with the database.
if (pIDatabase)
{
IDATABASE_Release (pIDatabase);
}
// Release IDBMgr object. This step needs to be done
// only if no further use of the IDBMgr object is needed.
if (pIDBMgr)
{
IDBMGR_Release (pIDBMgr);
}
4、創建一條記錄代碼:
boolean CreateOneRecord(IDatabase * pIDatabase, AEEDBField * dbField, int nNumfields)
{
IDBRecord * pIDBRecord = NULL;

// IDATABASE_CreateRecord: creates a new database record with the fields
// specified by pDBFields.
if ((pIDBRecord = IDATABASE_CreateRecord (pIDatabase, dbField, nNumfields)) != NULL)
{
// Successfully created a database record.
// Release record
IDBRECORD_Release (pIDBRecord);
return TRUE;
}
else
{
//Create DB Record Failed
}
return FALSE;
}
dbField指向數據庫記錄域,nNumfields是域的個數。可以創建一個這樣的記錄:
代碼:
{
AEEDBField dbField[3];
int nNumfields = 3;
const char firstName [] = "John";
const char lastName [] = "Smith";
const char address [] = "123 First Street, USA";
AEEDBFieldType fieldType;
AEEDBFieldName fieldName;
// Data fill values used to create a database record.
// The parameter dbField is a three item array of type
// AEEDBField. Each array item corresponds to three fields
// of the record.
dbField[0].fName = AEEDBFIELD_FIRSTNAME;
dbField[0].fType = AEEDB_FT_STRING;
dbField[0].pBuffer = (void *)firstName;
dbField[0].wDataLen = STRLEN (firstName);
dbField[1].fName = AEEDBFIELD_LASTNAME;
dbField[1].fType = AEEDB_FT_STRING;
dbField[1].pBuffer = (void *)lastName;
dbField[1].wDataLen = STRLEN (lastName);
dbField[2].fName = AEEDBFIELD_ADDRESS;
dbField[2].fType = AEEDB_FT_STRING;
dbField[2].pBuffer = (void *)address;
dbField[2].wDataLen = STRLEN (address);

return CreateOneRecord(pIDatabase, dbField, 3);
}
5、獲取記錄個數代碼:
uint32 GetRecordCount(IDatabase * pIDatabase)
{
// IDATABASE_GetRecordCount: returns the number of records in the
// database specified. This gives the number of records in the
// database prior to creating any records in the database.
return IDATABASE_GetRecordCount (pIDatabase);
}
6、讀取記錄域代碼:
boolean GetRecordByID(IDatabase * pIDatabase, uint16 u16RecID)
{
IDBRecord * pIDBRec1 = NULL;
AEEDBFieldType fType;
AEEDBFieldName fName;
uint16 fLen;
byte * data = NULL;;
// This will reset the record Index to 0.
IDATABASE_Reset (pIDatabase);
// IDATABASE_GetRecordByID: returns a pointer to the record whose
// record ID is specified.
pIDBRec1 = IDATABASE_GetRecordByID (pIDatabase, u16RecID);
// Get the raw data of the field
for(;;)
{
// Get record 1 first field and display it
fType = IDBRECORD_NextField (pIDBRec1, &fName, &fLen);
data = IDBRECORD_GetField (pIDBRec1, &fName, &fType, &fLen);
if (data != NULL)
{
switch(fName)
{
case AEEDBFIELD_FIRSTNAME;
break;
case AEEDBFIELD_LASTNAME;
break;
case AEEDBFIELD_ADDRESS;
break;
default:
break;
}
}
else
{
break; //break for
}
}
// Now remove record 1.
IDBRECORD_Release(pIDBRec1);
return TRUE;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章