我查看了文档,但是没有找到任何让我知道我通过cursor.execute("...")执行的最后一个命令是否成功的信息.
我期待一个像"1行受影响"的回复.
Aks*_*jan 10
这是一个老问题,但检查成功操作的一种方法psycopg2是在rowcount语句后查看光标的属性.此属性返回受最后一个execute语句影响的行数.
例如
connection = psycopg2.connect(dbname="foo",user="postgres")
cur = connection.cursor()
cur.execute("INSERT INTO foo VALUES (%s, %s)", (1,2))
cur.rowcount # returns 1
cur.execute("SELECT * FROM foo")
cur.rowcount # returns 0
Run Code Online (Sandbox Code Playgroud)
类似的属性是statusmessage,返回一个字符串,包括执行的最后一个操作的类型以及受影响的行数.
我预计会出现某种异常。
\n如果一切正常 \xe2\x80\x93 错误代码为00000并且不会出现异常。
以防create table万一,您可以随时仔细检查:
try:\n cur.execute("SELECT ouch FROM aargh;")\nexcept Exception, e:\n pass\n\nerrorcodes.lookup(e.pgcode[:2])\n# \'CLASS_SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION\'\nerrorcodes.lookup(e.pgcode)\n# \'UNDEFINED_TABLE\'\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4182 次 |
| 最近记录: |