Python学习记录-连接mysql

余生长醉 提交于 2020-01-31 02:15:25

1 安装python的mysql驱动

$ conda install  mysql-connector-python

2 测试mysql驱动是否安装成功

import不报错,即说明安装成功

>>> import mysql.connector

3 查询数据

# 导入MySQL驱动:
import mysql.connector
# 连接本地mysql
conn = mysql.connector.connect(user='userName', password='passWord', database='sakila')
cursor = conn.cursor()
cursor.execute('select * from actor limit 10')
values = cursor.fetchall()
values
# 关闭Cursor和Connection:
cursor.close()

conn.close()

查询被mysql数据库完成。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!