Skip to content Skip to sidebar Skip to footer

Insert Into Table Using For In Range And Keys Of The Value

I have a query (sql1) that populates data, and I am trying to insert the outcome of this data (sql1) as well as other inputs into same table. Here is first query (sql1). sql1 = ' S

Solution 1:

the second part is correct but instead of put this ( %s, %s, %s, %s, "UniqueCourseName", CURDATE() ) use this ( %s, %s, %s, %s, %s, %s ) and:

cursor.execute(sql2, (all[i]['Key1'], all[i]['Key2'], 
                       all[i]['Key3'], all[i]['Key4']),"UniqueCourseName", CURDATE())

if you print(all[0]), you will se something like

'Creator_Id':'value1''Record_Id,':'value2'' Course_Num':'value3'' SiteCode':'value3'' coursename ':'value4'' datestamp':value5

and 'Key' make reference to creator_id, because is a dictionary.

if return a tuple then:

cursor.execute(sql2, (i[0], i[1], 
                       i[2], i[3]),"UniqueCourseName", CURDATE())

Solution 2:

You can just do something like this:

for creator_id, record_id, course_num, site_code in all:
    cursor.execute(sql2, (creator_id, record_id, course_num, site_code))

fetchall() should return a list of tuples. You can unpack the tuples in the for loop to make the code easier to read.

Also, it looks like you're missing an opening ( after Insert into Courses

Post a Comment for "Insert Into Table Using For In Range And Keys Of The Value"