我有以下代码:
query = "SELECT * FROM `actions` WHERE clientId = '{}'".format(id)
cursor.execute(query)
r = cursor.fetchone()
try:
query = (
"INSERT INTO `actions`(`name`,`price`,`clientId`) VALUES"
" ('{}',{}'{}')".format(r['name'],r['price']+10, id)
)
cursor.execute(query)
self.dbconn.commit()
except:
print("something wrong")
我收到这个错误:
Traceback (most recent call last):
File "/opt/trybinance01/collector.py", line 179, in function1
cursor.execute(query)
File "/opt/trybinance01/venv_binance/lib/python3.6/site-packages/pymysql/cursors.py", line 165, in execute
result = self._query(query)
File "/opt/trybinance01/venv_binance/lib/python3.6/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/opt/trybinance01/venv_binance/lib/python3.6/site-packages/pymysql/connections.py", line 860, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/opt/trybinance01/venv_binance/lib/python3.6/site-packages/pymysql/connections.py", line 1061, in _read_query_result
result.read()
File "/opt/trybinance01/venv_binance/lib/python3.6/site-packages/pymysql/connections.py", line 1349, in read
first_packet = self.connection._read_packet()
File "/opt/trybinance01/venv_binance/lib/python3.6/site-packages/pymysql/connections.py", line 1008, in _read_packet
recv_data = self._read_bytes(bytes_to_read)
File "/opt/trybinance01/venv_binance/lib/python3.6/site-packages/pymysql/connections.py", line 1025, in _read_bytes
data = self._rfile.read(num_bytes)
AttributeError: 'NoneType' object has no attribute 'read'
为什么不被异常所抓住并打印“出错”?
你的代码中有两个cursor.execute()
s,其中只有一个受try
/ except
块的保护。
我怀疑如果你将追溯(179
)中的线与源代码中的第179行进行比较,你会发现它是cursor.execute()
/ try
块之外的except
。