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.
我試圖在學校存儲有關學生的數據。我以前做過幾張桌子,比如一張用於密碼和教師的桌子,我稍后會把它們放在一個程序中。
I have pretty much copied the create table function from one of these and changed the values to for the Pupil's information. It works fine on the other programs but I keep getting:
我幾乎從其中一個復制了create table函數,並將值更改為Pupil的信息。它在其他程序上工作正常,但我一直在:
sqlite3.OperationalError: no such table: PupilPremiumTable
when I try to add a pupil to the table, it occurs 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 isn't working.
我查看該文件夾,並且有一個名為PupilPremiumTable.db的文件,該表已經創建過,所以我不知道為什么它不起作用。
Here is some of my code, if you need more feel free to tell me so, as I said it worked before so I have no clue why it isn't working or even what isn't working:
這是我的一些代碼,如果您需要更多隨意告訴我,正如我之前所說的那樣,所以我不知道為什么它不起作用甚至什么不起作用:
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)
1 个解决方案
#1
25
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.
如果要確定打開新數據庫文件的位置,可以使用os.getcwd()驗證當前工作目錄的內容。你可能想要清理你在那里創建的額外文件。