sql1 = "SELECT * FROM table01 where num='searchid';" try: cursor.execute(sql1) results = cursor.fetchall() print(results) for i in results: print(f'姓名:{i[1]}\t,性别:{i[2]}\t,学号:{i[3]}') # print(i) except: print('数据查询失败')
时间: 2023-06-10 09:05:42 浏览: 54
这段代码是从名为 table01 的表里根据 num 进行搜索,并将搜索结果打印出来。其中 searchid 是变量,可以根据需要更改。查询结果会存储在 results 中,然后通过 for 循环将每一条数据按固定的格式打印出来,包括姓名、性别和学号。
相关问题
为什么执行这个代码没有数据sql1 = "SELECT * FROM table01 where num='searchid';" try: cursor.execute(sql1) results = cursor.fetchall() print(results) for i in results: print(f'姓名:{i[1]}\t,性别:{i[2]}\t,学号:{i[3]}') # print(i) except: print('数据查询失败')
执行这个代码没有数据是因为SQL语句中的searchid没有被替换为需要查找的实际值。应该将语句改为:sql1 = "SELECT * FROM table01 where num='" + searchid + "';",其中searchid是实际需要查询的值。如果searchid没有被正确赋值,也会出现没有数据的情况。
用结构体struct变量,设计一个学生期末成绩查询程序。 要求: 1.输入学生姓名或者学号查找对应期末成绩。 2.按照分数高低将所有同学成绩依次输出。 3.应有简易菜单提示
学生期末成绩查询程序的设计:
1. 首先定义一个结构体struct,用来存储学生的信息,如学生姓名、学号和期末成绩。
2. 然后根据需要输入学生的信息,并存入结构体数组中。
3. 接下来,根据用户输入的姓名或学号,通过循环比较找到对应的学生信息,并输出期末成绩。
4. 最后,按照分数高低顺序将所有学生的信息输出,并在菜单中提示用户选择。
代码示例:
```
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student
{
char name[20];
int id;
int score;
};
int main()
{
struct student students[MAX_STUDENTS];
int n, i, j;
char searchName[20];
int searchID;
printf("请输入学生人数:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("请输入第%d位学生的姓名:", i + 1);
scanf("%s", students[i].name);
printf("请输入第%d位学生的学号:", i + 1);
scanf("%d", &students[i].id);
printf("请输入第%d位学生的期末成绩:", i + 1);
scanf("%d", &students[i].score);
}
while (1)
{
printf("\n\n");
printf("1. 按姓名查询\n");
printf("2. 按学号查询\n");
printf("3. 按分数高低排序\n");
printf("4. 退出\n");
printf("请选择:
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)