c++ - 连接到 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;
}
你能帮忙吗?
解决方案
我遇到了类似的问题。我在使用 Visual Studio 17 时遇到了字符串转换异常。我通过以下步骤解决了这个问题:
我将“配置属性->高级”中的“字符集”配置为“使用多字节字符集”。
我仔细检查了我是在 x64 和发布模式下构建的。看起来二进制文件仅在发布模式下构建。
这为我解决了这个问题。希望有帮助。
推荐阅读
- javascript - 如何显示这种类型的位模式?
- python - 表单不显示在html,django上
- javascript - 从数组中,生成所有不同的、非空的子数组,并保持顺序
- swift - 复制构造函数和“无法调用非函数类型的值”错误
- typescript - SPFx:将新项目添加到现有列表文件夹(使用 pnp)
- visual-studio-code - 使 VS Code 模拟 Visual Studio 的 ctrl pageUp 以将光标移动到视图顶部
- typescript - 你能让工厂函数类型的参数安全吗?
- tensorflow - 将模型另存为 h5 / 将模型另存为 .ckpt
- python - 为什么在 `python3 __main__.py` 工作时`python3 -m project` 会产生错误?
- windows-7 - Electron-Angular9 应用程序在 Windows 7 中显示空白屏幕,但在 Windows 8 上运行良好