SQLITE_CANTOPEN_NOTEMPDIRERRORTier 2 — Caution⚠️ MEDIUM confidenceunable to open a temporary database file for storing temporary tables
What this means
SQLITE_CANTOPEN_NOTEMPDIR (extended code 270) is returned when SQLite needs to write a temporary file for sorting, hashing, or storing temporary tables but the temporary directory is unavailable, full, or not writable. This error is more commonly seen on mobile platforms and containerised environments where TMPDIR may be restricted.
Why it happens
- 1The TMPDIR environment variable points to a directory that does not exist or is not writable
- 2The temporary filesystem partition is full
- 3The application is running in a sandbox that restricts access to the default temporary directory
How to reproduce
TMPDIR is set to a non-existent directory and SQLite needs to spill a sort to disk.
import os, sqlite3
os.environ['TMPDIR'] = '/nonexistent/tmpdir'
conn = sqlite3.connect('demo.db')
# A sort that cannot fit in memory will attempt to spill to TMPDIR
conn.execute('SELECT * FROM large_table ORDER BY random_col')Fix 1: Set TMPDIR to a writable directory
When the default TMPDIR is wrong for the environment.
import os, sqlite3
import tempfile
# Use Python's tempfile module to find a valid temp directory
os.environ['TMPDIR'] = tempfile.gettempdir()
conn = sqlite3.connect('demo.db')Why this works
tempfile.gettempdir() returns a platform-appropriate writable temporary directory. Setting TMPDIR before connecting ensures SQLite uses a valid location for spill files.
What not to do
Increase cache_size to avoid the problem
Why it's wrong: Increasing cache_size may reduce the frequency of temp file spills but does not fix the underlying TMPDIR problem and will not help for very large result sets.
Version notes
SQLite 3.7.17+Extended code 270 introduced.Sources
📚 Official docs: https://www.sqlite.org/rescode.html#cantopen_notempdir
🔧 Source ref: sqlite3.h — SQLITE_CANTOPEN_NOTEMPDIR = 270
📖 Further reading: SQLite temporary files
Confidence assessment
⚠️ MEDIUM confidence
MEDIUM — extended code is documented but the specific trigger conditions are platform-dependent.
See also
📄 Reference pages