成語收錄和查詢

電子槍發表於2016-07-21

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

相關文章