1

I have a insert sql:

cur.execute(f"""INSERT INTO patients_patient (patient_id, patient_name) VALUES
                      (1,'Jane'),(2,'John')""")

I tired use parameters in query and pass the values separately:

cur.execute(f"""INSERT INTO patients_patient (patient_id, patient_name) VALUES
                      (%s,%s),(%s,%s)""",(1,'Jane'),(2,'John'))

Error:

cur.execute(f"""INSERT INTO patients_patient (patient_id, patient_name) VALUES
TypeError: function takes at most 2 arguments (3 given)

any friend can help?

1 Answer 1

2

Try using executemany() here:

sql = 'INSERT INTO patients_patient (patient_id, patient_name) VALUES (?, ?)'
values = [(1, 'Jane'), (2, 'John')]
cur.executemany(sql, values)

If you don't know a priori how many tuples to expect in the VALUES clause, then you may build it dynamically:

sql = 'INSERT INTO patients_patient (patient_id, patient_name) VALUES '
values = [(1, 'Jane'), (2, 'John')]
sql += '(%s' + ',%s'*(len(values)-1) + ')'
cur.executemany(sql, values)
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Tim,thanks,I have another one: cur.execute(f"""INSERT INTO patients_event_type (type_name) VALUES ('HR'),('RR')""") it not works,can you help me?
Well for starters why are you using execute() instead of executeMany() ?
The sql I post here works,but I copyed your last code ,it not work for this one.I want to change this one is because I want to use parameters.
Hi friend also help with this related one ? stackoverflow.com/questions/69952301/…

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.