数据库技术作为一种对信息管理的高效方式,逐渐在业界占据了重要的地位,可以说目前任何类型的IT产品,都或多或少的采用了数据库技术,数据库产品无所不入。
BREW作为无线开发环境也提供了数据库技术。那么下面我就对数据库技术作一下分析:
BREW数据库是一种简单的关系型数据库,无多任务,无多用户,多记录。它提供给开发者三个接口:IDBMgr,IDatabase,IDBRecord。
IDBMgr接口用于创建、打开、和删除数据库。IDatabase接口用户创建和访问数据库中的记录。IDBRecord接口用户访问、更新数据库记录中的域。
下面是一些常用的数据库操作:
1、创建新数据库
代码:
IDBMgr*pIDBMgr=NULL;
IDatabase*pIDatabase=NULL;
booleanbCreate=TRUE;
ISHELL_CreateInstance(pIShell,AEECLSID_DBMGR,(void**)&pIDBMgr);
if(pIDBMgr==NULL)
return;
if((pIDatabase=IDBMGR_OpenDatabase(pIDBMgr,pszFile,bCreate))==NULL)
{
//Openedanalreadyexistingdatabase.
}
else
{
//Createandatabase.
}
2、打开数据库
代码:
IDBMgr*pIDBMgr=NULL;
IDatabase*pIDatabase=NULL;
booleanbCreate=FALSE;
ISHELL_CreateInstance(pIShell,AEECLSID_DBMGR,(void**)&(*pIDBMgr));
if(pIDBMgr==NULL)
return;
if(((*pIDatabase)=IDBMGR_OpenDatabase((*pIDBMgr),pszFile,bCreate))==NULL)
{
//Openedanalreadyexistingdatabase.
}
else
{
//Openedandatabase.
}
3、关闭数据库
代码:
//IDATABASE_Releaseclosestheopendatabasefiles,andfrees
//anymemoryassociatedwiththedatabase.
if(pIDatabase)
{
IDATABASE_Release(pIDatabase);
}
//ReleaseIDBMgrobject.Thisstepneedstobedone
//onlyifnofurtheruseoftheIDBMgrobjectisneeded.
if(pIDBMgr)
{
IDBMGR_Release(pIDBMgr);
}
4、创建一条记录
代码:
booleanCreateOneRecord(IDatabase*pIDatabase,AEEDBField*dbField,intnNumfields)
{
IDBRecord*pIDBRecord=NULL;
//IDATABASE_CreateRecord:createsanewdatabaserecordwiththefields
//specifiedbypDBFields.
if((pIDBRecord=IDATABASE_CreateRecord(pIDatabase,dbField,nNumfields))!=NULL)
{
//Successfullycreatedadatabaserecord.
//Releaserecord
IDBRECORD_Release(pIDBRecord);
returnTRUE;
}
else
{
//CreateDBRecordFailed
}
returnFALSE;
}
dbField指向数据库记录域,nNumfields是域的个数。可以创建一个这样的记录:
代码:
{
AEEDBFielddbField[3];
intnNumfields=3;
constcharfirstName[]="John";
constcharlastName[]="Smith";
constcharaddress[]="123FirstStreet,USA";
AEEDBFieldTypefieldType;
AEEDBFieldNamefieldName;
//Datafillvaluesusedtocreateadatabaserecord.
//TheparameterdbFieldisathreeitemarrayoftype
//AEEDBField.Eacharrayitemcorrespondstothreefields
//oftherecord.
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);
returnCreateOneRecord(pIDatabase,dbField,3);
}
5、获取记录个数
代码:
uint32GetRecordCount(IDatabase*pIDatabase)
{
//IDATABASE_GetRecordCount:returnsthenumberofrecordsinthe
//databasespecified.Thisgivesthenumberofrecordsinthe
//databasepriortocreatinganyrecordsinthedatabase.
returnIDATABASE_GetRecordCount(pIDatabase);
}
6、读取记录域
代码:
booleanGetRecordByID(IDatabase*pIDatabase,uint16u16RecID)
{
IDBRecord*pIDBRec1=NULL;
AEEDBFieldTypefType;
AEEDBFieldNamefName;
uint16fLen;
byte*data=NULL;;
//ThiswillresettherecordIndexto0.
IDATABASE_Reset(pIDatabase);
//IDATABASE_GetRecordByID:returnsapointertotherecordwhose
//recordIDisspecified.
pIDBRec1=IDATABASE_GetRecordByID(pIDatabase,u16RecID);
//Gettherawdataofthefield
for(;;)
{
//Getrecord1firstfieldanddisplayit
fType=IDBRECORD_NextField(pIDBRec1,&fName,&fLen);
data=IDBRECORD_GetField(pIDBRec1,&fName,&fType,&fLen);
if(data!=NULL)
{
switch(fName)
{
caseAEEDBFIELD_FIRSTNAME;
break;
caseAEEDBFIELD_LASTNAME;
break;
caseAEEDBFIELD_ADDRESS;
break;
default:
break;
}
}
else
{
break;//breakfor
}
}
//Nowremoverecord1.
IDBRECORD_Release(pIDBRec1);
returnTRUE;
}