I'm am working on editing a python script and I need to check if a table exists. The code works however since I am now trying to check if a table exists I need to first understand what is going on.
The code currently has:
import pg
con = pg.connect(...)
and a bunch of calls to:
con.query(...)
I found this example on SO: Checking if a postgresql table exists under python (and probably Psycopg2)
but I am unsure if psycopg2 is the same as pg and I can't seem to find any documentation on pg so I don't know if import pg can do con.exucute(...) simular to how psycopg2 can.
Everytime I search for pg I get documentation for psycopg2.
Anyone know the differences between the two?
Can I use con.execute() and con.cursor() for pg?
pgis most likely a reference to the PyGreSQL library, which is DB-API compliant, as is psycopg2, so they should operate the same--though ordinarily you would use theexecutemethod of a cursor object rather than of the connection object.cursorobject march with anexecuteand store the last value of the execute?execute()can be obtained through the cursor object withcursor.fetchone()orcursor.fetchall(). The complete DB-API specifications are here: python.org/dev/peps/pep-0249. Whichever library you use may have some additional features; you should check that documentation also.