头文件
#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/exception.h>
using namespace std;
using namespace sql::mysql;
using namespace sql;
问题
刚连接时我们可能会出现这个问题
解决方法 1
1、点击“工具”->“选项”
2、点击“跨平台”->“连接管理器”->“远程标头IntelliSense” ->“更新”
3、更新中
刚刚报错就消失了
解决方法2
1、“项目”->“清除浏览数据库错误”
2、“项目”->“重新扫描解决方案”
如果以上都解决不了可能是MySQL没有安装好,重装吧
简单测试
#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/exception.h>
using namespace std;
using namespace sql::mysql;
using namespace sql;
int main()
{
try
{
// 加载MySQL驱动
MySQL_Driver* driver = sql::mysql::get_driver_instance();
// 连接数据库系统
Connection* conn = driver->connect("tcp://127.0.0.1:3306", "root", "123456");
// 打开数据库
conn->setSchema("mysql");
cout << "Connected 2 MySQL successful" << endl;
}
catch (sql::SQLException &e)
{
cout << "Error: " << e.what() << endl;
}
return 0;
}