Answer the question
In order to leave comments, you need to log in
Do I need to put a commit in the with construct?
Python 3.7.3
PostgreSQL 10
import psycopg2
class User:
def __init__(self, database):
self.conn = psycopg2.connect(database)
self.cursor = self.conn.cursor()
def set_address(self, address, user_id):
with self.conn:
self.cursor.execute('UPDATE users SET address=%s WHERE user_id=%s', (user_id, address))
self.conn.commit()
Answer the question
In order to leave comments, you need to log in
The with clause seems to imply closing the database connection.
Cursors can be used as context managers: leaving the context will close the cursor.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question