mysql 头文件_C++用mysql自带的头文件连接数据库

mysql.h文件在哪,怎么查找。自行百度

#include

#include

#include

#include

#include

using namespace std;

MYSQL *conn;

MYSQL_RES *res;

MYSQL_ROW row;

class people

{

public:

char name[20];

int pid;

int type;

char phone[30];

public:

people(int a){};

people(){

setall();

};

~people(){};

public:

void setall();

};

void people::setall()

{

cout<

cin>>pid;

cout<

// gets(name);

cin>>name;

cout<

cin>>type;

cout<

cin>>phone;

}

void save()

{

char sql[1000];

people a;

sprintf(sql,"insert into student values(%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;

}

void update(){

char sql[1000];

people a(1);

cout<

cin >> a.pid;

cout<

cin >> a.name;

cout <

cin >> a.type;

cout << "请输入你要更改的用户的电话:";

cin >> a.phone;

sprintf(sql,"update student set name = '%s',usetype=%d,phone='%s' where pid = %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;

}

void del()

{

char sql[1000];

int pid;

cout<

cin>>pid;

sprintf(sql,"delete from student where pid = %d",pid);

if(mysql_query(conn, sql))

{

printf("删除 失败(%s)\n",mysql_error(conn));

return;

}

else

{

printf("删除成功!\n");

return;

}

return;

}

void menu()

{

cout<

cout<

cout<

cout<

cout<

}

void show()

{

if (mysql_query(conn, "select * from student")) {

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<

}

mysql_free_result(res);

}

int main() {

int s;

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,"set names utf8");

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);return 0;}

cout<

getchar();

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值