毫无意义的 fetchone——pymysql 中的 fetchone 有什么意义吗()
【毫无意义的 fetchone——pymysql 中的 fetchone 有什么意义吗()】毫无意义的 fetchone
import pymysql.cursors
from pymysql.connections import Connection
from pymysql.cursors import Cursor
import time# Connect to the database
connection = pymysql.connect(host='192.168.31.203',
user='root',
password='yjcyjc',
database='d_replication_db')with connection:
connection: Connectionwith connection.cursor() as cursor:
cursor:Cursor
# Create a new record
sql = "select * from master_slave_tweet;
"
cursor.execute(sql)
for _ in range(10):
print(cursor.fetchone())
fetchone
不是一次从 mysql server 中取一次,而是在你执行cursor.execute(sql)
的时候就把所有的数据取回来了,所以每调用fetchone
一次,就发起一个网络请求(根本不会发起网络请求)fetchone
会节约网络请求时间吗?不会,原因如上fetchone
会节约内存空间吗?不会,原因如上- 那
fetchone
的意义是什么呢?毫无意义,他也不能帮我们写出更优雅的方法。
select * from master_slave_tweet limit 1;
而不是 select * from master_slave_tweet;
之后再使用fetchone
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量