close

[Solved] Sqlite3, OperationalError: unable to open database file

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Sqlite3, OperationalError: unable to open database file in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Sqlite3, OperationalError: unable to open database file Error Occurs?

Today I get the following error Sqlite3, OperationalError: unable to open database file in python.

How To Solve Sqlite3, OperationalError: unable to open database file Error ?

  1. How To Solve Sqlite3, OperationalError: unable to open database file Error ?

    To Solve Sqlite3, OperationalError: unable to open database file Error On unix I got that error when using the ~ shortcut for the user directory. Changing it to /home/user resolved the error.

  2. Sqlite3, OperationalError: unable to open database file

    To Solve Sqlite3, OperationalError: unable to open database file Error On unix I got that error when using the ~ shortcut for the user directory. Changing it to /home/user resolved the error.

Solution 1

One reason might be running the code in a path that doesn’t match with your specified path for the database. For example if in your code you have:

conn = lite.connect('folder_A/my_database.db')

And you run the code inside the folder_A or other places that doesn’t have a folder_A it will raise such error. The reason is that SQLite will create the database file if it doesn’t exist not the folder.

One other way for getting around this problem might be wrapping your connecting command in a try-except expression and creating the directory if it raises sqlite3.OperationalError.

from os import mkdir import sqlite3 as lite

try:
    conn = lite.connect('folder_A/my_database.db')
except lite.OperationalError:
    mkdir('folder_A')
finally:
    conn = lite.connect('folder_A/my_database.db')

Solution 2

On unix I got that error when using the ~ shortcut for the user directory. Changing it to /home/user resolved the error.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read