Visual Studio 2022下C++桌面应用无法连接MySQL数据库
C++桌面应用连接MySQL时抛出sql::SQLException,错误码0
我用MySQL Workbench创建了名为db的数据库,本地MySQL服务器正常运行,但在Visual Studio 2022开发的C++桌面应用中点击登录按钮连接数据库时,弹出错误提示“Failed to connect to database: 0”,同时输出窗口抛出sql::SQLException异常。以下是相关代码片段,不确定是不是漏了必要配置,求帮忙排查:
case WM_COMMAND: { int wmId = LOWORD(wParam); // Parse the menu selections: switch (wmId) { /* */ case LOGIN_BUTTON: // Login button was clicked { wchar_t username[256], password[256]; GetWindowText(User_Box, username, 256); GetWindowText(Password_Box, password, 256); // Convert WCHAR to std::string or directly use wide character strings in your SQL queries depending on your setup // This example shows a simple conversion to ANSI strings for demonstration purposes char usernameA[256], passwordA[256]; size_t usernameA_size, passwordA_size; wcstombs_s(&usernameA_size, usernameA, sizeof(usernameA), username, _TRUNCATE); wcstombs_s(&passwordA_size, passwordA, sizeof(passwordA), password, _TRUNCATE); try { sql::Driver* driver; sql::Connection* con; std::unique_ptr<sql::Statement> stmt; std::unique_ptr<sql::ResultSet> res; /* Create a connection */ driver = get_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); /* Connect to the MySQL test database */ con->setSchema("db"); // Confirmation of successful connection MessageBox(hWnd, L"Successfully connected to the database.", L"Database Connection", MB_ICONINFORMATION); } catch (sql::SQLException& e) { std::wstring error_message = L"Failed to connect to database: "; error_message += std::to_wstring(e.getErrorCode()); MessageBox(hWnd, error_message.c_str(), L"Database Error", MB_ICONERROR); } } break;
排查步骤
验证MySQL Connector/C++项目配置
- 确认项目已正确链接库文件:在项目属性→链接器→输入→附加依赖项中,添加对应版本的
mysqlcppconn.lib(Debug/Release版本需匹配);同时确保包含目录已添加Connector/C++的include路径,库目录添加lib路径 - 运行时依赖检查:将
mysqlcppconn.dll复制到应用程序输出目录,或者把该DLL所在路径加入系统环境变量
- 确认项目已正确链接库文件:在项目属性→链接器→输入→附加依赖项中,添加对应版本的
补充完整错误信息
当前仅显示错误码0,无法定位具体问题。修改catch块,打印完整异常信息:catch (sql::SQLException& e) { std::wstring error_message = L"Failed to connect to database: "; error_message += std::to_wstring(e.getErrorCode()); error_message += L"\nSQL State: "; error_message += std::wstring(e.getSQLState().c_str()); error_message += L"\nError Detail: "; error_message += std::wstring(e.what()); MessageBox(hWnd, error_message.c_str(), L"Database Error", MB_ICONERROR); }SQL状态码和错误描述能直接指出问题,比如权限不足、协议不兼容、服务器未响应等
核对连接参数
- 确认
root用户密码正确,且允许从127.0.0.1通过TCP/IP连接:在MySQL Workbench执行SELECT user, host FROM mysql.user;,查看root对应的host是否包含127.0.0.1或% - 确认MySQL服务器端口为3306:查看配置文件(my.ini/my.cnf)的
port项,或在Workbench连接信息中核实
- 确认
检查字符串转换与使用逻辑
当前代码将用户输入的宽字符转成ANSI字符串,但连接时硬编码了root/root,未用到转换后的变量。后续若要使用用户输入的账号密码,需确保转换无乱码;如果MySQL使用utf8mb4字符集,建议直接使用宽字符版本的Connector/C++接口,避免字符编码问题扩大异常捕获范围
get_driver_instance()也可能抛出非sql::SQLException类型的异常,可临时添加std::exception捕获,排查是否有其他初始化错误:catch (std::exception& e) { std::wstring error_msg = L"Unexpected error: "; error_msg += std::wstring(e.what()); MessageBox(hWnd, error_msg.c_str(), L"Error", MB_ICONERROR); }
内容的提问来源于stack exchange,提问作者adrianfree01
相关产品推荐
相关产品推荐

