Ro_nair Ro_nair - 5 months ago 17
MySQL Question

Python zip for loop only executing once instead of 60 times

I have many python lists, from which I am transferring the data to the MySQL database.
Technically, each list variable has 60 values each. But at the time of executing the for loop, only 1 record gets updated instead of 60. I am using python zip form a long time and never faced a problem.

flg=1
prodcat="sac"

for a,b,c,d,e,f,g,h,o,p,q,r,s,t,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as1,at1,au in zip(lipgurl,lipdate,lides,liref,liname,licontry,listype,liprice,licurcy,lim,licolor,licond,lisize,liwidth,liheight,lidepth,liwish,limsg,liselracc,librand,liuoid,lifolby,lifolwing,lilikes,lisolddt,libuyer,liusprice,lipbought,libuyernm,libuyacc,libuycont,libuytype,liselrid,lisrcpage):
myid="VC"+str(flg)
cur.execute("INSERT INTO details VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(myid,a,au,ag,rdate,b,prodcat,c,d,e,at1,f,g,h,o,an,p,q,r,s,t,ab,ac,ad,ae,af,ai,aj,ak,al,am,ao,ap,aq,ar,as1))
flg=flg+1
print("Database updated")
con.commit()


In order to check whether each list variable has 60 values within it, I insterted a loop inside the above loop and printed. It prints out all 60 values.

Where am I going wrong? any suggestions?

Answer

Most likely you have list with only one element in it, so zip returns only one tuple of items, using izip_longest instead of zip should fix your issue ( you would get padded zip of items...):

from itertools import izip_longest


flg=1
prodcat="sac"

for a,b,c,d,e,f,g,h,o,p,q,r,s,t,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as1,at1,au\
        in izip_longest(lipgurl,lipdate,lides,liref,liname,licontry,listype,liprice,licurcy,lim,
                        licolor,licond,lisize,liwidth,liheight,lidepth,liwish,limsg,liselracc,librand,
                        liuoid,lifolby,lifolwing,lilikes,lisolddt,libuyer,liusprice,lipbought,libuyernm,
                        libuyacc,libuycont,libuytype,liselrid,lisrcpage):
    myid="VC"+str(flg)
    cur.execute("INSERT INTO details VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(myid,a,au,ag,rdate,b,prodcat,c,d,e,at1,f,g,h,o,an,p,q,r,s,t,ab,ac,ad,ae,af,ai,aj,ak,al,am,ao,ap,aq,ar,as1))
    flg=flg+1
    print("Database updated")
con.commit()