ccedbdatabase类提供一系列api函数以操作wince下的数据库文件*.db,不过该数据库文件一般由系统自动创建于database文件夹下面,该文件保存于内存中,仪器重启即丢失。此方式可用于数据量大且短期需要保存数据的系统。其操作具体如下所示: 1、定义数据库对象
ccedbdatabase类提供一系列api函数以操作wince下的数据库文件*.db,不过该数据库文件一般由系统自动创建于database文件夹下面,该文件保存于内存中,仪器重启即丢失。此方式可用于数据量大且短期需要保存数据的系统。其操作具体如下所示:
1、定义数据库对象:
ccedbdatabase db;
2、创建数据库:
// create database
ccedbprop dbprops[3] =
{
ccedbprop(ccedbprop::type_string,prop_no,ccedbprop::sort_ascending),
ccedbprop(ccedbprop::type_string,prop_name,ccedbprop::sort_ascending),
ccedbprop(ccedbprop::type_ushort,prop_age,ccedbprop::sort_ascending)
}; if(!ccedbdatabase::exists(_t(student.db)))
{
db.create(_t(student.db),db_ident_id,3,dbprops);
}
3、新增记录:
updatedata(true);
if(!db.open(_t(student.db)))
{
messagebox(_t(database open error!));
return;
}
ccedbprop props[3];
ccedbrecord rec;
props[0] = ccedbprop((lpwstr)(lpctstr)m_no,prop_no);
props[1] = ccedbprop((lpwstr)(lpctstr)m_name, prop_name);
if(db.seekfirstequal(props[1]))
{
messagebox(_t(tiger));
db.close();
return;
}
props[2] = ccedbprop((ushort)m_age,prop_age);
rec.addprops(props,3);
if(!db.addrecord(&rec))
{
messagebox(_t(add record error!));
return;
}
db.close();
4、编辑记录:
updatedata(true);
if(!db.open(_t(student.db)))
{
messagebox(_t(database open error!));
}
ccedbrecord rec;
ccedbprop pprop[2];
// pprop[0]=ccedbprop((lpwstr)(lpctstr)m_no,prop_no);
pprop[0]=ccedbprop((lpwstr)(lpctstr)m_name,prop_name);
pprop[1]=ccedbprop((ushort)m_age,prop_age);
rec.addprops(pprop,2);
//ok
/* ccedbprop pprop1[1];
pprop1[0]=ccedbprop((lpwstr)(lpctstr)m_no,prop_no);
db.seekfirstequal(pprop1[0]);
db.writecurrrecord(&rec);*/
//修改数据ok
//////////////////////////
/* bool oldseeknext=db.m_bautoseeknext;
db.m_bautoseeknext=true;
db.seekfirst();
ccedbrecord temprec;
ccedbprop* tempprop;
cstring strno,strname;
while(db.readcurrrecord(&temprec))
{
tempprop=temprec.getpropfromident(prop_no);
strno=tempprop->getstring();
tempprop=temprec.getpropfromident(prop_name);
strname=tempprop->getstring();
if((strno == m_no)&&(strname == m_name))
{
if(db.m_beof)
db.seeklast();
else
db.seekprev();
db.writecurrrecord(&rec);
break;
}
}
db.m_bautoseeknext=oldseeknext;*/
////////////////////////
//定位记录
bool oldseeknext=db.m_bautoseeknext;
db.m_bautoseeknext=true;
ccedbprop aimprop[1];
aimprop[0]=ccedbprop((lpwstr)(lpctstr)m_no,prop_no);
if(db.seekfirstequal(aimprop[0]))
{
ccedbrecord temprec;
ccedbprop *tempprop;
cstring strname;
// do
// {
db.readcurrrecord(&temprec);
// tempprop=temprec.getpropfromident(prop_name);
// strname=tempprop->getstring();
// messagebox(strname);
// if(!db.m_beof)
// messagebox(_t(tiger));
if(!db.m_beof)
db.seekprev();
db.deletecurrrecord();
/* if(strname == m_name)
{
db.writecurrrecord(&rec);
break;
}*/
// }while(db.seeknextequal(aimprop[0]));
}
db.m_bautoseeknext=oldseeknext;
/////////////////////////////////////
db.close();
5、删除记录:
if(!db.open(_t(student.db)))
{
messagebox(_t(database open error!));
}
//取得列表中选择的记录
position pos=m_ctrllist.getfirstselecteditemposition();
int nitem=m_ctrllist.getnextselecteditem(pos);
cstring no=m_ctrllist.getitemtext(nitem,0);
ccedbprop pprop[1];
pprop[0]=ccedbprop(no.getbuffer(0),prop_no);
db.seekfirstequal(pprop[0]);
////////////////////
ccedbrecord rec;
db.readcurrrecord(&rec);
ccedbprop* tempprop;
tempprop=rec.getpropfromident(prop_no);
cstring strno=tempprop->getstring();
messagebox(strno);
/////////////////////
db.deletecurrrecord();
db.close();
6、遍历记录
m_ctrllist.deleteallitems();
if(!db.open(_t(student.db)))
{
messagebox(_t(database open error!));
}
ccedbprop* pprop;
int nitem=0;
cstring str;
bool bprev = db.m_bautoseeknext;
db.m_bautoseeknext = true;
db.seekfirst();
ccedbrecord rec;
while(db.readcurrrecord(&rec))
{
pprop=rec.getpropfromident(prop_no);
lpwstr strno=pprop->getstring();
m_ctrllist.insertitem(nitem,strno);
pprop=rec.getpropfromident(prop_name);
lpwstr strname=pprop->getstring();
m_ctrllist.setitemtext(nitem,1,strname);
pprop=rec.getpropfromident(prop_age);
ushort age=pprop->getushort();
str.format(_t(%d),age);
m_ctrllist.setitemtext(nitem,2,str);
}
db.m_bautoseeknext = bprev;
db.close();
