参考:flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table

在用Flask写一个简单的py文件,做一个表单时,发现在login界面登陆的时候,出现:

OperationalError: (sqlite3.OperationalError) no such table: ...

错误,这个问题肯定是与数据库有关的;于是review了一下代码,关于数据库ORM的声明类Switches如下:

class Switches(db.Model):
__tablename__ = 'Switches'
sid = db.Column(db.Integer, primary_key=True)
sname = db.Column(db.String, unique=True, index=True)
#sprice = db.Column(db.Integer) def __repr__(self):
print('<Switch_id %d>' % id)

两列Column,一个主key叫做sid,外加一个属性sname,没有问题;也排除了app.config的问题,那么就是视图函数中的问题了。

但是看上去也没有问题:

@app.route('/', methods=['GET', 'POST'])
def index():
form = NameForm()
if form.validate_on_submit():
sw = Switches.query.filter_by(sname=form.name.data).first() # filter the DB
if sw is None:
sw = sw(sname=form.name.data)
db.session.add(sw)
session['known'] = False
else:
session['known'] = True
session['name'] = form.name.data
return redirect(url_for('myindex'))
return render_template('myindex.html', form=form, name=session.get('name'),
known=session.get('known', False))

逻辑语句if内是对表单的具体操作,在Swicthes数据库中找名字为输入数据的tuple,然后进行相关操作。

于是乎求助搜索引擎,在参考的那篇文章中回答者给出了这样的解释:

You're supposed to initialize/create the tables first. Please read the Creating the Database article in the official Flask documentation:

    Such systems need a schema that tells them how to store that information. So before starting the server for the first time it’s important to create that schema.

Here's Flask's example of using a schema SQL script to create the database, tables, etc:

sqlite3 /tmp/flaskr.db < schema.sql

The recommended way is to use db.create_all() within your app. For example, see: https://github.com/hypatia-software-org/staticfuzz/blob/master/staticfuzz.py#L391

大意是数据库没有创建这张表,一个推荐的解决方法是加入db.create_all()语句来创建表。

2017.3.30

解决 "OperationalError: (sqlite3.OperationalError) no such table: ..."问题的更多相关文章

  1. sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) Cannot add a NOT NULL column with default value NULL [SQL: u'ALTER TABLE address_scopes ADD COLUMN ip_version INTEGER NOT NULL']

    root@hett-virtual-machine:~# su -s /bin/sh -c "neutron-db-manage --config-file /etc/neutron/neu ...

  2. flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table

    狗书第五章 记得要先创建表 执行 db.create_all()语句来创建表 https://segmentfault.com/q/1010000005794140

  3. (sqlite3.OperationalError) no such table: users [SQL: 'SELECT users.id AS users_id, users.email AS users_email, users.username AS users_username, users.role_id AS users_role_id, users.password_hash A

    在注册新用户的时候报错: (sqlite3.OperationalError) no such table: users [SQL: 'SELECT users.id AS users_id, use ...

  4. 转!sqlite3.OperationalError) no such table- users [SQL- 'SELECT users.id AS users_id, users.email AS u

    在注册新用户的时候报错: (sqlite3.OperationalError) no such table: users [SQL: 'SELECT users.id AS users_id, use ...

  5. Airflow安装错误:sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError)

    1 完整的异常信息: raise errorclass, errorvalue sqlalchemy.exc.OperationalError: (_mysql_exceptions.Operatio ...

  6. sqlite3.OperationalError: no such table: account_user

    你可能是在项目中安装了多个app, 首先删除相关app的migration文件中的子文件 执行建表的时候使用: python manage.py makemigrations appname pyth ...

  7. 解决 pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 61] Conne

    pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 61] ...

  8. magento -- 解决magento错误:ERROR: Base table or view already exists: 1050 Table ... already exists

    相信有更新magento或者,备份转移magento站点的时候可能会碰到类似这样的错误提示: Base table or view already exists: 1050 Table ... alr ...

  9. 【MySQL】解决You can't specify target table 'user_cut_record_0413' for update in FROM clause

    问题 You can't specify target table 'user_cut_record_0413' for update in FROM clause 原因 待更新/删除的数据集与查询的 ...

随机推荐

  1. ios unrecognized selector sent to instance出现的原因和解决方案

    概述:造成unrecognized selector sent to instance iphone,大部分情况下是因为对象被提前release了,在你心里不希望他release的情况下,指针还在,对 ...

  2. linux下远程服务器批量执行命令及SFTP上传文件 -- python实现

    之前写过一个python远程执行命令的脚本,但在一个性能测试中,要将程序批量分发到不同服务器,程序无法使用,再将之前的脚本更新,加入批量上传的功能.之前脚本地址:http://www.cnblogs. ...

  3. WCF(一) 创建第一个WCF

    定义服务契约-创建宿主程序-创建客户端程序访问服务 namespace HelloService { /// <summary> /// 服务契约 /// </summary> ...

  4. [转]ORM的优缺点

    ORM[Object-Relation-Mapping]对象关系映射. 这个名词已经出来好几年了.已经不陌生.  以前在项目中针对相对复杂业务逻辑时一般采用领域模型驱动方式进行业务概述,分析和建模. ...

  5. CentOS 6.4下Squid代理服务器的安装与配置(转)

    add by zhj: 其实我们主要还是关注它在服务器端使用时,充当反向代理和静态数据缓存.至于普通代理和透明代理,其实相当于客户端做的事,和服务端没有什么关系.另外,Squid的缓存主要是缓存在硬盘 ...

  6. linux 下创建虚拟环境 python

    virtualenv是一个可以在同一计算机中隔离多个python版本的工具.有时,两个不同的项目可能需要不同版本的python,如 python2.7 / python3.6 ,但是如果都装到一起,经 ...

  7. 【转发】Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  8. chm帮助文档制作及C#调用

    http://zhoufoxcn.blog.51cto.com/792419/166049 http://wenku.baidu.com/view/a90adbd249649b6648d74794.h ...

  9. C#知识点备忘

    1.结构体不能用判断符号==判断是否为null,结构体是值类型,不论采用new与否,结构体中的值类型都已经赋了初值. 2.整数相除: a=; b=: c=a/b; 结果c= 如果想得到double型需 ...

  10. Python微信机器人

    Python微信机器人 本文目录 一 简介 二 登录微信 三 微信好友男女比例 四 微信好友地域分布 五 微信聊天机器人 一 简介 wxpy基于itchat,使用了 Web 微信的通讯协议,,通过大量 ...