Python sqlite3.OperationalError:没有这样的表: [英] Python sqlite3.OperationalError: no such table:

查看:550
本文介绍了Python sqlite3.OperationalError:没有这样的表:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试存储有关学校学生的数据。我之前做过几张桌子,例如一张密码表和一张Teachers桌子,稍后将它们合并到一个程序中。

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.

我已经从其中之一复制了create table函数,并将其值更改为Pupil的信息。

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:

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")

我在文件夹中查找了一个名为 PupilPremiumTable.db 的文件,该表之前已经创建过,所以我不知道为什么它不起作用。

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.

这是我的一些代码,如果您需要更多的信息告诉我,就像我之前说的那样,那么我没有知道为什么它不起作用甚至什么都不起作用:

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)


推荐答案

您假设当前的工作目录与脚本所在的目录相同。这不是假设你(们)能做到。您的脚本正在另一个目录中打开一个 new 数据库,该目录为空。

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:

您可以使用 os.getcwd() 如果您想弄清楚要打开的位置新的数据库文件;您可能要清理在此处创建的多余文件。

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.

这篇关于Python sqlite3.OperationalError:没有这样的表:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆