mysql驱动
mysqlclient
安装
pip install mysqlclient
连接数据库
操作数据
获取django配置好的连接对象
from django.db import connection
获取游标对象
cursor = connection.cursor()
执行SQL语句
cursor.execute(sql)
获取查询结果
rows = cursor.fetchall()
遍历查询结果得到一个一个的数据
for row in rows:
print(row)
演练
创建数据表
插入数据
结果
查询数据
查询的结果会保存在游标对象中
通过方法拿值
fetchone
fetchall
fetchmany