pymysql:一次尝试除了无法捕获多sql错误

时间:2017-06-21 09:31:52

标签: python mysql sql-server pymysql

更新

conn = pymysql.connect(host='localhost',user='user',password='password',db='mydb',charset='utf8')
cur = conn.cursor(pymysql.cursors.DictCursor)
try:
    query = 'select * from test where id = 1;abcd'
    cur.execute(query)
    res = cur.fetchone()
    print(res)
except Exception as e:
    print(e)

try:
    query = 'select * from test where id = 2;'
    cur.execute(query)
    res = cur.fetchone()
    print(res)
except Exception as e:
    print(e)

代码的结果如下:

{'name': '', 'score': 1.1, 'id': 1}
(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'abcd' at line 1")

第一行{'name': '', 'score': 1.1, 'id': 1}来自第一个;try...except...之前的第一个查询。然后错误消息来自第二个try...except...,但它来自sql查询abcd,这意味着第二个cur.execute(query)中的try...except...产生异常,因为{ {1}}和abcd未执行。

那么如何忽略select * from test where id = 2;并使第二个查询按预期执行?


原始问题
我正在用python构建一个Web服务器。我使用MySQL作为数据库,使用pymysql作为数据库的接口。

现在我遇到了一个问题:
当某个sql查询由于abcd而出错时,即使我使用了;,该程序也会被阻止。这是一个例子:

try...except...

如您所见,由于import pymysql import pymysql.cursors conn = pymysql.connect(host='localhost',user='user',password='password',db='mydb',charset='utf8') cur = conn.cursor(pymysql.cursors.DictCursor) try: query = 'select * from test where id = 1;abcd' <--- exception! cur.execute(query) res = cur.fetchone() print(res) except Exception as e: print(e) query = 'select * from test where id = 2;' cur.execute(query) res = cur.fetchone() print(res) 部分,第一个query是非法的。所以它会产生错误。但是,;abcd无法捕获此异常,因为我发现try...except...未执行。以下是我收到的消息:

print(e)

我无法理解为什么{'score': 1.1, 'name': '', 'id': 1} Traceback (most recent call last): File "db_mysql.py", line 23, in <module> cur.execute(query) File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 161, in execute while self.nextset(): File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 103, in nextset return self._nextset(False) File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 98, in _nextset conn.next_result(unbuffered=unbuffered) File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 860, in next_result self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1057, in _read_query_result result.read() File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1340, in read first_packet = self.connection._read_packet() File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1014, in _read_packet packet.check_error() File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error err.raise_mysql_exception(self._data) File "/usr/local/lib/python3.5/site-packages/pymysql/err.py", line 107, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'abcd' at line 1") 无法捕获错误。此外,似乎产生错误的查询是try...except...,而不是abcd。如果我是对的,我认为select * from test where id = 1;abcd会将此查询分为两个查询。

此外,如果我删除;,这意味着第一个查询变为;query = 'select * from test where id = 1abcd'可能会捕获错误,因此可以按预期执行第二个查询。 />

所以我的问题是:为什么try...except...无法捕获错误?我该怎么做才能处理所有sql错误,以免程序被阻止?

1 个答案:

答案 0 :(得分:1)

  

所以我的问题是:为什么try ... except ...无法捕获错误?什么   我该怎么做才能处理所有的SQL错误,这样程序就不会   被阻止?

也许具有很好的异常名称?

try:
    …
except pymysql.err.ProgrammingError as except_detail:
        print("pymysql.err.ProgrammingError: «{}»".format(except_detail))

查看在help(pymysql.err)PyMySQL's Github上键入的所有例外情况