为什么这个错误不是例外

时间:2018-03-09 15:52:00

标签: mysql python-3.x exception

我有以下代码:

    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'

为什么没有被异常捕获并打印出错误的"?

1 个答案:

答案 0 :(得分:1)

您的代码中有两个cursor.execute(),其中只有一个受try / except块保护。

我怀疑如果您将追溯(179)中的线与源代码中的第179行进行比较,您会发现它位于cursor.execute()之外的try {1}} / except阻止。

相关问题