亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

flask-sqlalchemy插入數據報錯sqlalchemy.exc.OperationalError

flask-sqlalchemy插入數據報錯sqlalchemy.exc.OperationalError

Helenr 2019-05-21 10:56:23
使用flask-sqlalchemy操作mysql數據庫時,注冊頁面填寫的信息無法添加到mysql數據庫中。代碼如下:db2.pyfromflaskimportFlaskfromflask_sqlalchemyimportSQLAlchemyimportpymysqlapp=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.usernamedb.create_all()forms.pyfromwtforms.fieldsimportBooleanField,TextField,StringField,PasswordField,SubmitFieldfromwtformsimportvalidatorsfromwtforms.validatorsimportDataRequiredfromflask_wtfimportFlaskFormclassRegForm(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,requestfromformsimportLoginForm,RegFormfromflask_wtf.csrfimportCSRFProtectfromformsimport*fromdb2import*fromflask_sqlalchemyimportSQLAlchemyapp=Flask(__name__)app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=Truedb.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','[email protected]')](Backgroundonthiserrorat:http://sqlalche.me/e/e3q8)請問如何解決。
查看完整描述

2 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

配置的是mysql,報錯的卻是sqlite3,你的數據庫配置沒有指對吧,倒底用的是什么庫呢,db類如何定義的?提示沒有user這個表,需要核對下倒底有沒有
                            
查看完整回答
反對 回復 2019-05-21
  • 2 回答
  • 0 關注
  • 1009 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號