gpt4 book ai didi

python - 使用 python 3 将一些多条记录插入 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 15:46:23 26 4
gpt4 key购买 nike

我想使用python将多条记录插入到mysql db。我使用 mysql.connector 。但我收到错误。当我尝试插入一条不带格式的记录时,它可以工作,但带格式的多行却不行!我使用了 ? 而不是 %s,但我仍然收到此错误。

   my_nodes = []
myconnection = mysql.connector.connect(
host='127.0.0.1', user='root', passwd='1234', db='job_graph1')
mycursor=myconnection.cursor()
for nodes in range(1, 33):
weight = random.randint(0, 100)
my_record = (nodes, weight, False, False)
my_nodes.append(my_record)

sqlqu = "INSERT INTO t_node(n_id,n_weight,is_entry,is_exit) VALUES(%S, %S, %s, %s)"
mycursor.executemany(sqlqu, my_nodes)

我收到的错误是:

Failed processing format-parameters; %s" % err) mysql.connector.errors.ProgrammingError: Failed processing format-parameters; 'tuple' object cannot be interpreted as an integer

最佳答案

因此您需要删除 sql 请求中的 %S 。因为它会导致此错误:

print("(%s, %S)"%(1, 2)) 
ValueError: unsupported format character 'S' (0x53) at index 6

因此,不要使用 VALUES(%S, %S, %s, %s),而是使用 VALUES(%s, %s, %s, %s)

关于python - 使用 python 3 将一些多条记录插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56982997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com