猿问

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

使用flask-sqlalchemy操作mysql数据库时,注册页面填写的信息无法添加到mysql数据库中。代码如下:
db2.py
fromflaskimportFlask
fromflask_sqlalchemyimportSQLAlchemy
importpymysql
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://comejack:123456@localhost:3306/flask'
db=SQLAlchemy(app)
classUser(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''%self.username
db.create_all()
forms.py
fromwtforms.fieldsimportBooleanField,TextField,StringField,PasswordField,SubmitField
fromwtformsimportvalidators
fromwtforms.validatorsimportDataRequired
fromflask_wtfimportFlaskForm
classRegForm(FlaskForm):
username=StringField('username',validators=[DataRequired()])
password=PasswordField('password',validators=[DataRequired()])
email=StringField('email',validators=[DataRequired()])
submit=SubmitField('SignUp')
run.py
#-*-coding:UTF-8-*---
fromflaskimportFlask,render_template,flash,redirect,request
fromformsimportLoginForm,RegForm
fromflask_wtf.csrfimportCSRFProtect
fromformsimport*
fromdb2import*
fromflask_sqlalchemyimportSQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True
db.init_app(app)
CSRFProtect(app)
@app.route('/reg/',methods=['GET','POST'])
defreg():
form=RegForm()
ifform.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()
returnrender_template('reg.html',title='Signup',form=form)
app.run(host='0.0.0.0',port=9000,debug=True)
reg.html
{%extends"base.html"%}
{%blockcontent%}

reg

{{form.csrf_token}}

pleaseenteryourusername:

{{form.username}}

pleaseenteryourpassword:

{{form.password}}

pleaseenteryouremail:

{{form.email}}

{{form.submit}}

{%endblock%}
运行python3run.py后,在reg页面输入注册的用户名密码邮箱后。点击提交,出现报错sqlalchemy.exc.OperationalErrorsqlalchemy.exc.OperationalError:(sqlite3.OperationalError)nosuchtable:user[SQL:'INSERTINTOuser(id,username,password,email)VALUES(?,?,?,?)'][parameters:(1,'123123','123123','c122@126.com')](Backgroundonthiserrorat:http://sqlalche.me/e/e3q8)
请问如何解决。
Helenr
浏览 994回答 2
2回答

芜湖不芜

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

相关分类

JavaScript
我要回答