0

I'm trying to get the PostgreSQL database connection in the python flask app and I would like to get it return the rows so I can have a name-based access to columns. I've tried to do it in accordance with Psycopg2 documentation, but wasn't successful.

Here's the code:

def get_connection():        
    conn = psycopg2.connect(dbname=DB_USER, user=DB_USER, password=DB_PASS, host=DB_HOST)
    cur = conn.cursor(cursor_factory=psycopg2.extras.DictRow)
    return cur

According to the documentation it seems fine but I'm getting an error:

File "/app/app.py", line 34, in get_connection
cur = conn.cursor(cursor_factory=psycopg2.extras.DictRow)
TypeError: __init__() takes 2 positional arguments but 3 were given

Can somebody please help me out with this?

Thank you.

0

1 Answer 1

1

The docs you link to don't show DictRow being used in this way at all. Maybe you meant to use DictCursor, which the docs do show being used in this way.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't want to argue, but "class psycopg2.extras.DictRow(cursor) - A row object that allow by-column-name access to data." sounded to me like the thing, that I wanted. However your suggestion was right and helped me, thank you.

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.