Sqlite3, OperationalError: unable to open database file

前端 未结 19 1485
再見小時候
再見小時候 2020-11-27 18:58

Question: Why can\'t I open the database?


Info: I\'m working on a project using sqlite3 database. I wrote a test program that runs and passes it t

相关标签:
19条回答
  • 2020-11-27 19:15

    The only thing you need to do is create the folder (as it doesn't exist already), only the database file will be created by the program. This really worked for me!

    0 讨论(0)
  • 2020-11-27 19:17

    Use the fully classified name of database file

    Use- /home/ankit/Desktop/DS/Week-7-MachineLearning/Week-7-MachineLearning/soccer/database.sqlite

    instead-

    0 讨论(0)
  • 2020-11-27 19:18

    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')
    
    0 讨论(0)
  • 2020-11-27 19:20

    had the same problem but top answer is too long for me so I suggest to open another shell window type cd #enter and try again

    0 讨论(0)
  • 2020-11-27 19:21

    Make sure you are not editing the settings.py file while trying to run syncdb, you will get the same error!!!

    self.connection = Database.connect(**kwargs)
    sqlite3.OperationalError: unable to open database file
    
    0 讨论(0)
  • 2020-11-27 19:28

    in my case, i tried creating the sqlite db in /tmp folder and from all the slashes i missed a single slash

    Instead of sqlite:///tmp/mydb.sqlite -> sqlite:////tmp/mydb.sqlite ...

    0 讨论(0)
提交回复
热议问题