【问题标题】:CString to std::string or sql::SQLString conversion - C++CString 到 std::string 或 sql::SQLString 转换 - C++
【发布时间】:2018-03-20 17:52:51
【问题描述】:

我正在尝试将来自 CComboBox 的 CString 变量转换为 std::stringsql::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


【解决方案1】:
CString &AG1 = CString(_T("A string"));
pComboAg1->GetLBText(pComboAg1->GetCurSel(), AG1);

这在很多方面都是错误的。 AG1 是指向某些固定数据的指针,而您正在将 AG1 设置为其他内容。

只需使用:

CString AG1;
pComboAg1->GetLBText(pComboAg1->GetCurSel(), AG1);

您的 SQL 数据应为 UTF-8。如果您正在创建新的 MFC 程序,则默认启用 UNICODE 选项 (UTF-16)。

从数据库中获取数据:

std::string str;
//get str from databases
CStringW atl = CA2W(str.c_str(), CP_UTF8);
combobox.AddString(atl);

向数据库发送数据:

CString AG1;
combobox.GetLBText(0, AG1);
std::string str = CW2A(AG1, CP_UTF8);
...

在极少数情况下,数据可能是 ANSI,在这种情况下删除 CP_UTF8 标志。

【讨论】:

    【解决方案2】:

    CString 到 std::string

    CString cstr = L"Hello";
    std::string str = std::string(CStringA(cstr));
    

    CString 到 sql::SQLString

    CString cstr = L"Hello";
    std::string str = std::string(CStringA(cstr));
    sql::SQLString sqstr = sql::SQLString(str.c_str());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      相关资源
      最近更新 更多