myapp.py
from flask import Flask
app = Flask(__name__)
app.config.from_object('config')
from view import *
if __name__ == '__main__':
app.run(debug=True)
view.py
from flask import render_template
from myapp import app
from model import db,User
@app.route('/test')
def test():
aaa=User(username='admin',password='admin',email='xxx')
db.session.add(aaa)
db.session.commit()
return 'commit user'
config.py
SQLALCHEMY_DATABASE_URL="mysql://%s:%s@%s/%s" % ('root', 'root', '127.0.0.1:3306', 'blog')
model.py
from flask.ext.sqlalchemy import SQLAlchemy
from myapp import app
db=SQLAlchemy(app)
class User(db.Model):
__tablename__='user'
id=db.Column(db.Integer,primary_key=True,unique=True)
username=db.Column(db.String(45))
password=db.Column(db.String(45))
email=db.Column(db.String(45))
def __repr__(self):
return '<User %r>' % self.username
运行myapp.py并访问/test
:
OperationalError:(sqlite3.OperationalError)没有这样的表用户
我已经创建了数据库和表