参考: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. SignalR实现在线聊天室功能

    一.在线聊天室 1.新建解决方案 SignalROnlineChatDemo 2.新建MVC项目 SignalROnlineChatDemo.Web (无身份验证) 3.安装SignalR PM> ...

  2. C# DateTime.Now详细用法

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  3. 【BZOJ5082】弗拉格 矩阵乘法

    [BZOJ5082]弗拉格 Description “如果明天进了面试,我就去爆妹子的照”——有妹子的丁相允作为一个oier,自然不能立太多flag,让我们来看一道和flag有关的题目吧 给你n个fl ...

  4. 【BZOJ2957】楼房重建 分块

    [BZOJ2957]楼房重建 Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子 ...

  5. Windows Phone 在读取网络图片之前先显示默认图片

    1.新建一个控件WindowsPhoneControl1 WindowsPhoneControl1.xaml <UserControl x:Class="DefaultImage.Wi ...

  6. 170623、springboot编程之JdbcTemplate操作数据库

    使用JdbcTemplate操作mysql数据库! 1.在pom中引入jpa包 <dependency> <groupId>org.springframework.boot&l ...

  7. php 自带的过滤函数和转义函数

    函数名 释义 介绍 htmlspecialchars 将与.单双引号.大于和小于号化成HTML格式 &转成&"转成"' 转成'<转成<>转成> ...

  8. Linux--vim编辑器和文件恢复

    第五章  Vim编辑器和恢复ext4下误删除的文件-Xmanager工具 本节所讲内容: 5.1  vim的使用 5.2  实战:恢复ext4文件系统下误删除的文件 5.3  实战:使用xmanage ...

  9. Nmap介绍

    1.Nmap介绍 Nmap用于列举网络主机清单.管理服务升级调度.监控主机或服务运行状况.Nmap可以检测目标机是否在线.端口开放情况.侦测运行的服务类型及版本信息.侦测操作系统与设备类型等信息. 1 ...

  10. PHP面向对象详解:继承、封装与多态

    首先,在解释面向对象之前先解释下什么是面向对象? [面向对象]1.什么是类? 具有相同属性(特征)和方法(行为)的一系列个体的集合,类是一个抽象的概念2.什么是对象?从类中拿到的具有具体属性值得个体, ...