我怎么知道我是否成功创建了一个表(Python、Psycopg2)?

问题描述:

我查看了文档,但没有找到任何让我知道我通过 cursor.execute("...") 执行的最后一个命令是否成功的信息.

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.

我期待收到类似受影响的 1 行"之类的回复.

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

我期待某种例外情况.
如果一切顺利 - 错误代码是 00000 并且不会有任何例外.

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.

create table 情况下,您始终可以仔细检查:

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'