2

While using a relative path, I'm getting an error with Sqlite3: OperationalError: unable to open database file,

But with an absolute path it works well. Below are the samples:

#database_filepath = 'Data/DatabaseFile.db'

def load_data(database_filepath):
    
    con = sqlite3.connect(database_filepath) #not working
    #con = sqlite3.connect(os.path.abspath(database_filepath)) #working fine

    df = pd.read_sql_query('select * from someTable', con)

Is there any better way doing it?

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Relative path should work. Please check your working directory, and if there is a sub dir named Data. – Yang HG Mar 14 '19 at 06:55

1 Answers1

0

Your codes work fine in my computer. Please check your working directory by:

import os
print(os.path.abspath('.'))

Then check if there is a sub dir ./Data existed.

print(os.path.isdir('./Data'))

If not, you should create this dir first. sqlite will not do it automatically.

Yang HG
  • 710
  • 3
  • 18