Python sqlite3.OperationalError: no such table:

I am trying to store student data at school. I used to make several tables, for example, for passwords and teachers, which I later collect in one program.

I pretty much copied the create table function from one of them and changed the values ​​for the student information. It works great with other programs, but I keep getting:

sqlite3.OperationalError: no such table: PupilPremiumTable 

when I try to add a student to the table, it appears on the line:

 cursor.execute("select MAX(RecordID) from PupilPremiumTable") 

I look in the folder and there is a file called PupilPremiumTable.db , and the table has already been created before, so I don’t know why it does not work.

Here are some of my codes, if you need to feel free to tell me this anymore, since I said it worked before, so I don’t know why it doesn’t work or even what doesn’t work:

 with sqlite3.connect("PupilPremiumTable.db") as db: cursor = db.cursor() cursor.execute("select MAX(RecordID) from PupilPremiumTable") Value = cursor.fetchone() Value = str('.'.join(str(x) for x in Value)) if Value == "None": Value = int(0) else: Value = int('.'.join(str(x) for x in Value)) if Value == 'None,': Value = 0 TeacherID = Value + 1 print("This RecordID is: ",RecordID) 
+7
python database sqlite3
source share
1 answer

It is assumed that the current working directory is the same as the directory where your script is located. This is not an assumption you can make. Your script opens a new database in another directory that is empty.

Use the absolute path for your database file. You can install it in 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 check that the current working directory is with os.getcwd() if you want to find out where instead you open a new database file; you probably want to clear the extra file that you created there.

+20
source share

All Articles