python版本:Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
mysql版本:Server version: 5.7.21-log MySQL Community Server (GPL)
pymsql版本:PyMySQL (0.8.0)
一:介绍
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。
Django中也可以使用PyMySQL连接MySQL数据库。
二:准备工作
1.pymsql安装:
1 pip install pymysql
2.pymsql连接数据库:
1 import pymysql
2
3 conn = pymysql.connect(
4 host='127.0.0.1',
5 port=3306,
6 user='root',
7 password='000000',
8 database='darkfitch_for_test',
9 charset='utf8'
10 )
11 //此处连接的是本地的mysql,端口为默认端口,连接到名为darkfitch_for_test的数据库,使用编码为utf-8
注意:在设置charset的时候,不要设置成utf-8.
三:pymysql对数据库内容的增删改查操作
建议在操作数据库时进行容错处理,即rollback.
1.增加数据
1.1增加单行数据


1 # 导入pymsql模块
2 import pymysql
3 # 建立一个叫conn的连接
4 conn = pymysql.connect(
5 host='127.0.0.1', #连接为本地地址
6 port=3306, #端口号,不写也默认为3306
7 user='root', #mysql用户名
8 password='000000', #mysql密码
9 database='darkfitch_for_test',#选择在musql中的一个数据库
10 charset='utf8' #编码设置为utf-8
11 )
12
13 cursor = conn.cursor() #建立游标
14 sql = 'insert into authors (a_name)VALUES (%s);' #插入一条数据到authors表,字段为a_name 插入方式为格式化插入
15 cursor.execute(sql,'ccc') #execute为执行,格式化的数据在这里传值 以规避SQl注入
16 conn.commit() #只有执行commit()后数据才会真正插入表中
17 cursor.close()
18 conn.close()
增加单行数据
1.2增加多行数据


1 # 导入pymsql模块
2 import pymysql
3 # 建立一个叫conn的连接
4 conn = pymysql.connect(
5 host='127.0.0.1', #连接为本地地址
6 port=3306, #端口号,不写也默认为3306
7 user='root', #mysql用户名
8 password='000000', #mysql密码
9 database='darkfitch_for_test',#选择在musql中的一个数据库
10 charset='utf8' #编码设置为utf-8
11 )
12
13 cursor = conn.cursor() #建立游标
14 sql = 'insert into authors (a_name)VALUES (%s);' #插入一条数据到authors表,字段为a_name 插入方式为格式化插入
15
16 data = ['asd','sdf','dfg','fgh']
17 cursor.executemany(sql,data) #execute为执行,这里传递的是一个赋值为可迭代对象的变量
18 conn.commit() #只有执行commit()后数据才会真正插入表中
19 cursor.close()
20 conn.close()
增加多行数据
注意:
executemany批量提交是在批量上传后一次提交,如果传输数据有问题需要回滚,则本次上传的所有数据都不会被插入表中.
1.3失败回滚操作


1 # 导入pymsql模块
2 import pymysql
3 # 建立一个叫conn的连接
4 conn = pymysql.connect(
5 host='127.0.0.1', #连接为本地地址
6 port=3306, #端口号,不写也默认为3306
7 user='root', #mysql用户名
8 password='000000', #mysql密码
9 database='darkfitch_for_test',#选择在musql中的一个数据库
10 charset='utf8' #编码设置为utf-8
11 )
12
13 cursor = conn.cursor()
14 sql = "insert into publisher (a_name,addr) VALUES (%s,%s)"
15 data = [("Alex", 'aaa'), ("Egon", ), ("Yuan", 'ccc')] #此处故意设置了一个错误参数
16 try: #容错处理
17 cursor.executemany(sql,data)
18 conn.commi()
19 except Exception as e:
20 conn.rollback() #出错后回滚
21
22 cursor.close()
23 conn.close()
失败回滚操作
1.4获取插入的最后一条的id -- 多表查询有用


1 # 导入pymsql模块
2 import pymysql
3 # 建立一个叫conn的连接
4 conn = pymysql.connect(
5 host='127.0.0.1', #连接为本地地址
6 port=3306, #端口号,不写也默认为3306
7 user='root', #mysql用户名
8 password='000000', #mysql密码
9 database='darkfitch_for_test',#选择在musql中的一个数据库
10 charset='utf8' #编码设置为utf-8
11 )
12
13
14 cursor = conn.cursor()
15 sql = 'insert into authors(a_name) VALUES (%s)'
16 cursor.execute(sql,'van')
17 last_id = cursor.lastrowid #获取最后一条数据的id
18 print(last_id)
19
20 conn.commit()
21 cursor.close()
22 conn.close()
lastrowid
2.删除数据


1 # 导入pymsql模块
2 import pymysql
3 # 建立一个叫conn的连接
4 conn = pymysql.connect(
5 host='127.0.0.1', #连接为本地地址
6 port=3306, #端口号,不写也默认为3306
7 user='root', #mysql用户名
8 password='000000', #mysql密码
9 database='darkfitch_for_test',#选择在musql中的一个数据库
10 charset='utf8' #编码设置为utf-8
11 )
12
13 cursor = conn.cursor()
14 sql = "delete from authors where id = %s"
15 cursor.execute(sql,[9])
16 # cursor.execute(sql,9) #对于单个数据,而且值是int类型的可以直接写,不需要放在可迭代对象中
17 conn.commit()
18 cursor.close()
19 conn.close()
删除数据
3.修改数据


1 # 导入pymsql模块
2 import pymysql
3 # 建立一个叫conn的连接
4 conn = pymysql.connect(
5 host='127.0.0.1', #连接为本地地址
6 port=3306, #端口号,不写也默认为3306
7 user='root', #mysql用户名
8 password='000000', #mysql密码
9 database='darkfitch_for_test',#选择在musql中的一个数据库
10 charset='utf8' #编码设置为utf-8
11 )
12
13 cursor = conn.cursor()
14 sql = "update authors set a_name = %s where id = %s"
15 cursor.execute(sql,['test',3]) #这里传值需要用可迭代对象包裹,否则报错:TypeError:
16
17 #execute() takes from 2 to 3 positional arguments but 4 were given
18
19 conn.commit()
20 cursor.close()
21 conn.close()
修改数据
4.查询数据
4.1查询单条数据


1 # 导入pymsql模块
2 import pymysql
3 # 建立一个叫conn的连接
4 conn = pymysql.connect(
5 host='127.0.0.1', #连接为本地地址
6 port=3306, #端口号,不写也默认为3306
7 user='root', #mysql用户名
8 password='000000', #mysql密码
9 database='darkfitch_for_test',#选择在musql中的一个数据库
10 charset='utf8' #编码设置为utf-8
11 )
12 # 获取光标
13 cursor = conn.cursor()
14 sql = 'select * from authors where id <6;'
15 # 执行sql语句
16 cursor.execute(sql)
17 # 获取查询的单条数据
18 ret = cursor.fetchone()
19 cursor.close()
20 conn.close()
21 # 打印出结果
22 print(ret)
查询单条数据
4.2查询多条数据


1 # 导入pymsql模块
2 import pymysql
3 # 建立一个叫conn的连接
4 conn = pymysql.connect(
5 host='127.0.0.1', #连接为本地地址
6 port=3306, #端口号,不写也默认为3306
7 user='root', #mysql用户名
8 password='000000', #mysql密码
9 database='darkfitch_for_test',#选择在musql中的一个数据库
10 charset='utf8' #编码设置为utf-8
11 )
12 # 获取光标
13 cursor = conn.cursor()
14 sql = 'select * from authors where id <6;'
15 # 执行sql语句
16 cursor.execute(sql)
17 # 获取查询的多条数据
18 ret = cursor.fetchall()
19 cursor.close()
20 conn.close()
21 # 打印出结果
22 print(ret
查询多条数据
5.其他用法
5.1查询指定条数数据
1 # 可以获取指定数量的数据
2 cursor.fetchmany(3)
5.2光标一定到指定的绝对位置
1 # 光标按绝对位置移动1
2 cursor.scroll(1, mode="absolute")
5.3光标上下移动指定位置
1 # 光标按照相对位置(当前位置)移动1 向上移动为负值,向下移动为正值
2 cursor.scroll(1, mode="relative")
















