(1)查询学号为10002学生的所有成绩,结果中需包含学号、姓名、所在系别、课程号、课程名以及对应成绩。
import pymysql.cursors
连接数据库
connect = pymysql.Connect(
host='localhost',
port=3306,
user='root',
passwd='123456789',
db='school',
charset='utf8'
)
获取游标
cursor = connect.cursor()
设置sql语句
sql = "SELECT student.Sno,Sname,Sdept,course.Cno,Cname,Grade " \
"FROM student,course,sc " \
"WHERE student.Sno = sc.Sno AND course.Cno = sc.Cno AND sc.Sno = '%s'"
设置数据
data = ('10002',)
执行sql语句
cursor.execute(sql % data)
获取数据
print("共有%s条记录" % cursor.rowcount)
for row in cursor.fetchall():
print("学号:%s\t姓名:%s\t系别:%s\t课程号:%s\t课程名:%s\t成绩:%d" % row)
关闭数据库连接
cursor.close()
connect.close()