使用pymysql进行数据Where条件查询和order by排序

代码基本一致,修改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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Modify_QmQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值