成语收录和查询

来源:互联网 发布:德国与中国 知乎 编辑:程序博客网 时间:2024/04/28 21:43

1.前言

前段时间有一小朋友拿着作业本问我一个四字成语什么花什么妍,想了半天没反应过来,后来百度了下才告诉人家,决定写一个软件实现四字成语的查询,目前收录了两部资源 <<中国成语大辞典>>和<<成语大全>>,查询在毫秒级别

2.思想

2.1加载外部资源如中国成语大辞典和成语大全到sqlite数据库中

2.2通过模糊查询获取数据库中的相关信息并进行展示

3.代码片段

3.1加载外部文件到数据库

    int nsum = 0;    std::vector<std::string> vecname;    DWORD dwBeg = GetTickCount();    std::fstream srcfile;    srcfile.open("成语大全.txt", std::ios_base::in);    if (srcfile.is_open())       {        std::string strline("");        while (srcfile>>strline)        {            int nfirst = strline.find("拼音");            if (nfirst != -1)            {                std::string strvalue = strline.substr(0, nfirst);                if (!strvalue.empty())                {                    strvalue = trim(strvalue);                    std::string strutf8 = MbsToUtf8(strvalue);                    vecname.push_back(strutf8);                    nsum++;                }            }        }    }    srcfile.close();    if (!vecname.empty())        m_sqlite.insertdb(vecname);    TCHAR ptBuf[MAX_PATH] = {0};    swprintf_s(ptBuf, MAX_PATH, TEXT("共收录%d条成语,耗时:%d秒."), nsum, (GetTickCount() - dwBeg) / 1000);    AfxMessageBox(ptBuf);

3.2查询数据库进行展示

    std::string strvalue("");    char szbuf[64] = {0};    GetDlgItemTextA(GetSafeHwnd(), IDC_EDIT1, szbuf, 64);    if (strlen(szbuf) > 0)        strvalue += szbuf;    else        strvalue += "_";    GetDlgItemTextA(GetSafeHwnd(), IDC_EDIT2, szbuf, 64);    if (strlen(szbuf) > 0)        strvalue += szbuf;    else        strvalue += "_";    GetDlgItemTextA(GetSafeHwnd(), IDC_EDIT3, szbuf, 64);    if (strlen(szbuf) > 0)        strvalue += szbuf;    else        strvalue += "_";    GetDlgItemTextA(GetSafeHwnd(), IDC_EDIT4, szbuf, 64);    if (strlen(szbuf) > 0)        strvalue += szbuf;    else        strvalue += "_";    CListBox *plistbox = (CListBox *)GetDlgItem(IDC_LIST1);    plistbox->ResetContent();    std::string strutf8 = MbsToUtf8(strvalue);    std::vector<std::string> vecname;    m_sqlite.selectdb(strutf8, vecname);    if (vecname.empty())        AfxMessageBox(_T("抱歉,没有找到相关程序,请点击进行收录"));    else    {        USES_CONVERSION;        for (int nindex = 0; nindex < vecname.size(); ++nindex)        {            strvalue = Utf8ToMbs(vecname[nindex]);            plistbox->InsertString(nindex, A2T(strvalue.c_str()));        }    }

3.3操作数据库的接口

bool CSqliteIdiom::insertdb(const std::string &strname){    bool bret = false;    if (m_pDB != nullptr)    {        std::string strSql = "";         strSql += "insert into tb_idiom(name)";          strSql += "values('";          strSql += strname;          strSql += "');";           char* cErrMsg;          int nRes = sqlite3_exec(m_pDB , strSql.c_str() ,0 ,0, &cErrMsg);         bret = nRes == SQLITE_OK;    }    return bret;}bool CSqliteIdiom::insertdb(const std::vector<std::string> &vecname){    bool bret = false;    if (m_pDB != nullptr)    {        sqlite3_exec(m_pDB, "BEGIN;", 0, 0, 0);        for (std::vector<std::string>::const_iterator it = vecname.begin(); it != vecname.end(); ++it)        {            std::string strSql = "";             strSql += "insert into tb_idiom(name)";              strSql += "values('";              strSql += *it;              strSql += "');";             sqlite3_exec(m_pDB , strSql.c_str() ,0 ,0, 0);        }        sqlite3_exec(m_pDB, "COMMIT;", 0, 0, 0);        bret = true;    }    return bret;}bool CSqliteIdiom::selectdb(const std::string &strname, std::vector<std::string> &allname){    bool bret = false;    if (m_pDB != nullptr)    {        char **dbResult;         char *errmsg;        int nRow, nColumn;        int index=0;        std::string strSql = "";         strSql += "select name from tb_idiom where name like '%";        strSql += strname;        strSql += "%'";        int rc = sqlite3_get_table( m_pDB, strSql.c_str(), &dbResult, &nRow, &nColumn, &errmsg);        if (rc == SQLITE_OK)        {            index = nColumn;            for(int  i = 0; i < nRow ; i++ )            {                for(int  j = 0 ; j < nColumn; j++ )                    allname.push_back(dbResult[index++]);            }        }        bret = true;    }    return bret;}

4.备注

4.1 vs2010+win7(64位旗舰版)+sqlite3 编译测试通过

4.2 完整demo下载地址 http://download.csdn.net/detail/zhang_ruiqiang/9581721

0 0