以前写的一个Wince自带EDB数据库使用范例

2010-03-10  赵祥方 

Wince自带EDB数据库,以下是写的一个例子:clog(通话记录):
 
#include "TAddLog.h"
#define _WIN32_DCOM
TAddLog::TAddLog()
{
    m_ceoidDB    = NULL;
    m_hCallLogDB   = NULL;
    memset(&m_ceguid, 0, sizeof(CEGUID));
}
TAddLog::~TAddLog()
{
    CloseDatabase();
}
//create the database 'clog.db'
bool TAddLog::CreateDatabase()
{
//dust judge is there 'clog.db'
    if (OpenDatabase()) {
        CloseDatabase();
        return true;
    }
    CEDBASEINFOEX     DBInfo;
    CEOID           DBOid=0 ;
    CEGUID m_VolGUID;
    //  Fill in the DBInfo structure
    CREATE_SYSTEMGUID(&m_VolGUID);
    memset(&DBInfo, 0, sizeof(CEDBASEINFOEX)) ;
    DBInfo.wVersion = CEDBASEINFOEX_VERSION;
    DBInfo.dwFlags |= CEDB_VALIDDBFLAGS | CEDB_VALIDNAME | CEDB_VALIDSORTSPEC;
    DBInfo.wNumSortOrder = 1;
    DBInfo.rgSortSpecs[0].rgPropID[0] = 0x00020040;//start time
    DBInfo.rgSortSpecs[0].rgdwFlags[0] = CEDB_SORT_DESCENDING;
    DBInfo.rgSortSpecs[0].wVersion = 2;
    DBInfo.rgSortSpecs[0].wNumProps = 1;
    DBInfo.rgSortSpecs[0].wKeyFlags = 0;
    wcscpy(DBInfo.szDbaseName, TEXT("clog.db"));
    // Volume might exist, create when EXISTING
    if (!CeMountDBVolEx(&m_VolGUID, L"\\pim.vol", NULL, OPEN_EXISTING)) {
      int  ret2 = GetLastError();
      printf("\n .....CeMountDBVol failed !\n");
      return false;
    }
    // Create the database
    if(NULL == (DBOid = CeCreateDatabaseWithProps(&m_VolGUID, &DBInfo, 0, NULL))) {
        int res = GetLastError();
        CeUnmountDBVol(&m_VolGUID);
        return false;
    }
    CeUnmountDBVol(&m_VolGUID);
    return true;
}
//open 'clog.db'
bool TAddLog::OpenDatabase()
{
    //Mount the volume
    BOOL bMountVol = CeMountDBVolEx(&m_ceguid, L"\\pim.vol", NULL, OPEN_EXISTING);
    if (FALSE == bMountVol) {
        printf("\n .....CeMountDBVolEx failed!\n");
        return false;
    }
/*
//----------------------Get all default dbase name by record oid on the volume
     HANDLE hDatabaseEnumHandle = CeFindFirstDatabaseEx(&m_ceguid, 0);
     if(hDatabaseEnumHandle == INVALID_HANDLE_VALUE) {
          return 0;
     }
     CEOID ceDbaseoid = 0;
     printf("\n+++++++++++Database name begin++++++++++++\n");
     while (ceDbaseoid = CeFindNextDatabaseEx(hDatabaseEnumHandle, 0)) {
         CEOIDINFOEX info;
         info.wVersion = 2;
         info.wObjType = OBJTYPE_DATABASE;
         wchar_t buf[256];
         if (CeOidGetInfoEx2(&m_ceguid, ceDbaseoid, &info)) {
             swprintf(buf, L"%s", info.infDatabase.szDbaseName);
             printf("%S", buf);
         }
     }
     printf("\n-----------Database name over------------\n");
     if (hDatabaseEnumHandle != INVALID_HANDLE_VALUE) {
         CloseHandle(hDatabaseEnumHandle);
     }
//------------------print the database over
*/
    SORTORDERSPECEX sort;
  sort.wVersion = 2;
  sort.wNumProps = 1;
  sort.wKeyFlags = 0;
  sort.wReserved = 0;
  sort.rgPropID[0] = 0x00020040; //orderd by start time
  sort.rgdwFlags[0] = CEDB_SORT_DESCENDING;
    m_hCallLogDB = CeOpenDatabaseInSession(NULL, &m_ceguid, &m_ceoidDB, TEXT("clog.db"), &sort, 0, NULL);
   if (INVALID_HANDLE_VALUE == m_hCallLogDB) {
        DWORD res = GetLastError();
        printf("\n.....opendatabase failed! last error is %d\n", res);
       CeUnmountDBVol(&m_ceguid);
        return false;
   }
    return true;
}
//Add record
int TAddLog::AddRecord(LPWSTR name, LPWSTR phonum, LPWSTR numtype, long logtype, \
                            SYSTEMTIME *stStartTime, SYSTEMTIME *stEndTime, int num)
{
    int IsSucs = OpenDatabase();
    if (!IsSucs) {
        return 0;
    }
    CEPROPVAL propsToWrite[6];
    CEOID ceoidwrite;
    FILETIME ftLocalStartTime;
    FILETIME ftLocalEndTime;
    SystemTimeToFileTime(stStartTime, &ftLocalStartTime);//SYSTEMTIME,LPFILETIME
    SystemTimeToFileTime(stEndTime, &ftLocalEndTime);//SYSTEMTIME,LPFILETIME
    for(int i = 0; i < num; i++) {
        propsToWrite[0].propid = 0x0007001f;//name
        propsToWrite[0].val.lpwstr = name;
        propsToWrite[0].wFlags = 0;
        propsToWrite[0].wLenData = 0;
        propsToWrite[1].propid = 0x0006001f;//number
        propsToWrite[1].val.lpwstr = phonum;
        propsToWrite[1].wFlags = 0;
        propsToWrite[1].wLenData = 0;
        propsToWrite[2].propid = 0x000a001f;//number type
        propsToWrite[2].val.lpwstr = numtype;
        propsToWrite[2].wFlags = 0;
        propsToWrite[2].wLenData = 0;
        propsToWrite[3].propid = 0x00020040;//start time
        LocalFileTimeToFileTime(&ftLocalStartTime, &propsToWrite[3].val.filetime);
        propsToWrite[3].wFlags = 0;
        propsToWrite[3].wLenData = 0;
        propsToWrite[4].propid = 0x00030040;//end time
        LocalFileTimeToFileTime(&ftLocalEndTime, &propsToWrite[4].val.filetime);
        propsToWrite[4].wFlags = 0;
        propsToWrite[4].wLenData = 0;
        propsToWrite[5].propid = 0x00040003;//log type
        propsToWrite[5].val.lVal = logtype;
        propsToWrite[5].wFlags = 0;
        propsToWrite[5].wLenData = 0;
       ceoidwrite = CeWriteRecordProps(m_hCallLogDB, 0, 6, propsToWrite);
       if (!ceoidwrite) {
            DWORD res = GetLastError();
           CloseDatabase();
           printf("\n .....add failed! last err is %d\n", res);
           return 0;
        }
    }
    CloseDatabase();
    return ceoidwrite;
}
//delete the record from the 'clog.db'
bool TAddLog::DelRecord()
{
    int IsSucs = OpenDatabase();
    if (!IsSucs) {
        return false;
    }
    CEOIDINFOEX oidinfo;
    oidinfo.wVersion = 2;
    int r = 0;
//    m_ceoidDB = 0;
    if (r = CeOidGetInfoEx2(&m_ceguid, m_ceoidDB, &oidinfo)) {
        if (oidinfo.infDatabase.dwNumRecords) {
            printf("\n.....DelRecords num is %d\n", oidinfo.infDatabase.dwNumRecords);
            CEOID oid;
            DWORD dwIndex;
            oid = CeSeekDatabase(m_hCallLogDB, CEDB_SEEK_BEGINNING, 0, &dwIndex);
            while (oid) {
                if (!CeDeleteRecord(m_hCallLogDB, oid)){;}
                oid = CeSeekDatabase(m_hCallLogDB, CEDB_SEEK_CURRENT, 1, &dwIndex);
            }
        }
    }
    CloseDatabase();
    return true;
}
//close 'clog.db'
bool TAddLog::CloseDatabase()
{
    if (INVALID_HANDLE_VALUE != m_hCallLogDB) {
      CloseHandle(m_hCallLogDB);
      CeUnmountDBVol(&m_ceguid);
      m_hCallLogDB = INVALID_HANDLE_VALUE;
        return true;
    }
    return false;
}
 
636°/6352 人阅读/1 条评论 发表评论

赵祥方  2010-03-31

大家都不写代码吗?怎么没人一起讨论?


登录 后发表评论