c++连接mysql数据库
1.使用VS创建项目
2.右键项目选择属性,进入C/C++ => 常规 => 附加包含目录,添加D:\mysql-5.7.19-win64\include(根据自己安装的MySQL版本及地址填写)
3.在属性页,进入链接器 => 常规 => 附加库目录,添加D:\mysql-5.7.19-winx64\lib(根据自己情况填写)
4.在项目的.\x64\Debug\中添加libmysql.dll(从计算机的资源管理器中添加),该dll文件在D:\mysql-5.7.19-winx64\lib目录下
5.代码测试
#include "stdio.h" #include "mysql.h" int main() { MYSQL * con; //= mysql_init((MYSQL*) 0); MYSQL_RES *res; MYSQL_ROW row; char tmp[400]; //database configuartion char dbuser[30] = "root"; //mysql账号名 char dbpasswd[30] = "123456"; //mysql账号密码 char dbip[30] = "localhost"; char dbname[50] = "student";//数据库名 char tablename[50] = "bl"; //数据表名 char *query = NULL; int x; int y; int rt;//return value unsigned int t; int count = 0; con = mysql_init((MYSQL*)0); if (con != NULL && mysql_real_connect(con, dbip, dbuser, dbpasswd, dbname, 3306, NULL, 0)) { if (!mysql_select_db(con, dbname)) { printf("Select successfully the database!\n"); con->reconnect = 1; query = "set names \'GBK\'"; rt = mysql_real_query(con, query, strlen(query)); if (rt) { printf("Error making query: %s !!!\n", mysql_error(con)); } else { printf("query %s succeed!\n", query); } } } else { MessageBoxA(NULL, "Unable to connect the database,check your configuration!", "", NULL); } //sprintf(tmp, "insert into %s values(%s,%d,%d)", tablename, "null", x, y); //注意如何向具有自增字段的数据库中插入记录 sprintf(tmp, "insert into bl values(null,'x','x','x','x')"); rt = mysql_real_query(con, tmp, strlen(tmp)); if (rt) { printf("Error making query: %s !!!\n", mysql_error(con)); } else { printf("%s executed!!!\n", tmp); } sprintf(tmp, "select * from %s", tablename); rt = mysql_real_query(con, tmp, strlen(tmp)); if (rt) { printf("Error making query: %s !!!\n", mysql_error(con)); } else { printf("%s executed!!!\n", tmp); } res = mysql_store_result(con);//将结果保存在res结构体中 while (row = mysql_fetch_row(res)) { for (t = 0; t<mysql_num_fields(res); t++) { printf("%s ", row[t]); } printf(".............\n"); count++; } printf("number of rows %d\n", count); printf("mysql_free_result...\n"); mysql_free_result(res); mysql_close(con); system("pause"); return 0; }