转WinCE数据库之CCeDBDatabase
Posted 91program
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转WinCE数据库之CCeDBDatabase相关的知识,希望对你有一定的参考价值。
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();
以上是关于转WinCE数据库之CCeDBDatabase的主要内容,如果未能解决你的问题,请参考以下文章