Retrieving Data from MySQL in batches via Python

后端 未结 4 2005
日久生厌
日久生厌 2021-01-31 11:11

I would like to make this process in batches, because of the volume.

Here\'s my code:

 getconn = conexiones()
 con = getconn.mysqlDWconnect()
 with con:         


        
4条回答
  •  情歌与酒
    2021-01-31 11:52

    Thank you, here's how I implement it with your suggestions:

    control = True
    index = 0
    while control==True:
       getconn = conexiones()
       con = getconn.mysqlDWconnect()
       with con:
            cur = con.cursor(mdb.cursors.DictCursor)
            query = "SELECT id, date, product_id, sales FROM sales  limit 10 OFFSET " + str(10 * (index))
            cur.execute(query)
            rows = cur.fetchall()
            index = index+1        
            if len(rows)== 0:
                control=False
       for row in rows:
            dataset.append(row)
    

提交回复
热议问题