码迷,mamicode.com
首页 > 数据库 > 详细

Python学习记录-连接mysql

时间:2020-01-31 10:56:13      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:select   port   style   cut   common   connector   col   round   pass   

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数据库完成。

Python学习记录-连接mysql

标签:select   port   style   cut   common   connector   col   round   pass   

原文地址:https://www.cnblogs.com/wooluwalker/p/12244535.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!