Python sqlite3.OperationalError: no such table:

前端 未结 3 1716
再見小時候
再見小時候 2020-12-14 00:49

I am trying to store data about pupils at a school. I\'ve done a few tables before, such as one for passwords and Teachers which I will later bring together in one program.

3条回答
  •  一个人的身影
    2020-12-14 01:26

    You are assuming that the current working directory is the same as the directory your script lives in. It is not an assumption you can make. Your script is opening a new database in a different directory, one that is empty.

    Use an absolute path for your database file. You can base it on the absolute path of your script:

    import os.path
    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    db_path = os.path.join(BASE_DIR, "PupilPremiumTable.db")
    with sqlite3.connect(db_path) as db:
    

    You can verify what the current working directory is with os.getcwd() if you want to figure out where instead you are opening the new database file; you probably want to clean up the extra file you created there.

提交回复
热议问题