(1)安裝mysql
pip install flask_sqlalchemy
(2)數據庫鏈接sql
from flask import Flask # 1.導入 from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) #2.定義要鏈接的數據庫 DB_URI = "mysql+pymysql://root:123456@127.0.0.1:3306/learn_sqlalchemy?charset=utf8" #3.添加到到配置中 app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] =False # 4.實例化一個對象,將app傳進去 db = SQLAlchemy(app) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
(3)建立表數據庫
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) DB_URI = "mysql+pymysql://root:123456@127.0.0.1:3306/learn_sqlalchemy?charset=utf8" app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer,primary_key=True,autoincrement=True) username = db.Column(db.String(50),nullable=False) class Article(db.Model): __tablename__ = "article" id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(50), nullable=False) uid = db.Column(db.Integer,db.ForeignKey("user.id")) author = db.relationship("User",backref='article') db.drop_all() #刪除表 db.create_all() #建立表 @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
(4)添加數據flask
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) DB_URI = "mysql+pymysql://root:123456@127.0.0.1:3306/learn_sqlalchemy?charset=utf8" app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer,primary_key=True,autoincrement=True) username = db.Column(db.String(50),nullable=False) class Article(db.Model): __tablename__ = "article" id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(50), nullable=False) uid = db.Column(db.Integer,db.ForeignKey("user.id")) author = db.relationship("User",backref='article') # db.drop_all() #刪除表 # db.create_all() #建立表 #添加數據 user = User(username='derek') article = Article(title='今天5/27號') article.author = user db.session.add(article) db.session.commit() @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
(5)查看、修改和刪除session
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) DB_URI = "mysql+pymysql://root:123456@127.0.0.1:3306/learn_sqlalchemy?charset=utf8" app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer,primary_key=True,autoincrement=True) username = db.Column(db.String(50),nullable=False) def __repr__(self): return "<username:%s>"%self.username class Article(db.Model): __tablename__ = "article" id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(50), nullable=False) uid = db.Column(db.Integer,db.ForeignKey("user.id")) author = db.relationship("User",backref='article') # db.drop_all() #刪除表 # db.create_all() #建立表 # 1.添加數據 # user = User(username='derek') # article = Article(title='今天5/27號') # article.author = user # # db.session.add(article) # db.session.commit() # 2.查詢數據 # users=User.query.all() # print(users) # 3.修改數據,先查找出來再修改 # user = User.query.filter(User.username=='derek').first() # user.username = 'jack' # db.session.commit() # 4.刪除數據 user = User.query.filter(User.username=='jack').first() db.session.delete(user) db.session.commit() @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()