Python sqlite3.OperationalError:没有这样的表:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28126140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python sqlite3.OperationalError: no such table:
提问by Ben
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:
我几乎从其中之一复制了创建表函数,并将值更改为学生信息。它在其他程序上运行良好,但我不断收到:
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)
采纳答案by Martijn Pieters
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 newdatabase 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()
如果您想找出打开新数据库文件的位置,您可以验证当前工作目录的内容;您可能想要清理在那里创建的额外文件。
回答by Jorge Cardenas
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".
也许您正在将视图或查询加载到数据库,但您没有给 Django 足够的时间将模型迁移到数据库。这就是“表不存在”的原因。
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.
第二种方法是清理以前的迁移,删除数据库并重新开始迁移过程。
回答by Usman Ghani Mughal
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/
首先,您需要检查该表是否 100% 存在于数据库中,您可以为该https://inloop.github.io/sqlite-viewer/使用 sqlite 查看器
if table exist then you can write your table name in '' for example
如果表存在,那么你可以在 '' 中写你的表名,例如
''' Select * from 'TableName' '''
''' 从 'TableName' 中选择 * '''
what ever your query is I am just using Select * as an example
您的查询是什么我只是使用 Select * 作为示例