How to (really) fetch one row at a time with pymysql
I wasted some time today with python to find out how to create an unbuffered cursor for pymysql, which i use to connect to MySQL dbs instead of MySQLdb.
By default pymysql fetch all the rows of your SELECTs, even if you use fetchone instead of fetchall, because by default when you create a pymysql cursor with connection.cursor() the cursor is a buffered
The unbuffered cursor is really useful, and sometimes necessary, if you need to get a large amount of data. In order to do so:
conn = pymysql.connect(host= "localhost",user="youruser",passwd="xxxxx",db="mydb") # get for example every row query='SELECT * FROM names' # create the unbuffered cursor with pymysql.cursors.SSCursor class x = conn.cursor(pymysql.cursors.SSCursor) # execute your query x.execute(query) # get one row at a time row = x.fetchone() while row is not None: #...do your dirty things with the row row = x.fetchone()