Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of `cursor` class in psycopg?

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

  1. 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):
    
  2. pass both connection and cursor - makes function signatures and implementation needlessly complicated

    def process_log_file(self, connection, cursor):
    
  3. pass only cursor as parameter and use mycursor.connection.commit() for commiting

    def process_log_file(self, cursor):
    
like image 472
geekQ Avatar asked Apr 30 '14 13:04

geekQ


1 Answers

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".

like image 112
fog Avatar answered Sep 28 '22 03:09

fog