app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:19931218@localhost/test'app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = Truedb = SQLAlchemy(app)class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
def __repr__(self):
return '' % self.username
from sql_learn import db,User
import pymysql
db.create_all()#创建相应的表
#user_Susan = User(username = 'Susan',email = '6666663@qq.com')
#db.session.add(user_Susan)
#user_Susan.username = 'Susan_2'
#db.session.add(user_Susan)
#db.session.commit()
#print(db.session.query(User,User.id,User.username).all())#这里返回的是一个元组,每一个对象还是一个元组,包含User类,id,username
#print(User.query.filter_by(username = 'Susan').all())#flask的查询对象返回User的对象user = User.query.filter_by(username = 'Susan').first()user.username = 'Susan_wifi'db.session.add(user)
db.session.commit()