All Courses

Errors in Python

Shashank Shanu

2 years ago

Errors In Python
Table of Content
  • Why Python sqlalchemy.exc.ResourceClosedError occurs ?
  • Output
  • How to solve this error?
            Many times, while performing your tasks in python you surely would come across many different types of error. In this article, I will try to give you detailed explanation of one of the error Python sqlalchemy.exc.ResourceClosedError, why this error occurs and how we can avoid or solve such error while performing our task.
So, let’s start…
While performing any task, there are many possibilities that a user may get an error. Error is a very good thing as it gives an opportunity to explore many different aspects and come to a solution. But before solving any error it primes important to know why this error occurred and then only, we can go for it to solve it.
SQLAlchemy has two main concepts that one must be aware of i.e., connections and engines. An engine can support many simultaneous connections. If we try to bind the table to an engine. Now, whenever we call. Execute function, a new connection is created for every single query that you execute. But sqlite3 only allows 1 simultaneous "connection". Let me give you a detailed explanation.

Why Python sqlalchemy.exc.ResourceClosedError occurs ?

To understand it better let take an example
engine = create_engine('sqlite:///testdb.db')
metadata = MetaData(bind=engine)
test = Table('test', metadata, autoload=true)

# Select all from pending_data
sel = select([test])
res = engine.execute(sel)


# do an insert into pending_data


Code Error: Python sqlalchemy.exc.ResourceClosedError
             There are many reasons which raise this type of error. Let me explain to you some of them one by one.
In simple word, one of the reasons why this error occurs is when we try to read rows from a SQL a statement that does not return any rows.
Example of such statements are INSERT, UPDATE and DELETE statements that do not return any column. While a SELECT statement in SQL, on the other hand, should always return rows, even if the number of rows is zero.
The second reason that raises this error is when the database driver has failed on a previous statement and the application attempts to continue to use the database connection without rolling back the transaction first, however, this requires additional conditions to be in a place that is not easy to replicate unless the SQL statement is accessing some specific kind of issue.

How to solve this error?

            The best way to fix this error is to create the connection and use it explicitly as shown below. And instead of the automatically created connections of the engine and to use the connection with the statement, which ensures that the connection would be closed at the end of the block:
engine = create_engine('sqlite:///testdb.db')
metadata = MetaData(bind=engine)
test = Table('test', metadata, autoload=true)

with engine.connect() as connection:
    # Select all from pending_data
    sel = select([test])
    res = connection.execute(sel)

    # do an insert into pending_data
I hope after you enjoyed reading this article and finally, you came to know about Python sqlalchemy.exc.ResourceClosedError.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Programming…

Submit Review