PYTHONWarningApril 18, 2026
Runtime Error
Cursor object used after being closed
What This Error Means
A cursor object is created from a database connection, but it's used again after the connection has been closed, resulting in a runtime error.
Why It Happens
This error occurs when a database cursor is used after the connection to the database has been closed. This can happen due to a misplaced close() method or if the connection is reopened and the existing cursor is not reused. It's essential to ensure that the cursor is not used after the connection has been closed.
How to Fix It
- 1To fix this error, ensure that the cursor object is not used after the connection has been closed. This can be achieved by closing the cursor before closing the connection or by reusing the existing cursor if the connection is reopened. Here's an example of how to do it correctly:
- 2// Before (broken code)
- 3cnx = sqlite3.connect('example.db')
- 4cur = cnx.cursor()
- 5cnx.close()
- 6cur.execute('SELECT * FROM table')
- 7// After (fixed code)
- 8cnx = sqlite3.connect('example.db')
- 9cur = cnx.cursor()
- 10cur.execute('SELECT * FROM table')
- 11cnx.close()
- 12cur.close()
Example Code Solution
❌ Before (problematic code)
Python
cnx = sqlite3.connect('example.db')
cur = cnx.cursor()
cnx.close()
cur.execute('SELECT * FROM table')✅ After (fixed code)
Python
cnx = sqlite3.connect('example.db')
cur = cnx.cursor()
cur.execute('SELECT * FROM table')
cnx.close()
cur.close()Fix for Cursor object used after being closed
Browse Related Clusters
Related PYTHON Errors
Related PYTHON Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error