#include<iostream> #include<string> #include"A.h" #include <winsock.h> //注意顺序,要放在mysql.h前 #include <mysql.h>//控制台项目中要在mysql.h之前include <winsock.h> using namespace std; int main() { //第一步初始化 MYSQL* pConn; pConn = mysql_init(NULL); //第二步连接 //第2、3、4、5参数的意思分别是:服务器地址、用户名、密码、数据库名,第6个为mysql端口号(0为默认值3306) if (!mysql_real_connect(pConn, "localhost", "root", "", "market", 0, NULL, 0)) { cout<<"无法连接数据库"<<mysql_error(pConn)<<endl; } else cout << "success" << endl; mysql_query(pConn, "set names gbk");//防止乱码。设置和数据库的编码一致就不会乱码 //第三步查询 if (mysql_query(pConn, "insert into user values(4,'jack',11122233344,'女','sscja')")) { printf("查询失败:%s", mysql_error(pConn)); } MYSQL_RES* result = mysql_store_result(pConn); MYSQL_ROW row; while (row = mysql_fetch_row(result)) { printf("%s %s ", row[1], row[2]); } //第四步释放内存 mysql_free_result(result); //第五步关闭连接 mysql_close(pConn); system("pause"); return 0; }