ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ### 下载安装包 ***** 1. 官网下载mysql (以5.6为例子)[下载地址](https://dev.mysql.com/downloads/mysql/) 下载的是win32版本的(版本要跟项目版本一致x32 x64) ### 配置 ***** 配置属性 -> C/C++ -> 常规 -> 附加包含目录 -> include文件夹 (vc++目录-》包含目录 设置成mysql5.6/include) 配置属性 -> 连接器 -> 常规 -> 附加库目录 -> lib文件夹 (或者 #pragma comment(lib,"libmysql.lib")) ### 代码示例 ***** ```c++ #include "stdafx.h" #include <iostream> #include <stdio.h> #include <winsock.h> #include <mysql.h> using namespace std; #pragma comment(lib,"wsock32.lib") #pragma comment(lib,"libmysql.lib") int main() { const char user[] = "root"; const char pswd[] = "root"; const char host[] = "11.11.111.111"; const char table[] = "DB_NAME"; unsigned int port = 3306; MYSQL myCont; MYSQL_RES *result; MYSQL_ROW sql_row; int res; mysql_init(&myCont); if (mysql_real_connect(&myCont, host, user, pswd, table, port, NULL, 0)) //if (mysql_real_connect(&myCont, host, user, pswd,0,0,NULL,0)) { mysql_query(&myCont, "SET NAMES UTF8"); //设置编码格式 // mysql_select_db(&myCont, table); res = mysql_query(&myCont, "select * from level_1");//查询 if (!res) { result = mysql_store_result(&myCont); cout << "结果集数量:" << mysql_num_rows(result) +1<< endl; if (result) { while (sql_row = mysql_fetch_row(result))//获取具体的数据 { cout << "ID:" << sql_row[1] << endl; cout << "Name:" << sql_row[2] << endl; } if(!mysql_query(&myCont, "insert into level_1(Name,Count) values('DongGuoZheng',2)")){ cout << "插入成功" << endl; } else { cout << "shibai" << endl; cout << mysql_error(&myCont) << endl; } } } else { cout << "query sql failed!" << endl; } } else { cout << "connect failed!" << endl; } if (result != NULL) mysql_free_result(result); mysql_close(&myCont); system("pause"); return 0; } ```