0

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?

  • 1
    `pg` is 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 the `execute` method of a cursor object rather than of the connection object. – rd_nielsen Aug 03 '17 at 20:11
  • @rd_nielsen Does a `cursor` object march with an `execute` and store the last value of the execute? – Error - Syntactical Remorse Aug 03 '17 at 20:15
  • 1
    The results of `execute()` can be obtained through the cursor object with `cursor.fetchone()` or `cursor.fetchall()`. The complete DB-API specifications are here: https://www.python.org/dev/peps/pep-0249/. Whichever library you use may have some additional features; you should check that documentation also. – rd_nielsen Aug 03 '17 at 20:18

1 Answers1

2

pg refers to the PyGreSQL driver:

http://www.pygresql.org/

PyGreSQL is a Python module that interfaces to a PostgreSQL database. It embeds the PostgreSQL query library to allow easy use of the powerful PostgreSQL features from a Python script or application.

PyGreSQL consists of two parts: the “classic” PyGreSQL interface provided by the pg module and the newer DB-API 2.0 compliant interface provided by the pgdb module.

Community
  • 1
  • 1
Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260