How to get a row-by-row MySQL ResultSet in python

后端 未结 5 784
情话喂你
情话喂你 2020-12-04 10:07

MySQL ResultSets are by default retrieved completely from the server before any work can be done. In cases of huge result sets this becomes unusable. I would like instead to

5条回答
  •  醉话见心
    2020-12-04 11:00

    I found the best results mixing a bit from some of the other answers.

    This included setting cursorclass=MySQLdb.cursors.SSDictCursor (for MySQLdb) or pymysql.cursors.SSDictCursor (for PyMySQL) as part of the connection settings. This will let the server hold the query/results (the "SS" stands for server side as opposed to the default cursor which brings the results client side) and build a dictionary out of each row (e.g. {'id': 1, 'name': 'Cookie Monster'}).

    Then to loop through the rows, there was an infinite loop in both Python 2.7 and 3.4 caused by while rows is not None because even when cur.fetchmany(size=10000) was called and there were no results left, the method returned an empty list ([]) instead of None.

    Actual example:

    query = """SELECT * FROM my_table"""
    conn = pymysql.connect(host=MYSQL_CREDENTIALS['host'], user=MYSQL_CREDENTIALS['user'],
                              passwd=MYSQL_CREDENTIALS['passwd'], charset='utf8', cursorclass = pymysql.cursors.SSDictCursor)
    cur = conn.cursor()
    results = cur.execute(query)
    rows = cur.fetchmany(size=100)
    while rows:
        for row in rows: 
            process(row)
        rows = cur.fetchmany(size=100)
    cur.close()
    conn.close()
    

提交回复
热议问题