简体   繁体   中英

Python MySQL connector fails to return all results from SELECT query

I have this query which I execute using the mysql-connector-python. The code is:

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()

This returns:

(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

However, if I run the exact same query through MySQL workbench, the results returned are: MySQL Workbench结果

I have no idea why the two queries are returning different results. I've also looked at the network traffic information using wireshark below, but there's no clear reason to me why this is so.

MySQL Workbench

Python Connector

You have to print the row in prior to fetch the next row like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM