Search code examples
pythonpsycopg

How do I know if I have successfully created a table (Python, Psycopg2)?


I've looked at the documentations but haven't found anything that lets me know if the last command i've execute via cursor.execute("...") is successful.

I'm expecting a reply like "1 row affected."


Solution

  • I'd expect some kind of exception to be risen.
    If everything went ok – the error code is 00000 and no exception will get risen.

    In create table case, you can always double check:

    try:
        cur.execute("SELECT ouch FROM aargh;")
    except Exception, e:
        pass
    
    errorcodes.lookup(e.pgcode[:2])
    # 'CLASS_SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION'
    errorcodes.lookup(e.pgcode)
    # 'UNDEFINED_TABLE'