Flask-SLQAlchemy OperationalError:(sqlite3.OperationalError)没有这样的表

时间:2018-08-02 04:15:49

标签: python flask-sqlalchemy

我是数据库新手,因此,如果这太基础了,对不起,但是我对SO中的其他相关问题并不满意。

MODELS.py

import json

from app import db

class User(db.Model):
    __tablename__='user'
    """
    Model for storing user data. 
    """
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    address = db.Column(db.String(30), unique=True, nullable=False)

    def __init__(self, name, username, email, address):
        self.name = name
        self.username = username
        self.email = email
        self.address = address

    def __repr__(self):
        return '<User %r>' % (self.user)

我运行app.py时,会创建一个空的player.sqlite3

APP.py

from flask import Flask, flash, redirect, g, render_template, request, session, abort, url_for
from flask import stream_with_context, Response
from flask_mail import Mail,  Message
from flask_oauth import OAuth
from flask.ext.sqlalchemy import SQLAlchemy
from app.datafoo.db.models import *


app = Flask(__name__)

app.config.update(
    SQLALCHEMY_DATABASE_URI= "sqlite:///%s" % os.path.join(app.root_path, 'app/datafoo/db/data/player.sqlite3'),
    DEBUG=True)

db = SQLAlchemy(app)

@app.route('/login', methods=['POST'])
def do_admin_login():

    # check user password
    if request.form['username'] == 'john':
        session['logged_in'] = True

        db.create_all()
        u = User(name=request.form['user'], 
                 username=request.form['username'], 
                 email=request.form['email'],
                 address=None)
        db.session.add(u)
        db.session.commit()  

    else:
        flash('wrong password!')

if __name__ == '__main__':
    app.run(use_reloader=True, port=PORT, threaded=True, debug=True)

但是我得到了追溯:

  

OperationalError:(sqlite3.OperationalError)没有这样的表:用户[SQL:u'INSERT INTO用户(名称,用户名,电子邮件,地址)VALUES(?,?,?,?)'] [参数:(u' John',u'johnsusername',u'john @ mac.com',无)]

我在这里想念什么?

0 个答案:

没有答案