0

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

4
  • Try to make all "s" lowercases. VALUES(%s, %s, %s, %s) Commented Jul 11, 2019 at 7:47
  • they changed when I was trying different ways to fix error. I changed them to lowercase but error still exists. Commented Jul 11, 2019 at 9:11
  • Ok but %S is a mistake. It must be in lowercase. print("(%s, %S)"%(1, 2)) ValueError: unsupported format character 'S' (0x53) at index 6 Commented Jul 11, 2019 at 9:21
  • you are right. thank you. Commented Jul 11, 2019 at 9:38

1 Answer 1

0

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.