Flask-SQLAlchemy错误
Posted 解忧杂货店
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask-SQLAlchemy错误相关的知识,希望对你有一定的参考价值。
感谢,http://www.zhihu.com/question/29719547
根据《Flask web开发...》这本书上操作连接数据一直有问题 ”No module named ‘mysqldb‘“
首先需要安装pip install PyMySQL
然后mysql://root:[email protected]/TestBlog 改为mysql+pymysql://root:[email protected]/TestBlog
下面是我测试通过的代码
from 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:[email protected]/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‘,‘[email protected]‘) db.session.add(udata) db.session.commit() return jsonify(data) if __name__ == ‘__main__‘: app.run()
以上是关于Flask-SQLAlchemy错误的主要内容,如果未能解决你的问题,请参考以下文章
使用 mysql 和烧瓶登录的 Flask-sqlalchemy 损坏管道错误 32
错误flask-sqlalchemy NameError:未定义全局名称'joinedload'
在 Flask-SQLAlchemy 模型上使用函数查询会给出 BaseQuery object is not callable 错误