[Python] Fix: sqlite3.OperationalError: no such table


>>> import sqlite3
>>> conn = sqlite3.connect('mydb.db')
>>> cursor = conn.cursor()
>>> cursor.execute("INSERT INTO employee (emp_name, emp_dept) VALUES (?, ?)", ('Sam', 'Finance'))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

sqlite3.OperationalError: no such table: employee

You may get the "OperationalError - no such table" error, when working with a SQL table with SQLite database in Python for the below reasons.

  • Make sure that the table you are trying to access does exist.
  • Make sure that you are not creating a new database (if the DB location is incorrect) thus the table does not exist.

Fix for OperationalError - no such table

  • Create the table in your SQLite database before you can perform operations on it.
  • If the database and the table already exist, make sure you are within the relative path where the database is.

sqlite3.OperationalError- no such table

We can know more about the OperationalError by taking a look at "PEP-249 – Python Database API Specification v2.0"

Exception
|__Warning
|__Error
   |__InterfaceError
   |__DatabaseError
      |__DataError
      |__OperationalError
      |__IntegrityError
      |__InternalError
      |__ProgrammingError
      |__NotSupportedError

OperationalError error is a sub-class of DatabaseError Exception which is raised when there are errors that are related to the database’s operation. These exceptions are not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, a memory allocation error occurred during processing, etc.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Β© Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap