Python sqlite3.OperationalError: no such table:

前端 未结 3 1713
再見小時候
再見小時候 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:12

    First you need to check if that table is 100% exist in the datbase you can use sqlite viewer for that https://inloop.github.io/sqlite-viewer/

    if table exist then you can write your table name in '' for example

    ''' Select * from 'TableName' '''

    what ever your query is I am just using Select * as an example

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-14 01:32

    I have to face same issue and there are a couple of approaches, but the one I think is the most probable one.

    Maybe you are loading views or queries to database but you haven´t granted enough time for Django to migrate the models to DB. That's why the "table doesn't exist".

    Make sure you use this sort of initialization in you view's code:

    form RegisterForm(forms.Form):
    
        def __init__(self, *args, **kwargs):
            super(RegisterForm, self).__init__(*args, **kwargs)
    

    A second approach is you clean previous migrations, delete the database and start over the migration process.

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