I would like to modify some data with INSERTs and UPDATEs. From the psycopg tutorials it looks like I need
cur = connection.cursor()
cur.execute(my_insert_statement)
connection.commit()
Psycopg's cursor class seems to have little to do with the cursors, as defined by postgres.
If I modularize my script, creating a connection in the main module and some worker functions (no threading, just for modularization) should I
pass the connection parameter to the functions and recreate cursor every time. Is there significant overhead creating new cursor object frequently?
def process_log_file(self, connection):
pass both connection and cursor - makes function signatures and implementation needlessly complicated
def process_log_file(self, connection, cursor):
pass only cursor as parameter and use mycursor.connection.commit() for commiting
def process_log_file(self, cursor):
Any of the three will work (it is mainly a matter of personal taste) but I better like (1). Here is why:
The cursor type is light-weight and just creating it doesn't do anything special apart from creating a new Python object. You're welcome to create, use (commit/rollback) and destroy as many cursor as you like, especially if that helps you keep the code clean and organized.
Also, cursors are important when you're working with complex logic that need access to data coming from multiple, different queries: in this case the cursor acts as a holder/iterator for your data.
In the end, passing around the connection (your real handle to the backend) and keeping the cursors local to the specific function/method just "feels right".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With