百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文
python使用pymysql在mysql的student表查询数据

python使用pymysql在mysql的student表查询数据

  • 网站名称:python使用pymysql在mysql的student表查询数据
  • 网站分类:技术文章
  • 收录时间:2025-07-08 14:43
  • 网站地址:

进入网站

“python使用pymysql在mysql的student表查询数据” 网站介绍

import pymysql

# 链接数据库
db = pymysql.connect(host='localhost', port=3306, user='root', password='root', \
                     db='student_info', charset='utf8')
# 获取游标
cursor = db.cursor()
# sql语句
sql_cmd = "SELECT sno,sname, ssex, sage FROM student WHERE sno = '%s';"
# 数据
data = ('95002',)
# 执行语句
cursor.execute(sql_cmd % data)
# 获取数据
data = cursor.fetchall()
# 打印数据
for item in data:
    print("学号:%s;\t姓名:%s;\t性别:%s;\t年龄:%d" % item)
print("共查找出", cursor.rowcount, "条数据")

# 关闭数据库链接
cursor.close()
db.close()