为了账号安全,请及时绑定邮箱和手机立即绑定

flask-sqlalchemy插入数据报错sqlalchemy.exc.OperationalError

flask-sqlalchemy插入数据报错sqlalchemy.exc.OperationalError

墨色风雨 2019-02-23 23:27:36
使用flask-sqlalchemy 操作mysql数据库时,注册页面填写的信息无法添加到mysql数据库中。代码如下: db2.py from flask import Flask from flask_sqlalchemy import SQLAlchemy import pymysql app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://comejack:123456@localhost:3306/flask' db = SQLAlchemy(app) class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(80), unique = True, nullable = False) password = db.Column(db.String(80), nullable = False) email = db.Column(db.String(120), unique = True, nullable = False) def __repr__(self): return '<User %r>' % self.username db.create_all() forms.py from wtforms.fields import BooleanField, TextField, StringField, PasswordField, SubmitField from wtforms import validators from wtforms.validators import DataRequired from flask_wtf import FlaskForm class RegForm(FlaskForm): username = StringField('username', validators = [DataRequired()]) password = PasswordField('password', validators = [DataRequired()]) email = StringField('email', validators = [DataRequired()]) submit = SubmitField('Sign Up') run.py # -*- coding:UTF-8 -*--- from flask import Flask, render_template, flash, redirect, request from forms import LoginForm, RegForm from flask_wtf.csrf import CSRFProtect from forms import * from db2 import * from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db.init_app(app) CSRFProtect(app) @app.route('/reg/', methods = ['GET','POST']) def reg(): form = RegForm() if form.validate_on_submit(): user1 = User(id = 1, username = form.username.data, password = form.password.data, email = form.email.data) print (user1) db.session.add(user1) db.session.commit() return render_template('reg.html', title = 'Sign up', form = form) app.run(host = '0.0.0.0', port = 9000, debug = True) reg.html {% extends "base.html" %} {% block content %} <h1>reg</h1> <form action="" method="POST" name="reg"> {{form.csrf_token}} <p>please enter your username:<br> {{form.username}} <br> </p> <p>please enter your password:<br> {{form.password}}<br> </p> <p>please enter your email:<br> {{ form.email }}<br> </p> <p>{{ form.submit }}</p> </form> {% endblock %} 运行python3 run.py后,在reg页面输入注册的用户名 密码 邮箱后。点击提交,出现报错sqlalchemy.exc.OperationalErrorsqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user [SQL: 'INSERT INTO user (id, username, password, email) VALUES (?, ?, ?, ?)'] [parameters: (1, '123123', '123123', 'c122@126.com')] (Background on this error at: http://sqlalche.me/e/e3q8) 请问如何解决。
查看完整描述

3 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

配置的是mysql, 报错的却是sqlite3, 你的数据库配置没有指对吧,倒底用的是什么库呢, db类如何定义的?
提示没有user这个表,需要核对下倒底有没有

查看完整回答
反对 回复 2019-03-01
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

亲,这个问题解决了么,遇到了同样的问题

查看完整回答
反对 回复 2019-03-01
  • 3 回答
  • 0 关注
  • 1199 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信