简体   繁体   中英

Insert some multiple records to mysql db using python 3

I want to insert multiple records to a mysql db using python . I use 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.

   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

So you need to remove %S in your sql request. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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