Python连接MySQL
http://www.runoob.com/python3/python-mysql-connector.html
由于 MySQL 服务器以独立的进程运行,并通过网络对外服务,所以,需要支持 Python 的 MySQL 驱动来连接到 MySQL 服务器。 MySQL 官方提供了 mysql-connector-python 驱动,但是安装的时候需要给 pip 命令加上参数 --allow-external :
$ pip install mysql-connector-python --allow-external mysql-connector-python
如果上面的命令安装失败,可以试试另一个驱动:
$ pip install mysql-connector
Python 连接到 MySQL 数据库示例:
# 导入MySQL驱动:
import mysql.connector
# 注意把password设为root口令,需要提前创建好lhrdb数据库
conn = mysql.connector.connect(user='root', password='lhr', database='lhrdb',host='127.0.0.1',port=3306)
cursor = conn.cursor()
# 创建user表:
cursor.execute('drop table if exists user')
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
# 插入一行记录,注意MySQL的占位符是%s:
cursor.execute('insert into user (id, name) values (%s, %s)', ['1', 'xiaomaimiao'])
cursor.execute('insert into user (id, name) values (%s, %s)', ['2', 'xiaotinger'])
print(cursor.rowcount)
# 提交事务:
conn.commit()
cursor.close()
# 运行查询:
cursor = conn.cursor()
cursor.execute('select * from user where id = %s', ('1',))
values = cursor.fetchall()
print(values)
# 关闭Cursor和Connection:
cursor.close()
conn.close()
运行结果:
1
[('1', 'xiaomaimiao')]
在MySQL中查询:
mysql> select * from user;
+----+-------------+
| id | name |
+----+-------------+
| 1 | xiaomaimiao |
| 2 | xiaotinger |
+----+-------------+
2 rows in set (0.00 sec)
需要注意的是:
l 执行INSERT等操作后要调用commit()提交事务;
l MySQL的SQL占位符是%s。
真题1、 Python 如何批量往 MySQL 数据库插入数据?
答案:批量插入使用 executemany() 方法,该方法的第二个参数是一个元组列表,包含了需要插入的数据:
# 导入MySQL驱动:
import mysql.connector
# 注意把password设为你的root口令,需要提前创建好lhrdb数据库
conn = mysql.connector.connect(user='root', password='lhr', database='lhrdb',host='127.0.0.1',port=3306)
cursor = conn.cursor()
# 创建user表:
cursor.execute('drop table if exists sites')
cursor.execute('create table sites (name varchar(20) primary key, url varchar(200))')
# 插入多行记录,注意MySQL的占位符是%s:
sql = "insert into sites (name, url) values (%s, %s)"
val = [
('Google', 'https://www.google.com'),
('Github', 'https://www.github.com'),
('Taobao', 'https://www.taobao.com'),
('itpub', 'http://blog.itpub.net/26736162/')
]
cursor.executemany(sql, val)
# 提交事务:
conn.commit()
print(cursor.rowcount, "条记录插入成功。")
cursor.close()
# 运行查询:
cursor = conn.cursor()
cursor.execute('select * from sites')
values = cursor.fetchall()
for x in values:
print(x)
# 关闭Cursor和Connection:
cursor.close()
conn.close()
运行结果:
4 条记录插入成功。
('Github', 'https://www.github.com')
('Google', 'https://www.google.com')
('itpub', 'http://blog.itpub.net/26736162/')
('Taobao', 'https://www.taobao.com')
About Me
........................................................................................................................ ● 本文作者:小麦苗,部分内容整理自网络,若有侵权请联系小麦苗删除 ● 本文在itpub( http://blog.itpub.net/26736162 )、博客园( http://www.cnblogs.com/lhrbest )和个人weixin公众号( xiaomaimiaolhr )上有同步更新 ● 本文itpub地址: http://blog.itpub.net/26736162 ● 本文博客园地址: http://www.cnblogs.com/lhrbest ● 本文pdf版、个人简介及小麦苗云盘地址: http://blog.itpub.net/26736162/viewspace-1624453/ ● 数据库笔试面试题库及解答: http://blog.itpub.net/26736162/viewspace-2134706/ ● DBA宝典今日头条号地址: http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826 ........................................................................................................................ ● QQ群号: 230161599 (满) 、618766405 ● weixin群:可加我weixin,我拉大家进群,非诚勿扰 ● 联系我请加QQ好友 ( 646634621 ) ,注明添加缘由 ● 于 2019-01-01 06:00 ~ 2019-01-31 24:00 在魔都完成 ● 最新修改时间:2019-01-01 06:00 ~ 2019-01-31 24:00 ● 文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解 ● 版权所有,欢迎分享本文,转载请保留出处 ........................................................................................................................ ● 小麦苗的微店 : https://weidian.com/s/793741433?wfr=c&ifr=shopdetail ● 小麦苗出版的数据库类丛书 : http://blog.itpub.net/26736162/viewspace-2142121/ ● 小麦苗OCP、OCM、高可用网络班 : http://blog.itpub.net/26736162/viewspace-2148098/ ● 小麦苗腾讯课堂主页 : https://lhr.ke.qq.com/ ........................................................................................................................ 使用 weixin客户端 扫描下面的二维码来关注小麦苗的weixin公众号( xiaomaimiaolhr )及QQ群(DBA宝典)、添加小麦苗weixin, 学习最实用的数据库技术。 ........................................................................................................................ |
![]() | ![]() |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26736162/viewspace-2557188/,如需转载,请注明出处,否则将追究法律责任。