Use psycopg2 to construct queries without connection

我们两清 提交于 2019-12-10 13:17:40

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4879804/use-psycopg2-to-construct-queries-without-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!