Python开发【笔记】:为什么pymysql重连后才能查到新添加的数据
2017-09-30 09:21
459 查看
PyMysql操控
问题描述:之前做数据库模块的时候用到了pymysql,测试中发现了一个问题,创建两个程序,select.py从数据库中不断的读取,insert.py在数据库中插入多条数据,但是select.py程序查不到新添加的数据,像是做了缓存一样,查到的数据永远不变;只有重启模块,再次建立连接后,新添加的数据才能被查到;还原当时的代码如下:
查询:
# select.py 不断的进行查询 import pymysql import time # Connect to the database connection = pymysql.connect(host='192.168.1.134', port=3306, user='remote', password='tx_1234abc', db='Jefrey', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor, ) while True: with connection.cursor() as cursor: # Create a new record sql = " select * from users WHERE email=%s" # sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" rows_count = cursor.execute(sql, ('webmaster@python.org')) result = cursor.fetchall() print(result) time.sleep(1)
插入:
# insert.py 进行数据插入 import pymysql.cursors import time # Connect to the database connection = pymysql.connect(host='192.168.1.134', port=3306, user='remote', password='tx_1234abc', db='Jefrey', charset='utf8mb4', # autocommit=True, cursorclass=pymysql.cursors.DictCursor,) try: # execute方法 with connection.cursor() as cursor: # Create a new record start = time.time() sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" for i in range(5): cursor.execute(sql,('webmaster@python.org','test')) connection.commit() time.sleep(1) finally: connection.close()
运行30秒,查询输出打印:
() () () () ()
此时数据表中的数据确实是插入进去了:
#首先修改隔离界别 set tx_isolation='serializable'; select @@tx_isolation; +----------------+ | @@tx_isolation | +----------------+ | SERIALIZABLE | +----------------+ #事务A:开启一个新事务 start transaction; #事务B:在A没有commit之前,这个交叉事务是不能更改数据的 start transaction; insert tx values('4','4'); ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction update tx set num=10 where id=1; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
SERIALIZABLE
注:
安全性考虑:Serializable>Repeatable read>Read committed>Read uncommitted
数据库效率:Read uncommitted>Read committed>Repeatable read>Serializable
一般情况下,我们会使用Repeatable read、Read committed mysql数据库默认的数据库隔离级别Repeatable read
相关文章推荐
- 为什么说Python 是大数据全栈式开发语言
- Python ORM框架SQLAlchemy学习笔记之数据添加和事务回滚介绍
- 我为什么说 Python 是大数据全栈式开发语言
- Python ORM框架SQLAlchemy学习笔记之数据添加和事务回滚介绍
- 我为什么说 Python 是大数据全栈式开发语言
- 开发笔记:Python中的Module
- Asp.NET开发笔记--文本框的数据绑定与操作
- python 宝典 笔记 第十二章 存储数据和对象 (各种对象转换成字符串)
- ASP.NET控件开发学习笔记--第4回 为控件添加属性
- 三维软件开发笔记---调错DataGrid控件数据绑定无法显示问题
- Python Web 开发 学习笔记1 安装配置 python+django
- WINCE应用开发笔记三--COM、数据类型、c++编程的一些感觉
- Asp.net控件开发学习笔记(六)----数据回传
- 本人开发系统--新生报到与数据统计管理系统--数据库设计及相关查询SQL语句笔记
- TB级数据检索引擎开发笔记(一)
- extjs学习笔记(六) grid中数据的保存,添加和删除
- .net开发笔记之十二:在Datagrid控件中添加统计(合计)行
- .net开发笔记之十一:取数据到Datagrid的一般步骤
- 三维软件开发笔记---数据填充及色标完成
- Java开发学习笔记之三:HTTP客户请求数据格式