参考: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. kubernetes创建yaml,pod服务一直处于 ContainerCreating状态的原因查找与解决

    最近刚刚入手研究kubernetes,运行容器的时候,发现一直处于ContainerCreating状态,悲了个催,刚入手就遇到了点麻烦,下面来讲讲如何查找问题及解决的 运行容器命令: kubectl ...

  2. Windows 2008 server R2安装.NET Framework4时提示“灾难性故障”

    报错信息: 安装.NET Framework 4时,提示安装未成功,“灾难性故障”.服务器的操作系统是windows  server 2008 R2. 查看系统日志时显示“无法安装 Windows 更 ...

  3. IOS项目分层

    上传者:踏浪帅 分类:其他(Others) 查看次数:408 下载次数:70 上传时间:2016-01-07 大小:3 KB 主项目中的分层主要包含四个模块,Main(主要).Expand(扩展).R ...

  4. springMVC 使用注解注入接口实现类

    spring常用的注释:   @Component:标准一个普通的spring Bean类. @Controller:标注一个控制器组件类. @Service:标注一个业务逻辑组件类. @Reposi ...

  5. spring boot tomcat 线程数 修改初始线程数 统计性能 每百次请求耗时

    [root@f java]# tail -30 nohup.outsearchES-TimeMillisSpent:448P->1602@fT->http-nio-8080-exec-3t ...

  6. Celery和Rabbitmq自学

    异步消息队列,也能用于定时和周期性任务.每次修改的task代码还要重启worker,这个有点麻烦 所有带task()装饰器的可调用对象(usertask)都是celery.app.task.Task类 ...

  7. MR的shuffle和Spark的shuffle之间的区别

    mr的shuffle mapShuffle 数据存到hdfs中是以块进行存储的,每一个块对应一个分片,maptask就是从分片中获取数据的 在某个节点上启动了map Task,map Task读取是通 ...

  8. Spark的Java API例子详解

    package com.hand.study; import scala.Tuple2; import org.apache.spark.SparkConf; import org.apache.sp ...

  9. 离线安装部署zabbix

    一. 安装好CentOS安装过程中添加php,mariadb等所需要的依赖 二. 准备好所有所需的rpm压缩文件包在centos中解压,这里放在根目录下zabbix_rpms文件夹下 三. 安装所需r ...

  10. Web Services 平台元素SOAP、WSDL 、UDDI

    Web Services 拥有三种基本的元素:SOAP.WSDL 以及 UDDI. 什么是 SOAP? SOAP 是一种使应用程序有能力通过 HTTP 交换信息的基于 XML 的简易协议.或者可以更简 ...