毫无意义的 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: Connection
with 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
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论