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

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

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

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

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

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

但是看上去也没有问题:

  1. @app.route('/', methods=['GET', 'POST'])
  2. def index():
  3. form = NameForm()
  4. if form.validate_on_submit():
  5. sw = Switches.query.filter_by(sname=form.name.data).first() # filter the DB
  6. if sw is None:
  7. sw = sw(sname=form.name.data)
  8. db.session.add(sw)
  9. session['known'] = False
  10. else:
  11. session['known'] = True
  12. session['name'] = form.name.data
  13. return redirect(url_for('myindex'))
  14. return render_template('myindex.html', form=form, name=session.get('name'),
  15. known=session.get('known', False))

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

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

  1. You're supposed to initialize/create the tables first. Please read the Creating the Database article in the official Flask documentation:
  2. 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.
  3. Here's Flask's example of using a schema SQL script to create the database, tables, etc:
  4. sqlite3 /tmp/flaskr.db < schema.sql
  5. 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. 关于sencha touch 用phonegap打包后,docked悬停的组件被手机软键盘遮挡的解决方法

    这个问题应该算是phonegap的一个bug,在mainifest.xml 里android:windowSoftInputMode设置成了adjustpan,理论上不会出现遮挡悬停组件这种情况, 不 ...

  2. PHP获取POST的原始数据的方法

    一般我们都用$_POST或$_REQUEST两个预定义变量来接收POST提交的数据.但如果提交的数据没有变量名,而是直接的字符串,则需要使用其他的方式来接收. 方法一: 使用全局变量$GLOBALS[ ...

  3. ntpdate自动对准时间的脚本

    author:headsen  chen date: 2018-10-09  19:50:15 #!/bin/bash yum -y install ntpdate /usr/sbin/ntpdate ...

  4. Lucene4.x创建索引与3.x的一些不同

    lucene3.x的时候创建Field的时候可以直接指定存储和索引的选项类下下边这样: doc.add(new Field("createrId",diaryField.getCr ...

  5. 【BZOJ3275】Number 最小割

    [BZOJ3275]Number Description 有N个正整数,需要从中选出一些数,使这些数的和最大.若两个数a,b同时满足以下条件,则a,b不能同时被选1:存在正整数C,使a*a+b*b=c ...

  6. 【BZOJ4698】Sdoi2008 Sandy的卡片 后缀数组+RMQ

    [BZOJ4698]Sdoi2008 Sandy的卡片 Description Sandy和Sue的热衷于收集干脆面中的卡片.然而,Sue收集卡片是因为卡片上漂亮的人物形象,而Sandy则是为了积攒卡 ...

  7. 微信小程序 --- 文件的上传和下载

    文件上传 / 文件下载 : wx.uploadFile

  8. Oracle Database Memory Structures

    Oracle Database creates and uses memory structures for various purposes. For example, memory stores ...

  9. 有限制的最短路spfa+优先队列

    poj1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10751   Accepted: 3952 De ...

  10. 防止独立IP被其它恶意域名恶意解析

    一:什么是恶意域名解析 一般情况下,要使域名能访问到网站需要两步,第一步,将域名解析到网站所在的主机,第二步,在web服务器中将域名与相应的网站绑定.但是,如果通过主机IP能直接访问某网站,那么把域名 ...