Python 为什么我突然收到“OperationalError:没有这样的表”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14262771/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:54:57  来源:igfitidea点击:

Why am I suddenly getting "OperationalError: no such table"?

pythonsqlite

提问by andy

I am trying to do various things with my database. I've connected and pulled data out and out data in, successfully, no problems. I've been debugging other issues, and then suddenly I can no longer get anything from my database table - I'm getting "OperationalError: no such table: article".

我正在尝试用我的数据库做各种事情。我已经成功地连接并拉出数据和拉出数据,没有问题。我一直在调试其他问题,然后突然我无法再从我的数据库表中获取任何信息 - 我收到“OperationalError:没有这样的表:文章”。

I'm really stumped here - this was working just fine, I was querying the db with no problems and inserting data, etc etc. Then suddenly I'm getting this error. The changes I made immediately before the error started appearing would seem to be totally unrelated - I undid them and still get this error. Here's the start of my script where I'm getting the error:

我真的被难住了 - 这工作得很好,我没有问题地查询数据库并插入数据等。然后突然我收到了这个错误。我在错误开始出现之前所做的更改似乎完全无关 - 我取消了它们但仍然收到此错误。这是我收到错误的脚本的开头:

import sqlite3

database='mydatabase'
db=sqlite3.connect(database)
c=db.cursor()

sql_command='SELECT id FROM article'
idlist=c.execute(sql_command)

I can open that database in SQLite Administrator and verify the table is there. Plus it was working before. I've also tried to verify that the table is in there by:

我可以在 SQLite Administrator 中打开该数据库并验证该表是否存在。加上它以前工作过。我还尝试通过以下方式验证该表是否在那里:

>>c.execute('select name from sqlite_master where type="table"').fetchall()
[]

so something is really wacky.

所以有些东西真的很古怪。

I've also tried closing and reopening the db connection and cursor. And closing the Python session. No dice. Help!

我还尝试关闭并重新打开数据库连接和游标。并关闭 Python 会话。没有骰子。帮助!

采纳答案by MBarsi

did you moved your code to another place?

你把你的代码移到另一个地方了吗?

because sqlite store the database into a file, when you call connect, if a file with the name 'mydatabase'exist, it will be loaded, otherwise. a new fresh database file will be created automatically.

因为sqlite将数据库存储到一个文件中,调用connect时,如果'mydatabase'存在同名文件则加载,否则加载。将自动创建一个新的数据库文件。

search for your old file with name 'mydatabase'and put it within your code.

使用名称搜索旧文件'mydatabase'并将其放入代码中。

回答by user3165060

I had exactly the same problem with sqlite3 and flask accessing a database. I changed the path to the database in my code to its full path and this solved the problem of data disappearing and tables not being found after restarting my flask application. I can recommend SQLite Manager firefox plugin, which helps with viewing records in your database tables. Follow this link to see a similar problem solved using full path references to your database Django beginner problem: manage.py dbsync

我在 sqlite3 和 Flask 访问数据库时遇到了完全相同的问题。我将代码中数据库的路径更改为其完整路径,这解决了重新启动 Flask 应用程序后数据消失和找不到表的问题。我可以推荐 SQLite Manager firefox 插件,它有助于查看数据库表中的记录。按照此链接查看使用对数据库Django 初学者问题的完整路径引用解决的类似问题:manage.py dbsync

Python does not do path expansion.so you might want to use Environment variables to store paths to your database. Link to WingWare's Environment Variable expansion

Python 不进行路径扩展。因此您可能希望使用环境变量来存储数据库的路径。 链接到 WingWare 的环境变量扩展

回答by EdgarT

I had the the problem. In my case, before trying to access the database I have this line:

我遇到了问题。就我而言,在尝试访问数据库之前,我有这一行:

 os.chdir("other/dir")
 conn = sqlite3.connect("database.db")

So sqlite3 create an empty database in that dir instead.

因此 sqlite3 在该目录中创建一个空数据库。