gpt4 book ai didi

python - 使用 python mysqldb 以编程方式偏移/限制

转载 作者:行者123 更新时间:2023-11-29 02:36:28 26 4
gpt4 key购买 nike

我正在尝试处理来自数据库表的数据。同时加载它们会占用大部分计算机资源。这是我当前的代码

cursor.execute("SELECT * from sometable")
rs = cursor.fetchall()
.
# process 1: set operation
.
for a in rs:
# process 2: put data in another db

有没有办法减少资源使用?就像在循环中一点一点地获取行?

最佳答案

您可以使用 cursor.fetchone()。了解一下 here .用法示例:

cursor.execute(query)
numrows = int(cursor.rowcount)
for x in range(0,numrows):
row = cursor.fetchone()

也可以考虑在mysql查询中使用LIMIT:

cursor.execute("SELECT * from sometable LIMIT 0, 100")

最后,避免使用星号运算符,只选择您需要的列。

关于python - 使用 python mysqldb 以编程方式偏移/限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4364129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com