51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2848|回复: 0
打印 上一主题 下一主题

[讨论] 测试时用到的Wince自带EDB数据库代码

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2010-2-4 10:20:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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 which include the "starttime" and "endtime" to the 'clog.db'
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:elRecord()
{
    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;
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-8 18:29 , Processed in 0.077437 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表