Python链接MySQL数据库


Python链接MySQL



http://www.runoob.com/python3/python-mysql-connector.html html


因为 MySQL 服务器以独立的进程运行,并经过网络对外服务,因此,须要支持 Python MySQL 驱动来链接到 MySQL 服务器。 MySQL 官方提供了 mysql-connector-python 驱动,可是安装的时候须要给 pip 命令加上参数 --allow-external python

$ pip install mysql-connector-python --allow-external mysql-connector-python mysql

若是上面的命令安装失败,能够试试另外一个驱动: git

$ pip install mysql-connector github

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。

 


真题一、 Python 如何批量往 MySQL 数据库插入数据? sql

答案:批量插入使用 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/,如需转载,请注明出处,不然将追究法律责任。

相关文章
相关标签/搜索