Fixing Sqlite and Python Error – sqlite3.OperationalError: no such table

Sqlite and Python Error – sqlite3.OperationalError: no such table is an error which occurs when the directory of your database is different than your working directory.

Today I will be explaining why this error is taking place and how to solve it.

Exploring the Sqlite and Python Error – sqlite3.OperationalError: no such table

This is an error which occurs when the directory of your database is different than your working directory.

Beware of mixing between different errors. Please make sure you are dealing with the same error.

                                                                       #
sqlite3.OperationalError: no such table: <yourTable>
                                                                       #

Bellow is a number of tested solutions that I have tried and worked for me.

The Method that eliminated my problem : Correctly get the directory where the database lives

The error occurs when the directory of the database is not the same as the directory of your scripts.

It gets confusing yes, since this is most likely a new database and you do not know it is path. Do not worry the fix is simple, you can just derive the path of your database from the path of your working directory.

This is how your code is going to look like.

First, you should start by importing os.path. using the line of code bellow.

                                                                       #
import os.path
                                                                       #

Store the path of the directory in DIRorigin for example

                                                                       #
DIRorigin = os.path.dirname(os.path.abspath(__file__))
                                                                       #

Now, we should use the line of code bellow to store the path to the database in DBpath

                                                                       #
DBpath = os.path.join(BASE_DIR, "mydatabase.db")
                                                                       #

and Voila, do not forget to add this line at the end.

                                                                       #
with sqlite3.connect(DBpath) as db:
                                                                       #

This method should fix the issue for most people, If that is not the case for you, I am sorry but I do not know any other way to solve this issue. The only method that solved my problem is the method above.

I hope this guide solved your problem, thank you for reading. Goof luck with your Python Journey.

Summing-up : 

This is the end of this article guys, I hope one of these solutions worked for you depending on which OS you have, I wish you good luck with your Python Journey. For donations you can use the red Kofi button above.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/