Your Python program opens the museum database and asks for the visitor log. The table is definitely there, you created it yesterday, but SQLite says it isn't:
Traceback (most recent call last):
File "main.py", line 3, in <module>
rows = db.execute("SELECT * FROM visitors").fetchall()
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such table: visitorsno such table: visitors means that the database SQLite has open contains no table with that name. There are only two possibilities: the name in your query doesn't match the table, or you're connected to a different database file than the one that has the table. The second one is the sneaky one, because SQLite quietly creates a new, empty database file when the one you asked for doesn't exist. This article shows how to tell which case you have and fix it in a few minutes.
Try it yourself
In a new, empty folder, save this as main.py and run python main.py:
import sqlite3
db = sqlite3.connect("museum.db")
rows = db.execute("SELECT * FROM visitors").fetchall()
print(rows)You'll get the error above. Now look in the folder: there's a new file called museum.db. In our test, it was 0 bytes. SQLite didn't find a museum.db, so it created an empty one, and an empty database has no tables at all.
What the error means
Read the last line in parts:
sqlite3.OperationalError: the error comes from SQLite, raised through Python'ssqlite3module. Other tools show the same message without this prefix.no such table: SQLite looked for the table and didn't find it.: visitors: the exact name it looked for, after your query was read. Copy it, letter by letter.
So ask two questions, in this order: Is this the right database file? And: is the name exactly right?
Cause 1: you opened the wrong database file
This causes more "no such table" errors than any typo, and it happens in three ways.
A relative path and the wrong working folder
sqlite3.connect("museum.db") doesn't look next to your Python file. It looks in the current working directory: the folder your terminal or editor was in when it started the program. In our test, the real database was in data/museum.db, the script said "museum.db", and running it gave:
sqlite3.OperationalError: no such table: visitorsAnd a new, empty museum.db appeared next to the script.
The reliable fix is to build the path from the script's own location:
import sqlite3
from pathlib import Path
DB_PATH = Path(__file__).parent / "data" / "museum.db"
db = sqlite3.connect(DB_PATH)
print(db.execute("SELECT * FROM visitors").fetchall())[('Alex',)]In our test this worked even when we ran it from a completely different folder. Path(__file__).parent is the folder that contains the script, wherever you start it from.
A misspelled file name
sqlite3.connect("musuem.db") doesn't fail. It creates musuem.db, empty, and the next query says no such table. Check the file name as carefully as the table name.
Two copies of the database
You created the table in DB Browser for SQLite, in a file on your desktop, but your program opens a copy in the project folder. Both are called museum.db, only one has the table. Check the full path of the file in each tool.
How to check which file you're connected to
Ask the database itself which file and which tables it has:
print(db.execute("PRAGMA database_list").fetchall())
print(db.execute("SELECT name FROM sqlite_master WHERE type = 'table'").fetchall())The first line shows the full path of the open file. The second lists every table in it. If the list is empty, you're in an empty database: go back to the path.
Cause 2: a typo in the table name
If the file is right, compare the names:
CREATE TABLE visitors (name TEXT, room TEXT);
SELECT * FROM visitor;no such table: visitorvisitor without the s. Singular versus plural is the most common table-name typo, because people name tables both ways. Pick one style, for example plural, and use it for every table.
Capital letters, on the other hand, don't matter in SQLite: SELECT * FROM Visitors found the visitors table in our test. That's different in PostgreSQL (see below).
A space or a quote in the name
If a table name was created or written with double quotes, every character counts, including spaces:
SELECT * FROM "visitors ";no such table: visitors The message ends with a space you can barely see. Avoid quotes and spaces in table names: visitor_log, not "visitor log".
Cause 3: the table was never created, or not saved
You wrote the CREATE TABLE, but did it actually run against this file?
- The
CREATE TABLEis in a different script that you haven't run, or ran against another file. - The script failed before it reached the
CREATE TABLEline. - You created the table in DB Browser for SQLite and didn't click "Write Changes". Until you write the changes, other programs don't see them.
- You used an in-memory database,
sqlite3.connect(":memory:"). It disappears when the program ends, along with every table in it.
A good habit for small projects: create tables with CREATE TABLE IF NOT EXISTS at the start of your program, so the table exists in whichever file you open:
db.execute("CREATE TABLE IF NOT EXISTS visitors (name TEXT, room TEXT)")Cause 4: the table was dropped or renamed
If the table existed before and now doesn't, look for a DROP TABLE or ALTER TABLE ... RENAME TO that ran, maybe in a setup script that resets the database each time. Searching your project for DROP TABLE takes ten seconds.
The same error in an INSERT
The message is the same for any statement that names the table:
INSERT INTO visitor_log VALUES ('Alex', 'Vault');no such table: visitor_logAn INSERT doesn't create the table for you. The table must exist first.
The same problem in PostgreSQL
PostgreSQL calls tables relations, so its message is different:
ERROR: relation "visitor" does not exist
LINE 1: SELECT * FROM visitor;
^PostgreSQL also has a trap that SQLite doesn't: it turns unquoted names into lowercase. A table created as "Visitors2" with quotes keeps its capital letter, and then SELECT * FROM Visitors2 fails:
ERROR: relation "visitors2" does not exist
LINE 1: SELECT * FROM Visitors2;
^Notice the lowercase visitors2 in the message: that's what PostgreSQL looked for. Use lowercase names without quotes, and this never happens. (In MySQL the message is Table 'dbname.visitor' doesn't exist.)
Errors that look alike
no such column: x: the table exists, but a column name is wrong. See SQLite: no such column.table visitors already exists: the opposite: you ranCREATE TABLEtwice. UseCREATE TABLE IF NOT EXISTS.unable to open database file: SQLite couldn't even create the file, usually because the folder in the path doesn't exist.misuse of aggregateand otherGROUP BYerrors: the table is fine, the query isn't. See GROUP BY errors.
Wrong fixes that don't help
- Running
CREATE TABLEagain in your program "to be safe" withoutIF NOT EXISTS. The second run fails withtable already exists. - Copying the database file around until it works. You end up with several copies and no idea which one is current. Fix the path once.
- Deleting the empty
museum.db. It's a symptom: the next run creates it again. Fix the path that created it.
A debugging walkthrough
- Look for an empty
.dbfile (0 bytes) next to your script. If there is one, the path is wrong: go to cause 1. - Run
PRAGMA database_listto see the full path of the open file. - List the tables with the
sqlite_masterquery. Is your table there, under exactly this name? - If the list is empty, either the path is wrong or the table was never created in this file.
- If the table is there with a different name, fix the query.
- Build the path with
Path(__file__).parent, so it works from any folder.
Practice
What happens in each case?
sqlite3.connect("shop.db")in a folder withoutshop.db, thenSELECT * FROM orders.- The table is
orders, the query saysSELECT * FROM Orders, in SQLite. - The same as 2, but in PostgreSQL, and the table was created as
CREATE TABLE orders.
Answers. (1) no such table: orders, and a new empty shop.db appears. (2) Works: SQLite table names aren't case-sensitive. (3) Works too: PostgreSQL lowercases both the unquoted orders and the unquoted Orders. Only quoted names keep their capitals.
Frequently asked questions
What does "no such table" mean in SQLite?
The database file that's open has no table with that name. Either the name is wrong, or you're connected to a different file.
Why does SQLite create an empty database file?
sqlite3.connect creates the file if it doesn't exist. That's convenient for new projects, but it hides wrong paths: you get an empty database instead of an error about the file.
How do I see all tables in an SQLite database?
SELECT name FROM sqlite_master WHERE type = 'table';. In the sqlite3 command-line tool, .tables does the same.
Are SQLite table names case-sensitive?
No. visitors and Visitors are the same table. PostgreSQL is different: it lowercases unquoted names and keeps quoted ones exactly.
Why does my query work in DB Browser but not in Python?
Either Python opens a different file, or the changes in DB Browser haven't been written. Click "Write Changes", and check the path Python uses.
A quick checklist
- Look for a new, empty
.dbfile: the path is wrong. - Check the open file with
PRAGMA database_list. - List the tables with
sqlite_master. - Compare the table name letter by letter, including singular and plural.
- Use
Path(__file__).parentto build the database path. - Use
CREATE TABLE IF NOT EXISTSin setup code.
For a method that works on any error, read how to read an error message. The SQLite documentation explains the schema table that lists every table.
