For some reason, I'm unable to insert data into multiple columns in mysql table. As soon as I insert into the first column (anyone column), I find it difficult to insert into other columns. I noticed that after inserting into the first column, in the second column, the insertion starts from the point where it ended in the first column, but on the second. I tried inserting on a third column but no insertion took place.
See my code below:
week1=xrange(1,101,1) #for column1
week2=xrange(1,101,1)#for column2
cur.executemany("INSERT INTO stud(classID) VALUES(%s)", [(x,) for x in week1])
db.commit()
Thanks in advance for your suggestions.
You have to produce lists with two columns; if your two columns are coming from two separate iterables, use zip()
to join them:
week1 = xrange(1, 101) #for column1
week2 = xrange(1, 101) #for column2
cur.executemany("INSERT INTO stud (classID, othercolumn) VALUES(%s, %s)", zip(week1, week2))
db.commit()
zip(week1, week2)
produces a sequence of tuples; each a pair of values taken from both input sequences: [(week1[0], week2[0]), (week1[1], week2[1]), ...]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.