[英]Insert some multiple records to mysql db using python 3
我想使用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)
我收到的错误是:
处理格式参数失败; %s“%err)mysql.connector.errors.ProgrammingError:处理格式参数失败;'tuple'对象不能解释为整数
因此,您需要在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)
代替VALUES(%S, %S, %s, %s)
VALUES(%s, %s, %s, %s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.