1

I have a list of absent_studentID_list

absent_studentID_list = ['sid000001','sid000006']

I want to update a column status inside student table which matches the studentID with absent_studentID_list

currently I'm looping over the list and using update

for sid in absent_studentID_list:
        u = update(studentTable).values({"status": "Absent"}).where(studentTable.c.studentid == str(sid))
        studentTableSession.execute(u)
        studentTableSession.commit()

Is there a better way to update than this?

2
  • 2
    How about u = studentTable.update().values(status="Absent").where(studentTable.c.studentid.in_(absent_studentID_list)) ? You should only need to execute it once instead of doing it in a loop. Commented Nov 28, 2019 at 11:25
  • @GordThompson can you post this answer so that i can accept your answer Commented Nov 28, 2019 at 13:32

1 Answer 1

3

You can use

u = studentTable.update()\
    .values(status="Absent")\
    .where(studentTable.c.studentid.in_(absent_studentID_list))

By using in_ you will only need to execute it once instead of doing it in a loop.

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.