user3605780
user3605780

Reputation: 7072

Why isn't this error an exception

I have the following code:

    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")

I'm getting this error:

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'

Why isn't this caught by the exception and printing "something wrong"?

Upvotes: 1

Views: 552

Answers (1)

Ethan Furman
Ethan Furman

Reputation: 69110

You have two cursor.execute()s in your code, and only one of them is protected by a try/except block.

I suspect if you compare the line in the traceback (179) with line 179 in your source code, you'll find it's the cursor.execute() that is outside the try/except block.

Upvotes: 1

Related Questions