Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use psycopg2 to construct queries without connection

I have several occasions where I want to collect data when in the field. This is in situations where I do not always have access to my postgres database.

To keep things in sync, it would be excellent if I could use psycopg2 functions offline to generate queries that can be held back and once I am able to connect to the database; process everything that is held back.

One thing I am currently struggling with is that the psycopg2 cursor requires a connection to be constructed.

My question is:

Is there a way to use a cursor to do things like mogrify without an active connection object? Or with a connection object that is not connected to a database? I would then like to write the mogrify results temporarily to file so they can be processed later.

like image 642
milovanderlinden Avatar asked Feb 02 '11 21:02

milovanderlinden


People also ask

Is psycopg2 asynchronous?

Asynchronous notificationsPsycopg allows asynchronous interaction with other database sessions using the facilities offered by PostgreSQL commands LISTEN and NOTIFY.

Does psycopg2 need PostgreSQL?

The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements.


1 Answers

It would be a fragile approach, as the connection is used to detect some parameters used for escaping (encoding, standard-conforming string etc).

You can have a "manual mogrify" calling psycopg2.extensions.adapt(x).getquoted() on your parameters and then merging them to the query using the regular Python % operator.

Currently released psycopg versions (up to 2.3.2) may fail on None -> NULL conversion though. You can either convert these values manually or register an adapter for None: you can see how in this commit.

like image 101
piro Avatar answered Nov 06 '22 03:11

piro