我创建了一个用客户端输入更新用户名的C程序,但我无法更新用户名,任何人都可以帮我解决此问题。
这是我的代码
#include<stdio.h>
#include<mysql.h>
#include<string.h>
#include<stdlib.h>
main()
{
int i,num_fields;
int len,choice;
char name[50],search[50],ename[50];
char query[200];
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW *row;
MYSQL_FIELD *field;
int ret;
char stat[50] = "INSERT INTO Users VALUES('%s')";
char updatename[200] = "UPDATE Users SET name='%s' WHERE name='%s'";
conn = mysql_init(NULL);
ret=mysql_real_connect(conn,"localhost","root","bisptop","testdb",0,NULL,0);
scanf("%d",&choice);
switch(choice)
case 1:
{
insert code
}
case 2:
{
display code
}
case 3:
{
printf("edit name");
scanf("%s",search);
mysql_query(conn,"SELECT * FROM Users");
result=mysql_store_result(conn);
num_fields=mysql_num_fields(result);
while((row=mysql_fetch_row(result)))
{
for(i=0;i<num_fields;i++)
{
if(strcmp(search,row[i])==0)
{
//printf("%s",row[i]);
printf("Enter new name\n");
scanf("%s",ename);
len=snprintf(query,sizeof(updatename)+strlen(ename),updatename,ename);
//printf("%d",len);
printf("%s\n",query);
mysql_query(conn,query);
mysql_close(conn);
}
}
}
break;
}
}
}
答案 0 :(得分:0)
len=snprintf(query,sizeof(updatename)+strlen(ename),updatename,ename);
如果要更新名称,此行不应包含旧名称和新名称吗?我们怎么知道要更新的名称?