# _*_ coding: utf-8 _*_
# @Time : 2020/8/7 22:20
# @Author : BingL
# @Email : 15736980819@163.com
# @File : 操作MySQL数据库查询一条数据.py
# @Software: PyCharm
# @Desc :静,是一种修养。
# cur.fetchone() 查询一条数据
# cur.fetchmany() 查询任意条数据
# cur.fetchall() 查询所有数据
# 导入pymysql
import pymysql
# 创建连接
con = pymysql.connect(host='localhost', user='root', password='bing', port=3306, database='python_db')
# 创建游标
cur = con.cursor()
# 编写查询的sql
# sql = 'select * from t_student'
sql = 'select * from t_student where age = 23'
try:
# 执行sql
cur.execute(sql)
# 处理结果集
student = cur.fetchone()
sno = student[0]
sname = student[1]
age = student[2]
score = student[3]
print('sno:', sno, 'sname:', sname, 'age:', age, 'score:', score)
print('查询一条数据成功')
except Exception as e:
print(e)
print('查询一条数据失败')
finally:
# 关闭连接
con.close()
操作MySQL数据库查询一条数据
最新推荐文章于 2024-08-04 10:06:58 发布