pymysql: 一次尝试,但无法捕获 multi-sql 错误
pymysql: one try except can't catch multi-sql errors
更新
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
,这意味着第二个 try...except...
中的 cur.execute(query)
产生由于 abcd
和 select * from test where id = 2;
未执行的异常。
那么我怎样才能忽略 abcd
并使第二个查询按预期执行呢?
原始问题
我正在使用 python 构建 Web 服务器。我使用MySQL作为数据库,pymysql作为数据库的接口。
现在我遇到一个问题:
当某些sql查询由于;
而出错时,即使我使用了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...
可能会捕获错误,因此第二个查询可以按预期执行。
所以我的问题是:为什么 try...except...
无法捕获错误?我该怎么做才能处理所有 sql 错误,以便程序不会被阻止?
So my question is: why the try...except... can't catch the error? What
could I do to handle all of sql errors so that the program won't be
blocked?
也许有好的例外名称?
try:
…
except pymysql.err.ProgrammingError as except_detail:
print("pymysql.err.ProgrammingError: «{}»".format(except_detail))
查看 help(pymysql.err)
或 PyMySQL's Github
中的所有异常类型
更新
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
,这意味着第二个 try...except...
中的 cur.execute(query)
产生由于 abcd
和 select * from test where id = 2;
未执行的异常。
那么我怎样才能忽略 abcd
并使第二个查询按预期执行呢?
原始问题
我正在使用 python 构建 Web 服务器。我使用MySQL作为数据库,pymysql作为数据库的接口。
现在我遇到一个问题:
当某些sql查询由于;
而出错时,即使我使用了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...
可能会捕获错误,因此第二个查询可以按预期执行。
所以我的问题是:为什么 try...except...
无法捕获错误?我该怎么做才能处理所有 sql 错误,以便程序不会被阻止?
So my question is: why the try...except... can't catch the error? What could I do to handle all of sql errors so that the program won't be blocked?
也许有好的例外名称?
try:
…
except pymysql.err.ProgrammingError as except_detail:
print("pymysql.err.ProgrammingError: «{}»".format(except_detail))
查看 help(pymysql.err)
或 PyMySQL's Github