简体   繁体   中英

Python mysql.connector doesn`t return all rows

The table contains 21.756 rows . Python script returns only 20.715 rows (-1041) . Excluding any column from SELECT statements results in correct number of rows.

  • Python = 3.9.0
  • MySQL Percona 5.7.29

Query in 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 row(s) returned

Python script:

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

returns result = 20.715 rows

Fix that works

When I comment out or remove any column in query it results in correct number of rows returned. It does not depend which column is excluded.

query = '''
            SELECT
            id,
            title,
            -- auctionId,
            sellingMethod,
            auctionSubMethod,
            status,
            auction_date,
            lotId,
            description,
            amount
            FROM auctions
        '''

returns result = 21.576 rows

What I`ve tried with no result

  • cursor.fetchall()
  • cnx.cursor(buffered=False)
  • using limit and offset: executed SELECT 3 times, incrementing offset and limit by 10000. The results are the same (10.000+10.000+715 rows)

Updating mysql-connector-python module resolved the issue.

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