vs2015连接mysql进行数据库操作


要求:电脑提前安装好vs,mysql。

1、在需要连接mysql的项目上右键选择“属性” -> “C/C++” -> “常规” ->选择“附加包含目录”

在弹出窗口中添加mysql的安装目录,我的是“C:\Program Files\MySQL\MySQL Server 8.0\include”->确定。

2、在“链接器” ->"常规" ->“附加库目录”,添加mysql安装目录下的lib所在路径。我的是“C:\Program Files\MySQL\MySQL Server 8.0\lib”

3、在“链接器” ->"输入" ->“附加依赖项”,添加“libmysql.lib”(这个地方添加的都一样)

4、将C:\Program Files\MySQL\MySQL Server 8.0\lib下的libmysql.dll拷贝到项目文件夹下。

5、运行测试

 1 #include <mysql.h> 
 2 #include <iostream>
 3 #include<string>
 4 #include<vector>
 5 using namespace std;
 6 
 7 int main() {
 8     const char user[] = "root";         //username
 9     const char pswd[] = "*********";         //password
10     const char host[] = "localhost";    //or"127.0.0.1"
11     const char table[] = "industry";        //database
unsigned int port = 3306; 13 MYSQL mysql; 14 mysql_init(&mysql); 15 if (mysql_real_connect(&mysql, host, user, pswd, table, port, NULL, 0)) 16 { 17 cout << "connect success!" << endl; 18 return true; 19 } 20 else 21 { 22 cout << "connect failed!" << endl; 23 return false; 24 } 25 }

 

 

注:若提示#include<mysql.h>有错误,则应检查本地编辑器的配置跟前四步添加库的时候的配置是否一致。

我在前面添加库时配置是:release x64.

则本地配置也应该选择:

 

 

 

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM