C++用mysql自带的头文件连接数据库
mysql.h文件在哪,怎么查找。自行百度
#include<mysql/mysql.h> #include<stdio.h> #include<iostream> #include<fstream> #include<string.h> usingnamespacestd; MYSQL*conn; MYSQL_RES*res; MYSQL_ROWrow; classpeople { public: charname[20]; intpid; inttype; charphone[30]; public: people(inta){}; people(){ setall(); }; ~people(){}; public: voidsetall(); }; voidpeople::setall() { cout<<"请输入该用户的编号"<<endl; cin>>pid; cout<<"请输入该用户的名字"<<endl; //gets(name); cin>>name; cout<<"请输入该用户的类型"<<endl; cin>>type; cout<<"请输入该用户的联系方式"<<endl; cin>>phone; } voidsave() { charsql[1000]; peoplea; sprintf(sql,"insertintostudentvalues(%d,'%s',%d,'%s')",a.pid,a.name,a.type,a.phone); if(mysql_query(conn,sql)) { printf("添加失败:(%s)\n",mysql_error(conn)); return; } else { printf("添加成功!\n"); return; } return; } voidupdate(){ charsql[1000]; peoplea(1); cout<<"请输入你要更改的用户的编号:"; cin>>a.pid; cout<<"请输入你要此编号用户的姓名:"; cin>>a.name; cout<<"请输入你要更改的用户的类型:"; cin>>a.type; cout<<"请输入你要更改的用户的电话:"; cin>>a.phone; sprintf(sql,"updatestudentsetname='%s',usetype=%d,phone='%s'wherepid=%d",a.name,a.type,a.phone,a.pid); if(mysql_query(conn,sql)) { printf("更改失败:(%s)\n",mysql_error(conn)); return; } else { printf("更改成功!\n"); return; } return; } voiddel() { charsql[1000]; intpid; cout<<"请输入你要删除的人的编号"<<endl; cin>>pid; sprintf(sql,"deletefromstudentwherepid=%d",pid); if(mysql_query(conn,sql)) { printf("删除失败(%s)\n",mysql_error(conn)); return; } else { printf("删除成功!\n"); return; } return; } voidmenu() { cout<<"1.用户录入"<<endl; cout<<"2.显示"<<endl; cout<<"3.更改"<<endl; cout<<"4.删除"<<endl; cout<<"5.退出"<<endl; } voidshow() { if(mysql_query(conn,"select*fromstudent")){ fprintf(stderr,"%s\n",mysql_error(conn)); return; } res=mysql_use_result(conn); printf("编号\t名字\t类型\t联系方式\n"); while((row=mysql_fetch_row(res))!=NULL){ cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<endl; } mysql_free_result(res); } intmain(){ ints; conn=mysql_init(NULL); if(!mysql_real_connect(conn,"localhost", "root","root","abc",0,NULL,0)){ fprintf(stderr,"%s\n",mysql_error(conn)); return-1; } mysql_query(conn,"setnamesutf8"); while(true){ menu(); cin>>s; if(s==2){show();} if(s==1){save();} if(s==3){update();} if(s==4){del();} if(s==5){mysql_close(conn);return0;} cout<<"按任意键继续.."<<endl; getchar(); } return0; }