#include "../include/net.h"
//编写一子函数,更新数据库中数据
//参数1 sqlite3 *db
//
int updata(int socket_fd)
{
Honour s1;
char sql[100] = {'\0'};
int d_op = 0,op1 = 0;
printf("1->英雄序号 2->英雄名字 3->英雄价格 -1->退出\n");
printf("请输入要更新的选项:");
scanf("%d",&d_op);
sprintf(sql,"%d",d_op);
send(socket_fd,sql,sizeof(sql),0);
if(-1 == d_op)return -1;
switch(d_op)
{
case 1: //更新id
printf("1->英雄名字 2->英雄价格\n");
printf("要根据以上哪个选项更新英雄序号:");
scanf("%d",&op1);
sprintf(sql,"%d",op1);
send(socket_fd,sql,sizeof(sql),0);
switch(op1)
{
case 1: //按姓名更新id
printf("请输入更新序号的英雄名字:");
scanf("%s",s1.name);
send(socket_fd,s1.name,sizeof(s1.name),0);
printf("请输入更新后的英雄序号:");
scanf("%d",&s1.id);
sprintf(sql,"%d",s1.id);
send(socket_fd,sql,sizeof(sql),0);
sprintf(sql,"update honour set id = %d where name = '%s'",s1.id,s1.name);
send(socket_fd,sql,sizeof(sql),0);
break;
case 2: //按价格更新id
printf("请输入更新序号的英雄价格:");
scanf("%s",s1.price);
send(socket_fd,s1.price,sizeof(sql),0);
printf("请输入更新后的英雄序号:");
scanf("%d",&s1.id);
sprintf(sql,"%d",s1.id);
send(socket_fd,sql,sizeof(sql),0);
sprintf(sql,"update honour set id = %d where price = '%s'",s1.id,s1.price);
send(socket_fd,sql,sizeof(sql),0);
break;
}
break;
case 2: //更新姓名
printf("1->英雄序号 2->英雄价格\n");
printf("要根据以上哪个选项更新英雄名字:");
scanf("%d",&op1);
sprintf(sql,"%d",op1);
send(socket_fd,sql,sizeof(sql),0);
switch(op1)
{
case 1: //按id更新姓名
printf("请输入更新名字的英雄序号:");
scanf("%d",&s1.id);
sprintf(sql,"%d",s1.id);
send(socket_fd,sql,sizeof(sql),0);
printf("请输入更新后的英雄名字:");
scanf("%s",s1.name);
send(socket_fd,s1.name,sizeof(s1.name),0);
sprintf(sql,"update honour set name = '%s' where id = %d",s1.name,s1.id);
send(socket_fd,sql,sizeof(sql),0);
break;
case 2: //按成绩更新姓名
printf("请输入需要更新名字的英雄价格:");
scanf("%s",s1.price);
send(socket_fd,s1.price,sizeof(sql),0);
printf("请输入更新后的英雄名字:");
scanf("%s",s1.name);
send(socket_fd,s1.name,sizeof(s1.name),0);
sprintf(sql,"update honour set name = '%s' where price = '%s'",s1.name,s1.price);
send(socket_fd,sql,sizeof(sql),0);
break;
}
break;
case 3: //更新价格
printf("1->英雄序号 2->英雄名字\n");
printf("要根据以上哪个选项更新英雄价格:");
scanf("%d",&op1);
sprintf(sql,"%d",op1);
send(socket_fd,sql,sizeof(sql),0);
switch(op1)
{
case 1: //按id更新价格
printf("请输入更新价格的英雄序号:");
scanf("%d",&s1.id);
sprintf(sql,"%d",s1.id);
send(socket_fd,sql,sizeof(sql),0);
printf("请输入更新后的英雄价格:");
scanf("%s",s1.price);
send(socket_fd,s1.price,sizeof(sql),0);
sprintf(sql,"update honour set price = '%s' where id = %d",s1.price,s1.id);
send(socket_fd,sql,sizeof(sql),0);
break;
case 2: //按姓名更新价格
printf("请输入更新价格的英雄名字:");
scanf("%s",s1.name);
send(socket_fd,s1.name,sizeof(s1.name),0);
printf("请输入更新后的英雄价格:");
scanf("%s",s1.price);
send(socket_fd,s1.price,sizeof(sql),0);
sprintf(sql,"update honour set price = '%s' where name = '%s'",s1.price,s1.name);
send(socket_fd,sql,sizeof(sql),0);
break;
}
break;
}
recv(socket_fd,sql,sizeof(sql),0);
printf("%s\n",sql);
}
评论0