【发布时间】:2018-03-20 17:52:51
【问题描述】:
我正在尝试将来自 CComboBox 的 CString 变量转换为 std::string 或 sql::SQLString 用于在 SQL 查询中设置它(使用 Mysql 连接器 C++)。所以,这里是我的代码:
CComboBox *pComboRule = (CComboBox*)GetDlgItem(ID_EDIT_RULE);
CComboBox *pComboAg1 = (CComboBox*)GetDlgItem(ID_EDIT_AG1);
CComboBox *pComboAg2 = (CComboBox*)GetDlgItem(ID_EDIT_AG2);
CComboBox *pComboAg3 = (CComboBox*)GetDlgItem(ID_EDIT_AG3);
CString &AG1 = CString(_T("A string"));
CString &AG2 = CString(_T("A string"));
CString &AG3 = CString(_T("A string"));
CString str;
pComboAg1->GetLBText(pComboAg1->GetCurSel(), AG1);
pComboAg2->GetLBText(pComboAg2->GetCurSel(), AG2);
pComboAg3->GetLBText(pComboAg3->GetCurSel(), AG3);
try {
std::string s = CStringA(AG1);
sql::PreparedStatement *pstmt = con->prepareStatement("SELECT `nAccessIdn` FROM `base`.`tb_table` WHERE `field` IN (?, ?, ?) LIMIT 3");
pstmt->setString(1, s);
pstmt->setString(2, s);
pstmt->setString(3, s);
sql::ResultSet *res = pstmt->executeQuery();
if (res->rowsCount() != 0) {
while (res->next())
{
str.Format(_T("AG : %s\r\n"), res->getString(1));
OutputDebugString(str);
}
}
}
catch (const std::bad_alloc& e) {
CString itemString;
itemString.Format(_T("SQLException %s"), CString(e.what()));
MessageBox(itemString, _T("Failed Logon Attempt"), MB_OK | MB_ICONERROR);
}
我已经尝试了很多类似的东西
std::string s((LPCTSTR)AG1);
或
std::basic_string<TCHAR>
或
std::string std(somStr, someStr.GetLength());
或
CT2A(cst.GetString());
或
char* myStr = "This is a C string!";
std::string myCppString = myStr;
等等……
有人知道成功的方法吗?
详情:
Visual Studio 2017 社区版 - Unicode 项目 - 运行时库:/MDd
非常感谢您的帮助:)
编辑:C++ 新手,代码已更新。
【问题讨论】:
-
你试过了 - 出了什么问题?
-
没有解决办法...
-
那意味着什么?
-
因为它是一个宽字符串构建?
-
我经常遇到 bad_alloc 异常,但我不明白为什么,因为当我想使用指针时,某些函数或转换不起作用。这是合乎逻辑的,但我是 C++ 新手,我不知道如何处理它......
标签: c++ string unicode type-conversion c-strings