头图 | CSDN 下载自视觉中国
安装PyMySQL库
如果你想要利用python操作MySQL数据库,就必须先要安装pymysql库,这个库的安装很大略,直策应用pip install pymysql;如果这种办法还是安装不上,就用如下链接找一个得当的安装包,进行安装,这个就不细说了。

https://www.lfd.uci.edu/~gohlke/pythonlibs/
利用Python连接MySQL数据库
1)六个常用的连接参数
参数host:mysql做事器所在的主机的ip;
参数user:用户名
参数password:密码
参数port:连接的mysql主机的端口,默认是3306
参数db:连接的数据库名
参数charset:当读取数据涌现中文会乱码的时候,须要我们设置一下编码;我们利用python操作数据库的时候,那么python就相称于是client,我们是用这个client来操作mysql的server做事器,python3默认采取的utf8字符集,我的mysql做事器默认采取latin1字符集,因此mysql中创建的每张表,都是建表的时候加了utf8编码的,因此这里设置的该当便是connection连接器的编码。
2)python连接mysql的语法
import pymysql
db=pymysql.connect(host='localhost',user='root',password='123456',
port=3306,db='spiders',charset=' utf8')
最基本的参数是host,user,password和port,必须要有。剩下两个参数根据你自己的情形决定是否利用。
host指的是mysql做事器安装在哪里,由于我的mysql便是安装在本机上,因此这里可以写localhost,我也可以写成主机名或者主机ip。
db指的是你要操作的是哪一个数据库,在进行数据库连接的时候,最好加上这个参数。
3)一个大略的热身案例
# 导包
import pymysql
# 利用pymysql连接上mysql数据库做事器,创建了一个数据库工具;
db=pymysql.connect(host='localhost',user='root', password='123456',
port=3306, db='huangwei', charset='utf8')
# 开启mysql的游标功能,创建一个游标工具;
cursor = db.cursor
# 要实行的SQL语句;
sql = \公众select from student\"大众
# 利用游标工具实行SQL语句;
cursor.execute(sql)
# 利用fetchone方法,获取返回的结果,但是须要用变量保存返回结果;
data = cursor.fetchone
print(data)
# 断开数据库的连接,开释资源;
db.close
结果如下:
Cursor游标工具的一些常用方法
1)cursor用来实行命令的方法
execute(query, args):实行单条sql语句,吸收的参数为sql语句本身和利用的参数列表,返回值为受影响的行数;
executemany(query, args):实行单条sql语句,但是重复实行参数列表里的参数,返回值为受影响的行数;
2)cursor用来吸收返回值的方法
fetchone:返回一条结果行;
fetchmany(size):吸收size条返回结果行。如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据;
fetchall:吸收全部的返回结果行;
创建表(建)
import pymysql
db=pymysql.connect(host='localhost',user='root', password='123456',
port=3306, db='huangwei', charset='utf8')
# 创建一个游标工具;
cursor = db.cursor
# 建表语句;
sql = \"大众\"大众\公众create table person(
id int auto_increment primary key not ,
name varchar(10) not ,
age int not ) charset=utf8\公众\"大众\公众
# 实行sql语句;
cursor.execute(sql)
# 断开数据库的连接;
db.close
把稳:你在mysql中sql语句怎么写,在这里就怎么写。还有一个细节须要把稳的是,在python中,将代码进行多次换行的时候,最好利用“三引号”。
查询数据(查)
1)fetchone:一次获取一条记录
import pymysql
db = pymysql.connect(host='localhost',user='root',db='huangwei',
password='123456',port=3306,charset='utf8')
cursor = db.cursor
cursor.execute('select count() from person')
aa = cursor.fetchone
print(aa)
# 把稳这一句一定是在循环之外,不能放到循环里面。
# 想想这是为什么?
cursor.execute('select name,age from person')
for i in range(aa[0]):
a,b = cursor.fetchone
c = \"大众我的名字叫{},今年{}岁\"大众.format(a,b)
display(c)
db.close
结果如下:
2)fetchall:一次获取所有记录
import pymysql
db = pymysql.connect(host='localhost',user='root',db='huangwei',
password='123456',port=3306,charset='utf8')
cursor = db.cursor
cursor.execute('select name,age from person')
aa = cursor.fetchall
# print(aa)
for a,b in aa:
c = \"大众我的名字叫{},今年{}岁\"大众.format(a,b)
display(c)
db.close
结果如下:
注:还有一个fetchmany方法,用于一次性获取指定条数的记录,请自行下去研究。
3)利用pandas中的read_sql方法,将提取到的数据直接转化为DataFrame,进行操作
import pymysql
import pandas as pd
db = pymysql.connect(host='localhost',user='root',db='huangwei',
password='123456',port=3306,charset='utf8')
cursor = db.cursor
df1 = pd.read_sql(\公众select from student where ssex='男'\"大众,db)
display(df1)
df2 = pd.read_sql(\"大众select from student where ssex='女'\"大众,db)
display(df2)
结果如下:
插入数据(增)
1)一次性插入一条数据
import pymysql
db=pymysql.connect(host='localhost',user='root', password='123456',
port=3306, db='huangwei', charset='utf8')
cursor = db.cursor
# mysql中SQL语句怎么写,这里就怎么写;
name = \公众猪八戒\"大众
age = 8000
sql = 'insert into person(name,age) values (\"大众猪八戒\"大众,8000)'
try:
cursor.execute(sql)
db.commit
print(\公众插入成功\"大众)
except:
print(\公众插入失落败\公众)
db.rollback
db.close
1.1)一次性插入一条数据
import pymysql
db=pymysql.connect(host='localhost',user='root', password='123456',
port=3306, db='huangwei', charset='utf8')
cursor = db.cursor
# 插入数据
sql = 'insert into person(name,age) values(%s,%s)'
try:
cursor.execute(sql,('孙悟空',100000))
db.commit
print(\公众插入成功\"大众)
except:
print(\"大众插入失落败\"大众)
db.rollback
db.close
2)一次性插入多条数据
import pymysql
db=pymysql.connect(host='localhost',user='root', password='123456',
port=3306, db='huangwei', charset='utf8')
cursor = db.cursor
# 插入数据
sql = 'insert into person(name,age) values(%s,%s)'
# 把稳:(('牛魔王',9000),('铁扇公主',8000),('玉皇大帝',6000))也可以
# 小括号都可以换为中括号
datas = [('牛魔王',9000),('铁扇公主',8000),('玉皇大帝',6000)]
try:
cursor.executemany(sql,datas)
db.commit
print(\"大众插入成功\"大众)
except:
print(\"大众插入失落败\"大众)
db.rollback
db.close
更新数据(改)
import pymysql
db=pymysql.connect(host='localhost',user='root', password='123456',
port=3306, db='huangwei', charset='utf8')
cursor = db.cursor
# 更新数据
sql = 'update person set age=%s where name=%s'
try:
cursor.execute(sql,[90000,\"大众玉皇大帝\"大众])
db.commit
print(\公众更新成功\公众)
except:
print(\"大众更新失落败\公众)
db.rollback
db.close
删除数据(删)
import pymysql
db=pymysql.connect(host='localhost',user='root', password='123456',
port=3306, db='huangwei', charset='utf8')
cursor = db.cursor
# 删除数据
sql = 'delete from person where age=8000'
try:
cursor.execute(sql)
db.commit
print(\"大众删除成功\"大众)
except:
print(\"大众删除失落败\"大众)
db.rollback
db.close
总结如下:
PyMySQL模块是默认开启MySQL的事务功能的,因此,进行 \"大众增\"大众、 \"大众删\公众、\"大众改\"大众的时候,一定要利用db.commit提交事务,否则就看不见所插入的数据。
进行 \公众增\公众、\公众删\公众、\"大众改\"大众的时候,一定要利用try…except…语句,由于万一没插入成功,别的代码都无法实行。当语句实行不堪利,我们就db.rollback回滚到操作之前的状态;当语句实行成功,我们就db.commit提交事务。
作者:Huang supreme,个人博客地址:https://blog.csdn.net/weixin_41261833。
声明:本文系作者投稿,版权归其所有。