question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'sqlite:///:memory:' is OK while `sqlite:///db.sqlite` fails with `Cannot operate on a closed database.`

See original GitHub issue

Run

$ pytest -sv tests/test_transactions.py

and see, that when the test runs with in-memory sqlite database, all cases are passing, but if it is using real file, it fails in all but one case.

The failure shows tracebacks such as:

Traceback (most recent call last):
  File "/home/javl/sandbox/playwith_records/records/.tox/py36/lib/python3.6/site-packages/sqlalchemy/engine/result.py", line 1186, in fetchone
    row = self._fetchone_impl()
  File "/home/javl/sandbox/playwith_records/records/.tox/py36/lib/python3.6/site-packages/sqlalchemy/engine/result.py", line 1067, in _fetchone_impl
    return self.cursor.fetchone()
sqlite3.ProgrammingError: Cannot operate on a closed database.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/javl/sandbox/playwith_records/records/.tox/py36/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1288, in _safe_close_cursor
    cursor.close()
sqlite3.ProgrammingError: Cannot operate on a closed database.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

4reactions
felcianocommented, Nov 10, 2019

Has this fix been released? It looks like the patch was integrated on May 7 2019 but I just installed the master branch via pipenv install git+https://github.com/kennethreitz/records.git@master#egg=records and I’m still seeing this bug when trying to access a local sqlite3 database:

db = records.Database("sqlite:////absolute/path/to/db.sqlite3")
rows = db.query("SELECT * FROM TASK")
for r in rows:
	print(r)
0reactions
dannguyencommented, Jul 8, 2019

Not sure if this issue has been fixed with the May 7/12 merges, but just wanted to note that this error arises when using the records command-line tool:

$ records 'select * from things' csv --url sqlite:///mythings.sqlite
Error closing cursor
Traceback (most recent call last):
  File "/Users/dan/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages/sqlalchemy/engine/result.py", line 1186, in fetchone
    row = self._fetchone_impl()
  File "/Users/dan/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages/sqlalchemy/engine/result.py", line 1067, in _fetchone_impl
    return self.cursor.fetchone()
sqlite3.ProgrammingError: Cannot operate on a closed database.

During handling of the above exception, another exception occurred:

# .....

line 1186, in fetchone
    row = self._fetchone_impl()
  File "/Users/dan/.pyenv/versions/anaconda3-5.3.1/lib/python3.7/site-packages/sqlalchemy/engine/result.py", line 1067, in _fetchone_impl
    return self.cursor.fetchone()
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) Cannot operate on a closed database. (Background on this error at: http://sqlalche.me/e/f405)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot operate on a closed database. [Python] [sqlite] - Stack ...
You write "It seems to me like the connection to the database is not closed before the second connection is attempted" but, in...
Read more >
In-Memory Databases - SQLite
The database ceases to exist as soon as the database connection is closed. Every :memory: database is distinct from every other. So, opening...
Read more >
Connecting To SQLite Database Using Node.js
If an error occurred, the error object is not null , otherwise, it is null . If you don't provide the callback function...
Read more >
SQLite - SQLAlchemy 1.4 Documentation
The SQLite database supports transactional DDL as well. In this case, the pysqlite driver is not only failing to start transactions, it also...
Read more >
Database — peewee 3.15.4 documentation
from peewee import * # SQLite database using WAL journal mode and 64MB ... These statements are typically run when a new database...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found