安装mysql模块。
pip install PyMySQL
mysql查询代码
# 导入模块
import pymysql# 1.连接到mysql数据库
conn = pymysql.connect(host='localhost', user='root', password='1234', db='mycommodity', charset='utf8')
# localhost连接本地数据库 user 用户名 password 密码 db数据库名称 charset 数据库编码格式# 2.创建游标对象
cursor = conn.cursor() # cursor当前的程序到数据之间连接管道# 3.组装sql语句 需要查询的MySQL语句
sql = 'select * from commodity'# 4.执行sql语句
cursor.execute(sql)# 5.处理结果集
# 获取一条数据
one = cursor.fetchone()
print(one)
# 获取多条数据 传入需要获取的数据的条数
many = cursor.fetchmany(3)
print(many)# 获取所有数据
all = cursor.fetchall()
# 输出获取到的数据的数据类型
print(type(all))# 逐条输出获取到的数据类型及数据
for each in all:
print(type(each),each)# 获取数据库表中列的参数
fields = cursor.description
head = []
# 或取数据库中表头
for field in fields:
head.append(field[0])
print(head)# 6.关闭所有的连接
# 关闭游标
cursor.close()
# 关闭数据库
conn.close()