代码基本一致,修改sql语句后,使用一个变量接收值,这个时候的值是一个集合,使用for循环进行遍历输出
import pymysql as mysql
# 连接database 给出host本地的地址 user和password 连接数据库的账号
# 和密码 字符编码 数据库的名称
conn=mysql.connect(host="127.0.0.1",user="root",
password="root",charset="utf8",database="student")
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示
# 使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute("select sname from t_gread where sno=1825123602")
res=cursor.fetchall()
for x in res:
print(x)
使用order by排序的时候,与上述代码一致,修改sql语句为按sno(学号)进行排序即可。
import pymysql as mysql
# 连接database 给出host本地的地址 user和password
# 连接数据库的账号和密码 字符编码 数据库的名称
conn=mysql.connect(host="127.0.0.1",user="root",
password="root",charset="utf8",database="student")
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示
# 使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute("select * from t_gread order by sno")
res=cursor.fetchall()
for x in res:
print(x)