Flask-SQLAlchemy错误
时间:2021-07-01 10:21:17
帮助过:3人阅读
flask
import Flask
from flask_sqlalchemy
import SQLAlchemy
from flask
import Response
from flask
import json, jsonify
app = Flask(
__name__)
app.config[‘SQLALCHEMY_DATABASE_URI‘] =
‘mysql+pymysql://root:123456@localhost/TestBlog‘
app.config[‘SQLALCHEMY_TRACK_MODIFICATIONS‘] =
True
db =
SQLAlchemy(app)
class User(db.Model):
__tablename__ =
‘t_users‘
id = db.Column(db.Integer, primary_key=
True)
username = db.Column(db.String(80), unique=
True)
password = db.Column(db.String(100
))
email = db.Column(db.String(120), unique=
True)
def __init__(self, username, password, email):
self.username =
username
self.password =
password
self.email =
email
def __repr__(self):
return ‘<User %r>‘ %
self.username
@app.route(‘/‘)
def hello_world():
# me = User(‘zxj‘)
# db.session.add(me)
return ‘{"name":"zxj"}‘
@app.route(‘/register‘, methods = [
‘POST‘])
def register():
data =
{
‘username‘:
‘张先生‘,
‘userID‘: 1
}
# js = json.dumps(data)
# resp = Response(js, status=200, mimetype=‘application/json‘)
udata = User(
‘admin‘,
‘password‘,
‘admin@admin.com‘)
db.session.add(udata)
db.session.commit()
return jsonify(data)
if __name__ ==
‘__main__‘:
app.run()
Flask-SQLAlchemy错误
标签: