Use psycopg2 to construct queries without connection

3.8k views Asked by At

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.

2

There are 2 answers

3
piro On BEST ANSWER

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.

1
chmullig On

It seems like it would be easier and more versatile to store the data to be inserted later in another structure. Perhaps a csv file. Then when you connect you can run through that table, but you can also easily do other things with that CSV if necessary.