Python MySQL连接器无法返回SELECT查询的所有结果

时间:2015-02-02 03:55:28

标签: python mysql sql connector mysql-connector-python

我有这个查询,我使用mysql-connector-python执行。代码是:

try:
    conn = mycon.connect(user=****,password=****,host=****,database=****,autocommit=True)
except mycon.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Authentication error - incorrect username and/or password.")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist.")
    else:
        print(err)
cursor = conn.cursor()
no_of_results = cursor.execute("SELECT * FROM name_table\nLIMIT 0, 1000\n")
row = cursor.fetchone()
print(row)
while row is not None:
    row = cursor.fetchone()
    print(row)
cursor.close()
conn.close()

返回:

(1, 'Mains', 'Mains electrical circuit.')
(2, 'Solar', 'Solar panels.')
(3, 'AirCon', 'Air conditioner.')
(4, 'Oven', 'Oven.')
(5, 'Power1', 'General power circuit 1.')
(6, 'Power2', 'General power circuit 2.')
(7, 'Lights1', 'Lights circuit 1.')
(8, 'Lights2', 'Lights circuit 2.')
None

但是,如果我通过My​​SQL工作台运行完全相同的查询,则返回的结果为:MySQL Workbench Results

我不知道为什么这两个查询会返回不同的结果。我还使用下面的wireshark查看了网络流量信息,但我没有明确理由说明原因。

MySQL Workbench

Python Connector

1 个答案:

答案 0 :(得分:0)

你必须在获取下一行之前打印行,如下所示:

while row is not None:
    print(row)
    row = cursor.fetchone()