首页 > 解决方案 > 连接到 MySQL 时如何修复 xdev C++ api 中的“字符串太长”错误

问题描述

我正在使用 MySQL Connector/C++ 8.0.15 连接 MySQL 数据库,当我在 Visual Studio 2017 中运行示例时,它失败并出现错误“字符串太长”。

我的操作系统 Windows 10 和我使用 Visual Studio 2017,我只使用 X DevAPI for C++。

void test_XdevCPlus_demo()
{
    std::string usr = "root";
    std::string pwd = "1030";
    // Connect to MySQL Server on a network machine
    try
    {
        Session mySession(
            SessionOption::USER, usr,
            SessionOption::PWD, pwd,
            SessionOption::HOST, "localhost",
            SessionOption::PORT, 33060,
            SessionOption::DB, "test",
            SessionOption::SSL_MODE, SSLMode::DISABLED
        );

        Schema myDb = mySession.getSchema("test");
    }
    catch (mysqlx::Error::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}

当我运行该示例时,我在本地主机上创建会话,端口 33060 ... STD EXCEPTION: string too long 异常发生在 Impl::from_utf8(*this, utf8) where utf8="root"。// TODO: 明确 utf8 转换

string(const char *other)
    {
        if (!other)
            return;
        std::string utf8(other);
        -- > Impl::from_utf8(*this, utf8);
    }


    catch (std::exception &ex)
    {
        cout << "STD EXCEPTION: " << ex.what() << endl;
        return 1;
    }

你能帮忙吗?

标签: c++mysql

解决方案


我遇到了类似的问题。我在使用 Visual Studio 17 时遇到了字符串转换异常。我通过以下步骤解决了这个问题:

  1. 我将“配置属性->高级”中的“字符集”配置为“使用多字节字符集”。

  2. 我仔细检查了我是在 x64 和发布模式下构建的。看起来二进制文件仅在发布模式下构建。

这为我解决了这个问题。希望有帮助。


推荐阅读