About mysql cursor and iterator

后端 未结 1 756
猫巷女王i
猫巷女王i 2020-12-28 16:42

Imagine I have a mysql cursor and data read. The amount of data might be very big that I want to deal with one line each time.

An easy and straight forward way might

相关标签:
1条回答
  • 2020-12-28 17:06

    The MySQLdb cursor class implements the iterator protocol, so you can simply do this:

    cursor.execute(sql)
    for row in cursor:
        print row
        ...
    

    Relevant code from MySQLdb.cursors.BaseCursor:

    def __iter__(self):
        return iter(self.fetchone, None)
    
    0 讨论(0)
提交回复
热议问题