MySql:将python列表插入表中 [英] MySql: Inserting python lists into a table

查看:58
本文介绍了MySql:将python列表插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将这两个列表插入同一张表的不同SQL列中

How do I insert these 2 lists into different SQL columns of the same table

a = [1,2,3]
b = [4,5,6]

这是将一个列表插入一列的方法

This is the way to insert one list into a column

query = "INSERT INTO tableName (col1) VALUES (%s)"
cursor.executemany(query, [(r,) for r in a])

我似乎无法弄清楚如何将两个列表都插入表中,我希望列表a位于一列中,列表b位于另一列中

I can't seem to figure out how to insert both the lists into the table, I want list a to be in one column and list b to be in other column

推荐答案

query = "INSERT INTO tableName (col1, col2) values (%s, %s)"
cursor.executemany(query, [(x, y) for x, y in zip(a, b)])

这篇关于MySql:将python列表插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆