from random import choicefrom flask import Flask, request, jsonifyfrom flask import render_templatefrom flask import Flaskfrom flask_sqlalchemy import SQLAlchemyimport pymysqlapp = Flask(__name__)# 相关配置app.config['SQLALCHEMY_DATAbase_URI'] = 'mysql://xxxxx:3306/stock_and_bond'app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Falseapp.config['SQLALCHEMY_ECHO'] = True# 创建组件对象pymysql.install_as_MySQLdb()db = SQLAlchemy(app)class BondData(db.Model): __tablename__ = "bond_table" id = db.Column(db.Integer, primary_key=True) bond_code = db.Column(db.String(100))@app.route("/")def login(): bonddata = BondData(bond_code="000002") db.session.add(bonddata) db.session.commit() return render_template("table.html")if __name__ == '__main__': app.run(debug=True)
flask_sqlalchemy依赖于pymysql所以需要单独安装,具体如下:
pip install flask-sqlalchemypip install pymysql
这样就可以正常使用了不会报错