②在vs2019中创建新项目,注意x64
③ 右击项目打开属性
④添加include路径
⑤添加lib路径
点击确定后点击应用
⑥
点击全部确定
⑦
⑧启动mysql
进入数据库:
在数据库中创建student的表
⑨在va2019中输入下面代码测试
注意:密码换成自己的数据库密码
//连接数据库
if (mysql_real_connect(&mysql, "127.0.0.1", "root", "123456_zc", "school", 3306, NULL, 0) == NULL)
{
cout << "错误原因:" << mysql_error(&mysql);
cout << "连接失败";
exit(-1);
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<mysql.h>using namespace std;int main()
{MYSQL mysql;//数据库句柄MYSQL_RES* res;//查询结果集MYSQL_ROW row;//记录结构体//初始化数据库mysql_init(&mysql);//设置字符编码mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");//连接数据库if (mysql_real_connect(&mysql, "127.0.0.1", "root", "123456_zc", "school", 3306, NULL, 0) == NULL){cout << "错误原因:" << mysql_error(&mysql);cout << "连接失败";exit(-1);}//查询数据int ret = mysql_query(&mysql, "select *from student;");cout << "ret=" << ret;cout << endl;//获取结果集res = mysql_store_result(&mysql);//给ROW赋值,判断ROW是否为空,不为空就打印数据while (row = mysql_fetch_row(res)){cout << row[0]<<" ";//打印idcout << row[1]<< " ";//打印namecout << row[2]<< " ";//打印classcout << row[3]<< " ";//打印sexcout << endl;}//释放结果集mysql_free_result(res);//关闭数据库mysql_close(&mysql);system("pause");return 0;}
成功!!!