简体   繁体   English

使用pymysql(Python)在MySQL中导入多个列表

[英]Import multiple lists in MySQL using pymysql (Python)

I am trying to insert multiple lists at once in MySQL using pymysql library. 我试图使用pymysql库在MySQL中一次插入多个列表。 I have tried the following: 我尝试了以下方法:

import pymysql

db = pymysql.connect(host="ipaddress", user="me", passwd="password", db="test") 
cursor=db.cursor()

list1=[3.1, 4.5, 8.3]
list2=[1.2, 3.4, 5.2]
list3=[2.4, 6.5, 6.9]

cursor.executemany("insert into table (var1,var2,var3) values (%f, %f, %f)", [list1,list2,list3])
db.commit()
db.close()

But I get error. 但是我得到了错误。 What I am doing wrong?. 我做错了什么? How should I be doing it? 我应该怎么做?

I have found out where the problem was. 我发现了问题所在。 The solution is the following: 解决方案如下:

cursor.executemany("insert into table (var1,var2,var3) values (%s, %s, %s)", [list1,list2,list3])

Instead of values (%f, %f, %f) it should be values (%s, %s, %s). 代替值(%f,%f,%f),它应该是值(%s,%s,%s)。 I have tested it and it works. 我已经对其进行了测试,并且可以正常工作。 Unfortunately cannot explain why the query should be written in this way as I am working with numeric lists and not strings. 不幸的是,当我使用数字列表而不是字符串时,无法解释为什么应该以这种方式编写查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM