Retrieving Data from MySQL in batches via Python

后端 未结 4 2016
日久生厌
日久生厌 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:54

    To expand on akalikin's answer, you can use a stepped iteration to split the query into chunks, and then use LIMIT and OFFSET to execute the query.

    cur = con.cursor(mdb.cursors.DictCursor)
    cur.execute("SELECT COUNT(*) FROM sales")
    
    for i in range(0,cur.fetchall(),5):
        cur2 = con.cursor(mdb.cursors.DictCursor)
        cur2.execute("SELECT id, date, product_id, sales FROM sales LIMIT %s OFFSET %s" %(5,i))
        rows = cur2.fetchall()
        print rows
    

提交回复
热议问题