1

i have 3 list

list1=[3, 4, 5, 6, 9]
list2=[1, 10, 5, 2, 1]
list3=[5, 53, 26, 11, 5]

how to insert this data into mysql database in column wise. each list should be stored in a column.I knew to store it row wise using cursor.executemany(sqlquery,lists). Please help me.

2 Answers 2

2

You can do this by using pandas:

Convert the lists into a Pandas dataframe:

In [1644]: df = pd.DataFrame({'list1':list1,'list2':list2,'list3':list3})

In [1645]: df
Out[1645]: 
   list1  list2  list3
0      3      1      5
1      4     10     53
2      5      5     26
3      6      2     11
4      9      1      5

Use to_sql method to write into mysql table:

df.to_sql(con=con, name='mytable', if_exists='replace', flavor='mysql')
Sign up to request clarification or add additional context in comments.

Comments

1

If you want only using cursor.executemany. So you can transform three lists into one by combining data from the same position.

list1=[3, 4, 5, 6, 9]
list2=[1, 10, 5, 2, 1]
list3=[5, 53, 26, 11, 5]
print(list(zip(list1,list2,list3)))
# [(3, 1, 5), (4, 10, 53), (5, 5, 26), (6, 2, 11), (9, 1, 5)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.