首先,我是C ++的新手,我正在努力学习它来更新我公司的一个遗留应用程序。这是在windows xp和visual c ++ 6.0上哪个不好玩..任何方法我都有这个select语句读取我的数据库并搜索ResourceID,就像我设置我的m_operSet一样。我希望能够在此类声明中使用4个特定变量。问题是,当我将它设置为特定的时,除了我设置它之外,它将找不到任何其他资源。这是我的代码,这是我的operations.Set.cpp和我的bordDlg.cpp。
这是我希望用于m_operSet
的变量列表' COB'
' NIC'
' SIC'
' PRS'
基本上我想使用if else语句..说如果COB使用COB ifelse NIC使用NIC ifelse SIC使用SIC IFelse PRS使用PRS
// BordDlg.cpp : implementation file
#include "operationSet.h"
#include "custSet.h"
// Initial filter is set in m_custSet Constuctor
m_custSet.setBaseID(baseID);
m_custSet.setLotID(lotID);
// Initial filter is set in m_operSet Constuctor
m_operSet.setBaseID(baseID);
m_operSet.setLotID(lotID);
m_operSet.setSubID(subID);
m_operSet.setSplitID(splitID);
// This is where I set this at and where I want to use either 'COB' 'NIC' 'SIC' 'PRS'
m_operSet.setResourceID("COB");
// operationSet.cpp : implementation file
//
#include "stdafx.h"
#include "operationSet.h"
void operationSet::setBaseID(CString baseID)
{
m_strFilter += "and WORKORDER_BASE_ID LIKE '" + baseID + " ";
}
void operationSet::setLotID(CString lotID)
{
m_strFilter += "and WORKORDER_LOT_ID LIKE '" + lotID + "' ";
}
void operationSet::setSplitID(CString splitID)
{
m_strFilter += "and [WORKORDER_SPLIT_ID] LIKE '" + splitID + "' ";
}
void operationSet::setSubID(CString subID)
{
m_strFilter += "and [WORKORDER_SUB_ID] LIKE '" + subID + "' ";
}
void operationSet::setResourceID(CString resID)
{
m_strFilter += "and [RESOURCE_ID] LIKE '" + resID + "' ";
}
答案 0 :(得分:1)
这些函数构建一个像这样的查询字符串:
SELECT ..whatever.. FROM ..whatever.. WHERE ... \
... and [WORKORDER_SUB_ID] LIKE 'wid' and [RESOURCE_ID] LIKE 'rid' and ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
^ build by setSubID ^ build by setResourceID ^ by other
你需要建立这个:
... and [WORKORDER_SUB_ID] LIKE 'wid' \
and ( [RESOURCE_ID] LIKE 'rid1' or [RESOURCE_ID] LIKE 'rid2' ) and ...
快速修复(不推荐)是:
m_operSet.setResourceID("COB' or [RESOURCE_ID] LIKE 'NIC' or [RESOURCE_ID] "
"LIKE 'SIC' or [RESOURCE_ID] LIKE 'PRS");
和
m_strFilter += "and ( [RESOURCE_ID] LIKE '" + resID + "' ) ";
这样你最终会得到一些怪物,其中添加了撇号,但在调用函数和被调用函数上都没有一致性。
更好的方法:
void operationSet::setResourceID(std::vector<std::string> const &resIDs) {
m_strFilter += "and (";
for (auto it = resIDs.begin(); it != resIDs.end(); ++it) {
if (it != resIDs.begin())
m_strFilter += " or ";
m_strFilter += " [RESOURCE_ID] LIKE '" + *it + "' ";
}
m_strFilter += " ) ";
}
m_operSet.setResourceID({"COB", "NIC", "SIC", "PRS"});
您需要检查应用程序框架的内容:
CString
和std::string
。他们很可能没有为他们写operator +
。因此,您有2个选项:如果.c_str()
和operator +
有CString
,请使用c strings
,或者只使用std::vector<CString>
代码以c++11
编写。查看您的编译器是否支持c++11
。如果不是,则需要将auto
替换为std::vector<std::string>::const_iterator
和呼叫:
std::string res_id_arr[4] = {"COB", "NIC", "SIC", "PRS"};
std::vector<std::string> res_ids(res_id_arr, res_id_arr + 4);
m_operSet.setResourceID(res_ids);
CString
)。所以你真的需要使用c++11
进行编译并使用移动语义。移动到c++11