VS2019连接mysql
时间: 2023-09-14 10:15:39 浏览: 38
可以按照以下步骤进行:
1. 下载 MySQL Connector/C++ 的安装包,并进行安装。
2. 在 VS2019 中创建一个 C++ 项目。
3. 在项目中添加 MySQL Connector/C++ 头文件路径和库文件路径。
4. 在代码中加入以下代码:
#include <iostream>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main() {
sql::Driver* driver;
sql::Connection* con;
sql::Statement* stmt;
sql::ResultSet* res;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "username", "password");
stmt = con->createStatement();
stmt->execute("USE database_name");
res = stmt->executeQuery("SELECT * FROM table_name");
while (res->next()) {
cout << res->getInt("id") << ", ";
cout << res->getString("name") << ", ";
cout << res->getInt("age") << endl;
}
delete res;
delete stmt;
delete con;
}
需要注意的是,username, password, database_name 和 table_name 需要根据具体情况进行替换。