【发布时间】:2021-07-10 10:54:08
【问题描述】:
该表包含 21.756 行。 Python 脚本仅返回 20.715 行 (-1041)。从 SELECT 语句中排除 任何 列会导致正确的行数。
- Python = 3.9.0
- MySQL Percona 5.7.29
在 MySQL Workbench 中查询
SELECT
id, -- int
title, -- varchar(1000)
auctionId, -- varchar(50)
sellingMethod, -- varchar(100)
auctionSubMethod, -- varchar(20)
status, -- varchar(50)
auction_date, -- varchar(50)
lotId, -- varchar(100)
description, -- text
amount -- decimal(30,2)
FROM auctions
返回 21576 行
Python 脚本:
result = []
try:
cnx = mysql.connector.connect(user=DB_USER,password=DB_PWD,host=DB_HOST,database=DB_NAME)
cursor = cnx.cursor(buffered=True)
query = '''
SELECT
id,
title,
auctionId,
sellingMethod,
auctionSubMethod,
status,
auction_date,
lotId,
description,
amount
FROM auctions
'''
cursor.execute(query)
rows = cursor.rowcount
row = cursor.fetchone()
while row is not None:
result.append(row)
row = cursor.fetchone()
except mysql.connector.Error as err:
if err.errno == mysql.connector.errorcode.ER_ACCESS_DENIED_ERROR:
print("ERROR: Something is wrong with your user name or password")
exit(1)
elif err.errno == mysql.connector.errorcode.ER_BAD_DB_ERROR:
print("ERROR: Database {db_name} does not exist")
exit(1)
else:
print(err)
exit(1)
finally:
cursor.close()
cnx.close()
return result
返回结果 = 20.715 行
修复有效
当我在查询中注释掉或删除 any 列时,它会返回正确的行数。这与排除哪一列无关。
query = '''
SELECT
id,
title,
-- auctionId,
sellingMethod,
auctionSubMethod,
status,
auction_date,
lotId,
description,
amount
FROM auctions
'''
返回结果 = 21.576 行
我试过没有结果
- cursor.fetchall()
- cnx.cursor(buffered=False)
- 使用limit和offset:执行SELECT 3次,将offset和limit递增10000,结果相同(10.000+10.000+715行)
【问题讨论】:
-
您使用
while循环而不是遍历光标对象是否有特定原因?还有你使用的是什么版本的连接器? -
这是试图解决问题的结果。以为问题出在内存分配上。