总目录:https://blog.csdn.net/qq_41106844/article/details/105553392
Python-后端 - 子目录:https://blog.csdn.net/qq_41106844/article/details/105553324
官方的github地址:https://github.com/PyMySQL/PyMySQL
pymsql是Python中操作MySQL的模块,其使用方法和py2的MySQLdb几乎相同。
安装
python -m pip install pymysql
基础操作
pymysql的使用首先要建立连接,建立游标,之后才可以对数据库进行操作。
我们先看一个简单的查询例子。
首先建立一个数据库。
![20155953-c5fca6ed17d38b98.png](https://i-blog.csdnimg.cn/blog_migrate/f0ad732fe99e1aafa374d00a818d3da9.png)
建表
#导包
import pymysql
#数据库连接
# conn = pymysql.connect(host='IP地址',port=3306,user='用户名',passwd='密码',db='数据库名',charset='编码')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd=' ', db='sql_py', charset='utf8')
#建立游标
#游标相当于一个指针,在表中游走指向我们进行操作的行。
cursor = conn.cursor()
sql="""
CREATE TABLE mess(
id INT(10) primary key,
name VARCHAR(255),
age INT(10),
dep VARCHAR(255)
);
"""
#执行语句
cursor.execute(sql)
#关闭游标,关闭数据库连接
cursor.close()
conn.close()
![20155953-44222e1ea9ab5500.png](https://i-blog.csdnimg.cn/blog_migrate/013a073e0fdc697ecf2249c93547c592.png)
之后数据库里面就建立了这个表
![20155953-4d29f0747717b3bc.png](https://i-blog.csdnimg.cn/blog_migrate/68c65bdb86d5c91e3f9f449d7fc0683b.png)
现在我们知道了pymysql的使用流程:
导包
建立数据库连接
建立游标
执行sql语句
关闭游标
关闭数据库连接
之后我们插入一些数据:
sql="""
INSERT INTO mess(id,name,age,dep) VALUES
(1,"张三",30,"技术部"),
(2,"李四",25,"业务部"),
(3,"王五",35,"人事部"),
(4,"牛六",32,"技术部");
"""
#执行语句
cursor.execute(sql)
#提交修改
conn.commit()
最后的提交修改一定要有,否则数据不会写入表中。
![20155953-6145a89a12cc8e41.png](https://i-blog.csdnimg.cn/blog_migrate/3af53edb777826f9717f3d58c2058679.png)
![20155953-b2b7619495645424.png](https://i-blog.csdnimg.cn/blog_migrate/dffb63781a4ee9240290be24dbdd586a.png)
接着我们来查询一下:
#查询数据
cursor.execute("select * from mess")
#fetchall从游标中获取数据并转换为一个元组
mess_li=cursor.fetchall()
for iin mess_li:
print(i)
# (1, '张三', 30, '技术部')
# (2, '李四', 25, '业务部')
# (3, '王五', 35, '人事部')
# (4, '牛六', 32, '技术部')
![20155953-8a0eaf23345a95b7.png](https://i-blog.csdnimg.cn/blog_migrate/d21894654d939746120bf1a4bf83991f.png)
还有游标操作
fetchone()和fetchmany()
#查询数据
cursor.execute("select * from mess")
#fetchone
ret1 = cursor.fetchone()# 取一条
print(ret1)
#(1, '张三', 30, '技术部')
#fetchmany
ret2 = cursor.fetchmany(2)# 取三条
print(ret2)
#((2, '李四', 25, '业务部'), (3, '王五', 35, '人事部'))
![20155953-c26e917102bccb74.png](https://i-blog.csdnimg.cn/blog_migrate/8a9183fb5d3e1662c4b7d26a392cac48.png)
scroll()
#建立游标
#游标相当于一个指针,在表中游走指向我们进行操作的行。
cursor = conn.cursor()
#查询数据
cursor.execute("select * from mess")
#scroll
ret1 = cursor.fetchone()# 取一条
print(ret1)
#(1, '张三', 30, '技术部')
# 光标按绝对位置向后移动3行(默认处在第一行,向后移动三行,不算第一行。 2,3,4 取第四行)
cursor.scroll(3, mode="absolute")
ret2 = cursor.fetchone()# 取一条
print(ret2)
#(4, '牛六', 32, '技术部')
# 光标按照相对位置向前移动2行(当前位置为4,先前移动两行,算第四行。 4,3 取第三行)
cursor.scroll(-2, mode="relative")
ret3 = cursor.fetchone()# 取一条
print(ret3)
#(3, '王五', 35, '人事部')
![20155953-a513cb69dc27efa9.png](https://i-blog.csdnimg.cn/blog_migrate/489291c1f3163a5433c13ada909b338a.png)
事务
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd=' ', db='csdata')
cursor = conn.cursor()
try:
insertSQL1="UPDATE acc set balance=balance-3000 WHERE name='张三'"
insertSQL2="UPDATE acc set balance=balance+3000 WHERE name='李四'"
cursor = conn.cursor()
cursor.execute(insertSQL1)
raise Exception #在这里抛出错误 让交易中断
cursor.execute(insertSQL2)
cursor.close()
conn.commit()
except Exception as e:
#收到错误,回滚,结束事务。
conn.rollback()
conn.commit()
cursor.close()
conn.close()
![20155953-56d969f474a48fcf.png](https://i-blog.csdnimg.cn/blog_migrate/4c9df0e7d50c79114e3d1fcee9cf9426.png)