简体   繁体   English

使用python 3将一些多条记录插入mysql db

[英]Insert some multiple records to mysql db using python 3

I want to insert multiple records to a mysql db using python . 我想使用python将多个记录插入到mysql db中。 I use mysql.connector . 我使用mysql.connector but I receive error. 但我收到错误。 when I try to insert a record without formatting it works but multiple line with formatting doesn't! 当我尝试插入不格式化的记录时,它可以工作,但格式化时多行却不行! I've used ? 我用过? instead of %s but I still receive this error. 而不是%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)

the error I receive is: 我收到的错误是:

Failed processing format-parameters; 处理格式参数失败; %s" % err) mysql.connector.errors.ProgrammingError: Failed processing format-parameters; 'tuple' object cannot be interpreted as an integer %s“%err)mysql.connector.errors.ProgrammingError:处理格式参数失败;'tuple'对象不能解释为整数

So you need to remove %S in your sql request. 因此,您需要在SQL请求中删除%S Because it causes this error : 因为它导致此错误:

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

So instead of VALUES(%S, %S, %s, %s) use VALUES(%s, %s, %s, %s) 因此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.

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