{"id":13,"date":"2025-07-27T19:14:15","date_gmt":"2025-07-27T11:14:15","guid":{"rendered":"https:\/\/22z.top\/?p=13"},"modified":"2025-07-27T19:14:16","modified_gmt":"2025-07-27T11:14:16","slug":"flask-sqlalchemy%e5%b8%b8%e7%94%a8%e6%95%b0%e6%8d%ae%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/22z.top\/?p=13","title":{"rendered":"Flask-SQLAlchemy\u5e38\u7528\u6570\u636e\u64cd\u4f5c"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Flask-SQLAlchemy<\/h2>\n\n\n\n<p>flask\u9ed8\u8ba4\u63d0\u4f9b\u6a21\u578b\u64cd\u4f5c\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u63d0\u4f9bORM\uff0c\u6240\u4ee5\u4e00\u822c\u5f00\u53d1\u7684\u65f6\u5019\u6211\u4eec\u4f1a\u91c7\u7528flask-SQLAlchemy\u6a21\u5757\u6765\u5b9e\u73b0ORM\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>SQLAlchemy\u662f\u4e00\u4e2apython\u8bed\u8a00\u7f16\u5199\u7684\u9ad8\u6027\u80fd\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93ORM\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u5c42\u7684 ORM \u548c\u5e95\u5c42\u7684\u539f\u751f\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528sqlalchemy \u4e0d\u9700\u8981\u8c03\u7528sqlalchemy \u672c\u8eab\u8fd9\u4e2a\u6a21\u5757\uff0c\u800c\u662f\u91c7\u7528flask-sqlalchemy \uff0c\u8fd9\u662f\u4e00\u4e2a\u7b80\u5316\u4e86 SQLAlchemy \u64cd\u4f5c\u7684flask\u6269\u5c55\u6a21\u5757\u3002\uff08\u4e3b\u8981\u662f\u7b80\u5316\u4e86sqlalchemy\u521d\u59cb\u5316\u4ee3\u7801\u548c\u5206\u9875\u64cd\u4f5c\u7b49\uff09<\/p>\n\n\n\n<p>SQLAlchemy\uff1a<a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/\">https:\/\/docs.sqlalchemy.org\/en\/14\/<\/a><\/p>\n\n\n\n<p>\u4e2d\u6587\u6587\u6863\uff1a<a href=\"https:\/\/www.osgeo.cn\/sqlalchemy\/orm\/index.html\">https:\/\/www.osgeo.cn\/sqlalchemy\/orm\/index.html<\/a><\/p>\n\n\n\n<p>flask-SQLAlchemy\uff1a<a href=\"https:\/\/flask-sqlalchemy.palletsprojects.com\/en\/2.x\/quickstart\/\">https:\/\/flask-sqlalchemy.palletsprojects.com\/en\/2.x\/quickstart\/<\/a><\/p>\n\n\n\n<p>\u5b89\u88c5 flask-sqlalchemy\u3010\u6e05\u534e\u6e90\u3011<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install flask-sqlalchemy -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/pre>\n\n\n\n<p>\u5982\u679csqlalchemy\u8fde\u63a5\u7684\u662f mysql \/MariaDB\u6570\u636e\u5e93\uff0c\u9700\u8981\u5b89\u88c5 mysqldb \u6216pymysql<strong>\u9a71\u52a8<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">conda install flask-mysqldb -c conda-forge<\/pre>\n\n\n\n<p>\u5b89\u88c5flask-mysqldb\u65f6\uff0c\u6ce8\u610f<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4f7f\u7528pip install \u5b89\u88c5 flask-mysqldb\u7684\u65f6\u5019\uff0cpython\u5e95\u5c42\u4f9d\u8d56\u4e8e\u4e00\u4e2a\u5e95\u5c42\u7684\u6a21\u5757 mysqlclient \u6a21\u5757<br>\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u6a21\u5757\uff0c\u5219\u4f1a\u62a5\u9519\u5982\u4e0b\uff1a<br>\u200b<br>Command \"python setup.py egg_info\" failed with error code 1 in \/tmp\/pip-install-21hysnd4\/mysqlclient\/<\/pre>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install -y libmysqlclient-dev python3-dev<br>\u200b<br># \u8fd0\u884c\u4e0a\u9762\u7684\u5b89\u88c5\u547d\u4ee4\u5982\u679c\u518d\u6b21\u62a5\u9519\u5982\u4e0b\uff1a<br># &nbsp; dpkg \u88ab\u4e2d\u65ad\uff0c\u60a8\u5fc5\u987b\u624b\u5de5\u8fd0\u884c \u2018sudo dpkg --configure -a\u2019 \u89e3\u51b3\u6b64\u95ee\u9898\u3002<br>\u200b<br># \u5219\u6839\u636e\u63d0\u793a\u6267\u884c\u547d\u4ee4\u4ee5\u4e0b\u547d\u4ee4\uff0c\u518d\u6b21\u5b89\u88c5mysqlclient<br># &nbsp;  sudo dpkg --configure -a<br># &nbsp;  apt-get install libmysqlclient-dev python3-dev<br>\u200b<br>\u89e3\u51b3\u4e86mysqlclient\u95ee\u9898\u4ee5\u540e\uff0c\u91cd\u65b0\u5b89\u88c5 flask-mysqldb\u5373\u53ef\u3002<br>pip install flask-mysqldb -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/pre>\n\n\n\n<p>\u539f\u751fSQLAlchemy\u8fde\u63a5\u914d\u7f6e\u64cd\u4f5c<\/p>\n\n\n\n<p>db.py\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import create_engine &nbsp; # \u9a71\u52a8\u5f15\u64ce<br>from sqlalchemy.ext.declarative import declarative_base # \u6570\u636e\u5e93\u57fa\u7c7b<br>from sqlalchemy import Column, Integer, String, Boolean, Numeric, Text # \u5b57\u6bb5\u3001\u6574\u578b<br>from sqlalchemy.orm import sessionmaker &nbsp;# \u8fde\u63a5\u4f1a\u8bdd<br>\u200b<br>engine = create_engine(<br> &nbsp; &nbsp;# \u8fde\u63a5\u6570\u636e\u5e93\u7684URL<br> &nbsp; &nbsp;# url=\"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/students?charset=utf8mb4\",  # \u5982\u679c\u5e95\u5c42\u9a71\u52a8\u662fpymysql<br> &nbsp; &nbsp;url=\"mysql:\/\/root:123@127.0.0.1:3306\/students?charset=utf8mb4\", &nbsp; &nbsp;# \u5982\u679c\u5e95\u5c42\u9a71\u52a8\u662fMysqlDB<br> &nbsp; &nbsp;echo=True, # \u5f53\u8bbe\u7f6e\u4e3aTrue\u65f6\u4f1a\u5c06orm\u8bed\u53e5\u8f6c\u5316\u4e3asql\u8bed\u53e5\u6253\u5370\uff0c\u4e00\u822cdebug\u7684\u65f6\u5019\u53ef\u7528<br> &nbsp; &nbsp;pool_size=8, # \u8fde\u63a5\u6c60\u7684\u5927\u5c0f\uff0c\u9ed8\u8ba4\u4e3a5\u4e2a\uff0c\u8bbe\u7f6e\u4e3a0\u65f6\u8868\u793a\u8fde\u63a5\u65e0\u9650\u5236<br> &nbsp; &nbsp;pool_recycle=60*30 # \u8bbe\u7f6e\u65f6\u95f4\u4ee5\u9650\u5236\u6570\u636e\u5e93\u591a\u4e45\u6ca1\u8fde\u63a5\u81ea\u52a8\u65ad\u5f00<br>)<br>\u200b<br>DbSession = sessionmaker(bind=engine)<br>session = DbSession()<br>\u200b<br># \u521b\u5efa\u6570\u636e\u57fa\u7c7b<br>Model = declarative_base()<\/pre>\n\n\n\n<p>manage.py\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import db<br>class Student(db.Model):<br> &nbsp; &nbsp;__tablename__ = \"tb_student\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True)<br> &nbsp; &nbsp;name = db.Column(db.String(255))<br> &nbsp; &nbsp;sex  = db.Column(db.Boolean)<br> &nbsp; &nbsp;age  = db.Column(db.Integer)<br> &nbsp; &nbsp;class_name = db.Column(\"class\", db.String(255),)<br> &nbsp; &nbsp;description = db.Column(db.Text)<br> &nbsp; &nbsp;is_delete = db.Column(db.Boolean, nullable=True, default=False)<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;# \u5982\u679c\u6ca1\u6709\u63d0\u524d\u58f0\u660e\u6a21\u578b\u4e2d\u7684\u6570\u636e\u8868\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u4ee3\u7801\u751f\u6210\u6570\u636e\u8868\uff0c<br> &nbsp; &nbsp;# \u5982\u679c\u6570\u636e\u5e93\u4e2d\u5df2\u7ecf\u58f0\u660e\u4e86\u6709\u6570\u636e\u8868\uff0c\u5219\u4e0d\u4f1a\u7ee7\u7eed\u751f\u6210<br> &nbsp; &nbsp;db.Model.metadata.create_all(db.engine)<br>\u200b<br> &nbsp; &nbsp;# \u6839\u636eID\u67e5\u8be2\u4e00\u6761\u6570\u636e<br> &nbsp; &nbsp;# ret = db.session.query(Student).get(19)<br> &nbsp; &nbsp;# print(ret)<br> &nbsp; &nbsp;# print(ret.name)<br> &nbsp; &nbsp;# print(ret.class_name)<br> &nbsp; &nbsp;#<br> &nbsp; &nbsp;# # \u67e5\u8be2\u6240\u6709\u6570\u636e<br> &nbsp; &nbsp;# data_list = db.session.query(Student).all()<br> &nbsp; &nbsp;# print(data_list)<br> &nbsp; &nbsp;# for data in data_list:<br> &nbsp; &nbsp;# &nbsp; &nbsp; print(data.name)<br>\u200b<br> &nbsp; &nbsp;# # \u6309\u6761\u4ef6\u67e5\u8be2<br> &nbsp; &nbsp;# data_list = db.session.query(Student).filter(Student.sex==True, Student.class_name=='301').all()<br> &nbsp; &nbsp;# print(data_list)<br> &nbsp; &nbsp;# for data in data_list:<br> &nbsp; &nbsp;# &nbsp; &nbsp; print(data.sex, data.name)<br>\u200b<br> &nbsp; &nbsp;# \u6dfb\u52a0\u4e00\u6761\u6570\u636e<br> &nbsp; &nbsp;# student = Student(<br> &nbsp; &nbsp;# &nbsp; &nbsp; name=\"xiaohua\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; class_name=\"305\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; sex=False,<br> &nbsp; &nbsp;# &nbsp; &nbsp; age=18,<br> &nbsp; &nbsp;# &nbsp; &nbsp; description=\"\u7f8e\u7f8e\u7684..\",<br> &nbsp; &nbsp;# )<br> &nbsp; &nbsp;# db.session.add(student)<br> &nbsp; &nbsp;# db.session.commit()<br> &nbsp; &nbsp;#<br>\u200b<br> &nbsp; &nbsp;# # \u6dfb\u52a0\u591a\u6761\u6570\u636e<br> &nbsp; &nbsp;# student_list = [<br> &nbsp; &nbsp;# &nbsp; &nbsp; Student(name=\"xiaohei\", class_name=\"305\", sex=False, age=18, description=\"\u7f8e\u7f8e\u7684..\",),<br> &nbsp; &nbsp;# &nbsp; &nbsp; Student(name=\"xiaobai\", class_name=\"304\", sex=True, age=18, description=\"\u7f8e\u7f8e\u7684..\",),<br> &nbsp; &nbsp;# &nbsp; &nbsp; Student(name=\"xiaohui\", class_name=\"303\", sex=False, age=18, description=\"\u7f8e\u7f8e\u7684..\",),<br> &nbsp; &nbsp;# ]<br> &nbsp; &nbsp;#<br> &nbsp; &nbsp;# db.session.add_all(student_list)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# \u66f4\u65b0\u4e00\u6761\u6570\u636e<br> &nbsp; &nbsp;# student = db.session.query(Student).get(35)<br> &nbsp; &nbsp;# student.name = \"\u5c0f\u4f1a\"<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# \u66f4\u65b0\u591a\u6761\u6570\u636e<br> &nbsp; &nbsp;# db.session.query(Student).filter(Student.class_name==\"303\").update({Student.age:Student.age+1})<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br>\u200b<br> &nbsp; &nbsp;# \u5220\u9664\u4e00\u6761\u6570\u636e<br> &nbsp; &nbsp;# student = db.session.query(Student).get(35)<br> &nbsp; &nbsp;# db.session.delete(student)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# \u5220\u9664\u591a\u6761\u6570\u636e<br> &nbsp; &nbsp;db.session.query(Student).filter(Student.class_name == \"401\").delete()<br>\u200b<br> &nbsp; &nbsp;# \u539f\u751fSQL\u8bed\u53e5<br> &nbsp; &nbsp;# \u8bfb<br> &nbsp; &nbsp;cursor = db.session.execute('select * from tb_student')<br> &nbsp; &nbsp;# \u4e00\u6761<br> &nbsp; &nbsp;# data = cursor.fetchone()<br> &nbsp; &nbsp;# print(data)<br>\u200b<br> &nbsp; &nbsp;# \u591a\u6761<br> &nbsp; &nbsp;# data_list = cursor.fetchall()<br> &nbsp; &nbsp;# print(data_list)<br>\u200b<br> &nbsp; &nbsp;# \u5199[\u6dfb\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539]<br> &nbsp; &nbsp;cursor = db.session.execute(<br> &nbsp; &nbsp; &nbsp; &nbsp;'insert into tb_student(name, class, age, sex, description) values(:name, :class, :age, :sex, :description)',<br> &nbsp; &nbsp; &nbsp; &nbsp;params={<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name\": \"xiaohong\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"class\": \"307\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"age\": 19,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"sex\": 0,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"description\": \".....\",<br> &nbsp; &nbsp; &nbsp;  })<br> &nbsp; &nbsp;db.session.commit()<br> &nbsp; &nbsp;print(cursor.lastrowid) # \u83b7\u53d6\u6700\u540e\u6dfb\u52a0\u7684\u4e3b\u952eID<br>\u200b<br>\u200b<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u636e\u5e93\u8fde\u63a5\u8bbe\u7f6e<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 Flask-SQLAlchemy \u4e2d\uff0c\u6570\u636e\u5e93\u7684\u94fe\u63a5\u914d\u7f6e\u4fe1\u606f\u4f7f\u7528URL\u6307\u5b9a\uff0c\u800c\u4e14\u7a0b\u5e8f\u4f7f\u7528\u7684\u6570\u636e\u5e93\u5fc5\u987b\u4fdd\u5b58\u5230Flask\u7684 <strong>SQLALCHEMY_DATABASE_URI<\/strong> \u914d\u7f6e\u9879\u4e2d<\/li>\n<\/ul>\n\n\n\n<p>manage.py\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># SQLAlchemy\u7684\u94fe\u63a5\u914d\u7f6e\uff1a\"\u6570\u636e\u5e93\u540d:\/\/\u8d26\u6237\u540d:\u5bc6\u7801@\u670d\u52a1\u5668\u5730\u5740:\u7aef\u53e3\/\u6570\u636e\u5e93\u540d\u79f0?\u914d\u7f6e\u53c2\u6570\u9009\u9879\"<br>app.config[\"SQLALCHEMY_DATABASE_URI\"] = \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br>\u200b<br>\u200b<br># \u5982\u679c\u4e0d\u4f7f\u7528mysqldb\u6539\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br># app.config[\"SQLALCHEMY_DATABASE_URI\"] = \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<\/pre>\n\n\n\n<p>\u5176\u4ed6\u8bbe\u7f6e\u9879\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a<br>SQLALCHEMY_TRACK_MODIFICATIONS = True<br>#\u67e5\u8be2\u65f6\u4f1a\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5<br>SQLALCHEMY_ECHO = True<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u914d\u7f6e\u5b8c\u6210\u9700\u8981\u53bb MySQL \u4e2d\u521b\u5efa\u9879\u76ee\u6240\u4f7f\u7528\u7684\u6570\u636e\u5e93<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql -uroot -p123<br>create database flaskdemo charset=utf8mb4;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u7528\u7684SQLAlchemy\u5b57\u6bb5\u7c7b\u578b<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6a21\u578b\u5b57\u6bb5\u7c7b\u578b\u540d<\/th><th>python\u4e2d\u6570\u636e\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><strong>Integer<\/strong><\/td><td>int<\/td><td>\u666e\u901a\u6574\u6570\uff0c\u4e00\u822c\u662f32\u4f4d<\/td><\/tr><tr><td><strong>SmallInteger<\/strong><\/td><td>int<\/td><td>\u53d6\u503c\u8303\u56f4\u5c0f\u7684\u6574\u6570\uff0c\u4e00\u822c\u662f16\u4f4d<\/td><\/tr><tr><td>BigInteger<\/td><td>int<\/td><td>\u4e0d\u9650\u5236\u7cbe\u5ea6\u7684\u6574\u6570<\/td><\/tr><tr><td>Float<\/td><td>float<\/td><td>\u6d6e\u70b9\u6570<\/td><\/tr><tr><td><strong>Numeric<\/strong><\/td><td>decimal.Decimal<\/td><td>\u666e\u901a\u6570\u503c\uff0c\u4e00\u822c\u662f32\u4f4d<\/td><\/tr><tr><td><strong>String<\/strong><\/td><td>str<\/td><td>\u53d8\u957f\u5b57\u7b26\u4e32<\/td><\/tr><tr><td><strong>Text<\/strong><\/td><td>str<\/td><td>\u53d8\u957f\u5b57\u7b26\u4e32\uff0c\u5bf9\u8f83\u957f\u6216\u4e0d\u9650\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u505a\u4e86\u4f18\u5316<\/td><\/tr><tr><td>Unicode<\/td><td>unicode<\/td><td>\u53d8\u957fUnicode\u5b57\u7b26\u4e32<\/td><\/tr><tr><td>UnicodeText<\/td><td>unicode<\/td><td>\u53d8\u957fUnicode\u5b57\u7b26\u4e32\uff0c\u5bf9\u8f83\u957f\u6216\u4e0d\u9650\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u505a\u4e86\u4f18\u5316<\/td><\/tr><tr><td><strong>Boolean<\/strong><\/td><td>bool<\/td><td>\u5e03\u5c14\u503c<\/td><\/tr><tr><td><strong>DateTime<\/strong><\/td><td>datetime.datetime<\/td><td>\u65e5\u671f\u548c\u65f6\u95f4<\/td><\/tr><tr><td>Date<\/td><td>datetime.date<\/td><td>\u65e5\u671f<\/td><\/tr><tr><td>Time<\/td><td>datetime.time<\/td><td>\u65f6\u95f4<\/td><\/tr><tr><td>LargeBinary<\/td><td>bytes<\/td><td>\u4e8c\u8fdb\u5236\u6587\u4ef6\u5185\u5bb9<\/td><\/tr><tr><td><strong>Enum<\/strong><\/td><td>enum.Enum<\/td><td>\u679a\u4e3e\u7c7b\u578b\uff0c\u76f8\u5f53\u4e8edjango\u7684choices\uff0c\u4f46\u662f\u529f\u80fd\u6ca1\u6709choices\u90a3\u4e48\u5f3a\u5927<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u7528\u7684SQLAlchemy\u5217\u7ea6\u675f\u9009\u9879<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9009\u9879\u540d<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>primary_key<\/td><td>\u5982\u679c\u4e3aTrue\uff0c\u4ee3\u8868\u5f53\u524d\u6570\u636e\u8868\u7684\u4e3b\u952e<\/td><\/tr><tr><td>unique<\/td><td>\u5982\u679c\u4e3aTrue\uff0c\u4e3a\u8fd9\u5217\u521b\u5efa\u552f\u4e00 \u7d22\u5f15\uff0c\u4ee3\u8868\u8fd9\u5217\u4e0d\u5141\u8bb8\u51fa\u73b0\u91cd\u590d\u7684\u503c<\/td><\/tr><tr><td>index<\/td><td>\u5982\u679c\u4e3aTrue\uff0c\u4e3a\u8fd9\u5217\u521b\u5efa\u666e\u901a\u7d22\u5f15\uff0c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387<\/td><\/tr><tr><td>nullable<\/td><td>\u5982\u679c\u4e3aTrue\uff0c\u5141\u8bb8\u6709\u7a7a\u503c\uff0c\u5982\u679c\u4e3aFalse\uff0c\u4e0d\u5141\u8bb8\u6709\u7a7a\u503c<\/td><\/tr><tr><td>default<\/td><td>\u4e3a\u8fd9\u5217\u5b9a\u4e49\u9ed8\u8ba4\u503c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u6570\u636e\u5e93\u57fa\u672c\u64cd\u4f5c<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728SQLAlchemy\u4e2d\uff0c\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664\u64cd\u4f5c\uff0c\u5747\u7531\u6570\u636e\u5e93\u4f1a\u8bdd(sessionSM)\u7ba1\u7406\u3002\n<ul class=\"wp-block-list\">\n<li>\u4f1a\u8bdd\u7528 db.session \u8868\u793a\u3002\u5728\u51c6\u5907\u628a\u6570\u636e\u5199\u5165\u6570\u636e\u5e93\u524d\uff0c\u8981\u5148\u5c06\u6570\u636e\u6dfb\u52a0\u5230\u4f1a\u8bdd\u4e2d\u7136\u540e\u8c03\u7528 db.commit() \u65b9\u6cd5\u63d0\u4ea4\u4f1a\u8bdd\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5728SQLAlchemy \u4e2d\uff0c\u67e5\u8be2\u64cd\u4f5c\u662f\u901a\u8fc7 query \u5bf9\u8c61\u64cd\u4f5c\u6570\u636e\u3002\n<ul class=\"wp-block-list\">\n<li>\u6700\u57fa\u672c\u7684\u67e5\u8be2\u662f\u8fd4\u56de\u8868\u4e2d\u6240\u6709\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7filter\u8fc7\u6ee4\u5668\u8fdb\u884c\u66f4\u7cbe\u786e\u7684\u6570\u636e\u5e93\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u578b\u7c7b\u5b9a\u4e49<\/h2>\n\n\n\n<p>\u6211\u4eec\u540e\u9762\u4f1a\u628a\u6a21\u578b\u521b\u5efa\u5230\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\uff0c\u4f46\u662f\u73b0\u5728\u6211\u4eec\u5148\u628a\u6a21\u578b\u7c7b\u5199\u5728main.py\u6587\u4ef6\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>\u200b<br>app = Flask(__name__)<br>class Config(object):<br> &nbsp; &nbsp;DEBUG = True<br> &nbsp; &nbsp;# \u6570\u636e\u5e93\u94fe\u63a5\u914d\u7f6e = \u6570\u636e\u5e93\u540d\u79f0:\/\/\u767b\u5f55\u8d26\u53f7:\u767b\u5f55\u5bc6\u7801@\u6570\u636e\u5e93\u4e3b\u673aIP:\u6570\u636e\u5e93\u8bbf\u95ee\u7aef\u53e3\/\u6570\u636e\u5e93\u540d\u79f0?charset=\u7f16\u7801\u7c7b\u578b<br> &nbsp; &nbsp;SQLALCHEMY_DATABASE_URI = \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a<br> &nbsp; &nbsp;SQLALCHEMY_TRACK_MODIFICATIONS = False<br> &nbsp; &nbsp;# ORM\u8fd0\u884c\u65f6\u4f1a\u663e\u793aORM\u751f\u6210\u7684\u539f\u59cbSQL\u8bed\u53e5[\u8c03\u8bd5]<br> &nbsp; &nbsp;SQLALCHEMY_ECHO = True<br>\u200b<br>app.config.from_object(Config)<br>\u200b<br>\u200b<br>\"\"\"\u6a21\u578b\u7c7b\u5b9a\u4e49\"\"\"<br>db = SQLAlchemy(app=app)<br># \u7b49\u540c\u4e8e<br># db = SQLAlchemy()<br># db.init_app(app) # \u52a0\u8f7d\u914d\u7f6e\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u4fe1\u606f\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;# \u58f0\u660e\u4e0e\u5f53\u524d\u6a21\u578b\u7ed1\u5b9a\u7684\u6570\u636e\u8868\u540d\u79f0<br> &nbsp; &nbsp;__tablename__ = \"tb_student\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u4f01\u4e1a\u4e2d\uff0c\u5f80\u5f80\u5927\u90e8\u5206\u7684\u516c\u53f8\u4f1a\u51fa\u73b0\u4ee5\u4e0b2\u79cd\u4e0d\u540c\u7684\u6570\u636e\u5e93\u5f00\u53d1\u60c5\u51b5\u3002<br> &nbsp;  # 1. \u4f01\u4e1a\u4e2d\u6709DBA\uff0cDBA\u4f1a\u63d0\u524d\u521b\u5efa\u6570\u636e\u5e93\u548c\u9879\u76ee\u4e2d\u5177\u4f53\u4e1a\u52a1\u7684\u6570\u636e\u8868\u3002<br> &nbsp; &nbsp; &nbsp; &nbsp; \u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u624b\u52a8\u5efa\u5e93\u5efa\u8868\uff0c\u53ea\u9700\u8981\u6839\u636e\u6570\u636e\u5e93\u8868\u7ed3\u6784\uff0c\u4f7f\u7528python\u58f0\u660e\u5bf9\u5e94\u7684\u6a21\u578b\u4e0e\u4e4b\u5339\u914d\uff0c\u5c31\u53ef\u4ee5\u64cd\u4f5c\u6570\u636e\u5e93\u4e86\u3002<br> &nbsp;  # 2. \u4f01\u4e1a\u6ca1\u6709DBA\uff0c\u6bd4\u8f83\u5751\u7239\uff1a<br> &nbsp;  # &nbsp;  2.1 \u5f00\u53d1\u4eba\u5458\uff0c\u81ea\u5df1\u624b\u64b8SQL\u8bed\u53e5\uff0c\u624b\u52a8\u5efa\u5e93\u5efa\u8868\u3002<br> &nbsp;  # &nbsp;  2.2 \u5f00\u53d1\u4eba\u5458\uff0c\u7f16\u5199\u6a21\u578b\uff0c\u4f7f\u7528\u6570\u636e\u8fc1\u79fb\uff0c\u624b\u52a8\u5efa\u5e93\u548c\u6570\u636e\u8fc1\u79fb\u5efa\u8868\u3002<br> &nbsp; &nbsp;<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_student(<br> &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp;  name varchar(15) comment \"\u59d3\u540d\",<br> &nbsp; &nbsp;  age smallint comment \"\u5e74\u9f84\",<br> &nbsp; &nbsp;  sex tinyint default 1 comment \"\u6027\u522b\",<br> &nbsp; &nbsp;  email varchar(128) comment \"\u90ae\u7bb1\u5730\u5740\",<br> &nbsp; &nbsp;  money NUMERIC(10,2) default 0.0 comment \"\u94b1\u5305\",<br> &nbsp; &nbsp;  key (name),<br> &nbsp; &nbsp;  unique key (email)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True,comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), index=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.Boolean, default=True, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(128), unique=True, comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10,2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self): # \u76f8\u5f53\u4e8edjango\u7684__str__<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>\u200b<br># \u6240\u6709\u7684\u6a21\u578b\u5fc5\u987b\u76f4\u63a5\u6216\u95f4\u63a5\u7ee7\u627f\u4e8edb.Model<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_course\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_course (<br> &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp;  name varchar(64) comment \"\u8bfe\u7a0b\",<br> &nbsp; &nbsp; &nbsp;  price NUMERIC(7,2) comment \"\u4ef7\u683c\",<br> &nbsp; &nbsp; &nbsp;  unique (name)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2), comment=\"\u4ef7\u683c\")<br> &nbsp; &nbsp;# repr()\u65b9\u6cd5\u7c7b\u4f3c\u4e8edjango\u7684__str__\uff0c\u7528\u4e8e\u6253\u5370\u6a21\u578b\u5bf9\u8c61\u65f6\u663e\u793a\u7684\u5b57\u7b26\u4e32\u4fe1\u606f<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>class Teacher(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8001\u5e08\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_teacher\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_teacher (<br> &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp;  name varchar(64) comment \"\u59d3\u540d\",<br> &nbsp; &nbsp; &nbsp;  option enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\") comment \"\u804c\u4f4d\",<br> &nbsp; &nbsp; &nbsp;  unique (`name`)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;option = db.Column(db.Enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\"), default=\"\u8bb2\u5e08\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u6570\u636e\u8868\u64cd\u4f5c<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa\u548c\u5220\u9664\u8868<\/h3>\n\n\n\n<p>\u521b\u5efa\u8868<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5728\u89c6\u56fe\u5185\u8c03\u7528\uff1a<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br># \u5728\u89c6\u56fe\u4ee5\u5916\u7684\u5730\u65b9\u8c03\u7528\uff1a<br>    with app.app_context():<br> &nbsp; &nbsp; &nbsp; &nbsp;# create_all()\u65b9\u6cd5\u6267\u884c\u7684\u65f6\u5019\uff0c\u9700\u8981\u653e\u5728\u6a21\u578b\u7684\u540e\u9762<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u68c0\u6d4b\u6570\u636e\u5e93\u4e2d\u662f\u5426\u5b58\u5728\u548c\u6a21\u578b\u5339\u914d\u7684\u6570\u636e\u8868\u3002<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u5982\u679c\u6ca1\u6709\uff0c\u5219\u6839\u636e\u6a21\u578b\u8f6c\u6362\u7684\u5efa\u8868\u8bed\u53e5\u8fdb\u884c\u5efa\u8868\u3002<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u5982\u679c\u627e\u5230\uff0c\u5219\u4e0d\u4f1a\u8fdb\u884c\u989d\u5916\u5904\u7406<br> &nbsp; &nbsp; &nbsp; &nbsp;db.create_all()<\/pre>\n\n\n\n<p>\u5220\u9664\u8868<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5728\u89c6\u56fe\u5185\u8c03\u7528\uff1a<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp; # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br># \u5728\u89c6\u56fe\u4ee5\u5916\u7684\u5730\u65b9\u8c03\u7528\uff1a<br> &nbsp; &nbsp;with app.app_context():<br> &nbsp; &nbsp; &nbsp; &nbsp;db.drop_all() &nbsp;# \u614e\u7528\uff0c\u5f88\u7ed9\u529b\u7684\uff01\uff01\u8fd9\u8868\u793a\u5220\u9664\u6570\u636e\u5e93\u4e2d\u6240\u6709\u6a21\u578b\u5bf9\u5e94\u7684\u8868\u3002<\/pre>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask, render_template<br>from flask_sqlalchemy import SQLAlchemy<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u7ba1\u7406\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_student\" # \u8868\u540d<br> &nbsp; &nbsp;# __abstract__ = True &nbsp; &nbsp; &nbsp;  # \u62bd\u8c61\u6a21\u578b\uff0c\u6570\u636e\u8fc1\u79fb\/\u5efa\u8868\u7684\u65f6\u5019\uff0c\u4e0d\u4f1a\u8ba4\u4e3a\u8fd9\u662f\u4e00\u4e2a\u6a21\u578b\uff0c\u4e5f\u5c31\u4e0d\u4f1a\u5efa\u8868\uff0c\u5f80\u5f80\u7528\u4e8e\u8bbe\u7f6e\u516c\u5171\u6a21\u578b\uff0c\u4fdd\u5b58\u516c\u5171\u5b57\u6bb5<br> &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  # \u4f01\u4e1a\u4e2d\uff0c\u5f80\u5f80\u5927\u90e8\u5206\u7684\u516c\u53f8\u4f1a\u51fa\u73b0\u4ee5\u4e0b2\u79cd\u4e0d\u540c\u7684\u6570\u636e\u5e93\u5f00\u53d1\u60c5\u51b5\u3002<br> &nbsp; &nbsp; &nbsp;  # 1. \u4f01\u4e1a\u4e2d\u6709DBA\uff0cDBA\u4f1a\u63d0\u524d\u521b\u5efa\u6570\u636e\u5e93\u548c\u9879\u76ee\u4e2d\u5177\u4f53\u4e1a\u52a1\u7684\u6570\u636e\u8868\u3002<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u4e0d\u9700\u8981\u81ea\u5df1\u624b\u52a8\u5efa\u5e93\u5efa\u8868\uff0c\u53ea\u9700\u8981\u6839\u636e\u6570\u636e\u5e93\u8868\u7ed3\u6784\uff0c\u4f7f\u7528python\u58f0\u660e\u5bf9\u5e94\u7684\u6a21\u578b\u4e0e\u4e4b\u5339\u914d\uff0c\u5c31\u53ef\u4ee5\u64cd\u4f5c\u6570\u636e\u5e93\u4e86\u3002<br> &nbsp; &nbsp; &nbsp;  # 2. \u4f01\u4e1a\u6ca1\u6709DBA\uff0c\u6bd4\u8f83\u5751\u7239\uff1a<br> &nbsp; &nbsp; &nbsp;  # &nbsp;  2.1 \u5f00\u53d1\u4eba\u5458\uff0c\u81ea\u5df1\u624b\u64b8SQL\u8bed\u53e5\uff0c\u624b\u52a8\u5efa\u5e93\u5efa\u8868\u3002<br> &nbsp; &nbsp; &nbsp;  # &nbsp;  2.2 \u5f00\u53d1\u4eba\u5458\uff0c\u7f16\u5199\u6a21\u578b\uff0c\u4f7f\u7528\u6570\u636e\u8fc1\u79fb\u6216\u8005ORM\u63d0\u4f9b\u5efa\u8868\u65b9\u6cd5\uff0c\u624b\u52a8\u5efa\u5e93\u548c\u6570\u636e\u8fc1\u79fb\u5efa\u8868\u3002<br> &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp; &nbsp; &nbsp;  create table db_student(<br> &nbsp; &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp; &nbsp;  name varchar(15) comment \"\u59d3\u540d\",<br> &nbsp; &nbsp; &nbsp; &nbsp;  age smallint comment \"\u5e74\u9f84\",<br> &nbsp; &nbsp; &nbsp; &nbsp;  sex tinyint(1) default 1 comment \"\u6027\u522b\",<br> &nbsp; &nbsp; &nbsp; &nbsp;  email varchar(255) comment \"\u90ae\u7bb1\u5730\u5740\",<br> &nbsp; &nbsp; &nbsp; &nbsp;  money NUMERIC(10,2) default 0.0 comment \"\u94b1\u5305\",<br> &nbsp; &nbsp; &nbsp; &nbsp;  key (name),<br> &nbsp; &nbsp; &nbsp; &nbsp;  unique key (email)<br> &nbsp; &nbsp; &nbsp;  );<br> &nbsp; &nbsp; &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;# \u5c5e\u6027\u540d = db.Column(\u5b57\u6bb5\u7c7b\u578b, \u5b57\u6bb5\u5217\u7ea6\u675f\u9009\u9879)<br> &nbsp; &nbsp;# \u5982\u679cSQL\u8bed\u53e5\u4e2d\u7684\u5b57\u6bb5\u540d\u5728python\u4e2d\u662f\u5173\u952e\u5b57\/\u4fdd\u7559\u5b57\uff0c\u5219\u5efa\u8bae\u6539\u5199\u7ed1\u5b9a\u5b57\u6bb5\u540d<br> &nbsp; &nbsp;# \u5c5e\u6027\u540d = db.Column(\"\u5b57\u6bb5\u540d\", \u5b57\u6bb5\u7c7b\u578b, \u5b57\u6bb5\u5217\u7ea6\u675f\u9009\u9879)<br> &nbsp; &nbsp;id = db.Column(\"student_id\", db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), index=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.SmallInteger, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(255), unique=True, comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10,2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;# repr()\u65b9\u6cd5\u7c7b\u4f3c\u4e8edjango\u7684__str__\uff0c\u7528\u4e8e\u6253\u5370\u6a21\u578b\u5bf9\u8c61\u65f6\u663e\u793a\u7684\u5b57\u7b26\u4e32\u4fe1\u606f<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br># \u6240\u6709\u7684\u6a21\u578b\u5fc5\u987b\u76f4\u63a5\u6216\u95f4\u63a5\u7ee7\u627f\u4e8edb.Model<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_course\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_course (<br> &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp;  name varchar(64) comment \"\u8bfe\u7a0b\",<br> &nbsp; &nbsp; &nbsp;  price NUMERIC(7,2) comment \"\u4ef7\u683c\",<br> &nbsp; &nbsp; &nbsp;  unique (name)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2), comment=\"\u4ef7\u683c\")<br> &nbsp; &nbsp;# repr()\u65b9\u6cd5\u7c7b\u4f3c\u4e8edjango\u7684__str__\uff0c\u7528\u4e8e\u6253\u5370\u6a21\u578b\u5bf9\u8c61\u65f6\u663e\u793a\u7684\u5b57\u7b26\u4e32\u4fe1\u606f<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>class Teacher(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8001\u5e08\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_teacher\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_teacher (<br> &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp;  name varchar(64) comment \"\u59d3\u540d\",<br> &nbsp; &nbsp; &nbsp;  option enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\") comment \"\u804c\u4f4d\",<br> &nbsp; &nbsp; &nbsp;  unique (`name`)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;option = db.Column(db.Enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\"), default=\"\u8bb2\u5e08\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;title = \"\u7f51\u7ad9\u9996\u9875\"<br> &nbsp; &nbsp;return render_template(\"index.html\", **locals())<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp; # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<br>\u200b<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u6570\u636e\u64cd\u4f5c<\/h1>\n\n\n\n<p>\u6dfb\u52a0\u4e00\u6761\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u6dfb\u52a0\u4e00\u6761\u6570\u636e<br>student = Student(name=\"\u5c0f\u660e\", age=17, email=\"xiaoming@qq.com\", money=100) # \u5b9e\u4f8b\u5316\u6a21\u578b\u5bf9\u8c61<br>db.session.add(student) # \u628a\u6a21\u578b\u5bf9\u8c61\u6dfb\u52a0\u6570\u636e\u5e93session\u4f1a\u8bdd\u5bf9\u8c61\u4e2d\u3002db.session\u662fSQLAlchemy\u4e2d\u5185\u7f6e\u7684\u4f1a\u8bdd\u7ba1\u7406\u5bf9\u8c61sessionSM\u7684\u6210\u5458<br>db.session.commit() &nbsp; &nbsp; # \u63d0\u4ea4\u4f1a\u8bdd<br>\u200b<br># \u518d\u6b21\u63d2\u5165\u4e00\u6761\u6570\u636e<br>student2 = Student(name='\u5c0f\u7ea2', sex=False, age=13, email=\"16565666@qq.com\", money=600)<br>db.session.add(student2)<br>db.session.commit() &nbsp; &nbsp; # \u63d0\u4ea4\u4f1a\u8bdd<\/pre>\n\n\n\n<p>\u4e00\u6b21\u63d2\u5165\u591a\u6761\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># 1. \u5148\u5b9e\u4f8b\u5316\u8981\u521b\u5efa\u7684\u6a21\u578b\u5bf9\u8c61<br>student = Student(name=\"\u5c0f\u7ea2\", age=17, email=\"xiaohong@qq.com\", money=200)<br># 2. \u628a\u5b9e\u4f8b\u5bf9\u8c61\u6dfb\u52a0\u5230\u8fde\u63a5\u4f1a\u8bdd<br>db.session.add(student)<br># 1. \u5148\u5b9e\u4f8b\u5316\u8981\u521b\u5efa\u7684\u6a21\u578b\u5bf9\u8c61<br>student = Student(name=\"\u5c0f\u82b1\", age=16, email=\"xiaohua@qq.com\", money=200)<br># 2. \u628a\u5b9e\u4f8b\u5bf9\u8c61\u6dfb\u52a0\u5230\u8fde\u63a5\u4f1a\u8bdd<br>db.session.add(student)<br># 3. \u53ea\u9700\u8981\u5728\u7ed3\u675f\u7684\u65f6\u5019\u63d0\u4ea4\u4e8b\u52a1\u5373\u53ef<br>db.session.commit()<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"># 1. \u5148\u521b\u5efa\u7684\u5217\u8868\u8981\u6dfb\u52a0\u7684\u5b9e\u4f8b\u5316\u6a21\u578b\u5bf9\u8c61\u5217\u8868<br>student_list = [<br> &nbsp; &nbsp;Student(name='wang', email='wang@163.com', age=20),<br> &nbsp; &nbsp;Student(name='zhang', email='zhang@189.com', age=21),<br> &nbsp; &nbsp;Student(name='chen', email='chen@126.com', age=19),<br> &nbsp; &nbsp;Student(name='zhou', email='zhou@163.com', age=18),<br> &nbsp; &nbsp;Student(name='tang', email='tang@163.com', age=16),<br> &nbsp; &nbsp;Student(name='wu', email='wu@gmail.com', age=20),<br> &nbsp; &nbsp;Student(name='qian', email='qian@gmail.com', age=21),<br> &nbsp; &nbsp;Student(name='liu', email='liu@163.com', age=21),<br> &nbsp; &nbsp;Student(name='li', email='li@163.com', age=18),<br> &nbsp; &nbsp;Student(name='sun', email='sun@163.com', age=17),<br>]<br>\u200b<br># 2. \u4e00\u6b21\u6027\u6dfb\u52a0\u5230\u8fde\u63a5\u4f1a\u8bdd\u4e2d<br>db.session.add_all(student_list)<br>db.session.commit()<\/pre>\n\n\n\n<p>\u5220\u9664\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u65b9\u6cd51[\u5148\u67e5\u8be2\u540e\u5220\u9664\uff0c2\u6761\u8bed\u53e5\u5b8c\u6210\u5220\u9664\u64cd\u4f5c]<br># \u5148\u67e5\u8be2\u51fa\u6765<br>student = Student.query.first()<br>print(student)<br># \u518d\u8fdb\u884c\u5220\u9664<br>db.session.delete(student)<br>db.session.commit()<br>\u200b<br># \u65b9\u6cd52\u30101\u6761\u8bed\u53e5\u5b8c\u6210\u5220\u9664\u64cd\u4f5c\uff0c\u6027\u80fd\u66f4\u597d\u66f4\u9ad8\u6548\u3011 &nbsp; &nbsp; <br># \u7c7b\u4f3c\u4e50\u89c2\u9501\uff0c\u5728\u6570\u636e\u6539\u52a8\u65f6\u6dfb\u52a0\u6761\u4ef6\u5e76\u5224\u65ad\u6761\u4ef6\u6210\u7acb\u4ee5\u540e\u624d\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\uff0c\u8fd9\u79cd\u7528\u6cd5\u5c31\u662f\u4e50\u89c2\u9501<br>Student.query.filter(Student.id == 5).delete()<br>db.session.commit()<br>\u200b<br>\u200b<br>\"\"\"<br>\u60b2\u89c2\u9501\uff0c\u662f\u5c5e\u4e8e\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u79cd\u9501\u673a\u5236\uff0c\u4f46\u662f\u4e50\u89c2\u9501\u5e76\u975e\u771f\u6b63\u7684\u6570\u636e\u5e93\u9501\u3002<br>2\u79cd\u9501\u90fd\u662f\u6570\u636e\u5e93\u5728\u5e94\u5bf9\u5e76\u53d1\u64cd\u4f5c\u65f6\uff0c\u9632\u6b62\u51fa\u73b0\u8d44\u6e90\u62a2\u593a\u7684\uff0c\u57fa\u4e8e\u4e0d\u540c\u4eba\u751f\u89c2\u6240\u5b9e\u73b02\u79cd\u89e3\u51b3\u65b9\u6848\u3002<br>\u60b2\u89c2\u9501\u7684\u57fa\u672c\u4f7f\u7528\uff1a<br> &nbsp;  &gt;&gt;&gt; \u6570\u636e\u5e93\u7ec8\u7aef\u5f00\u59cb<br>\u200b<br> &nbsp;  begin;  -- \u5f00\u542f\u4e8b\u52a1<br> &nbsp;  select * from db_student where student_id = 5 for update; -- \u6dfb\u52a0\u4e00\u628a\u66f4\u65b0\u9501\u3010\u60b2\u89c2\u9501\u3011<br> &nbsp;  .... &nbsp;  -- \u5728\u4e8b\u52a1\u63d0\u4ea4\u4e4b\u524d\uff0c\u4efb\u4f55\u7b2c\u4e09\u65b9\u8fde\u63a5\u90fd\u4e0d\u80fd\u4fee\u6539 student_id = 5\u8fd9\u6761\u6570\u636e <br> &nbsp;  commit; -- \u63d0\u4ea4\u4e8b\u52a1<br>\u200b<br> &nbsp;  &lt;&lt;&lt; \u6570\u636e\u5e93\u7ec8\u7aef\u5f00\u59cb<br>\u200b<br>\u60b2\u89c2\u9501\u7684\u95ee\u9898\uff1a<br>1. \u63d0\u524d\u9501\u5b9a\u6570\u636e\uff0c\u5f62\u6210\u4e32\u884c\u5316\uff0c\u5f62\u6210\u963b\u585e\uff0c\u4e0d\u5229\u4e8e\u6027\u80fd\u53d1\u6325\uff0c\u4e0d\u9002\u7528\u9ad8\u5e76\u53d1\u573a\u666f\u3002<br>2. \u60b2\u89c2\u9501\u53ea\u80fd\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\uff0c\u4e0d\u80fd\u4fdd\u8bc1\u810f\u6570\u636e\u7684\u51fa\u73b0<br>\u200b<br>\u4e50\u89c2\u9501\u7684\u51fa\u73b0\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u60b2\u89c2\u9501\u7684\u95ee\u9898\u3002<br>\u4e3e\u4f8b\uff1a\u53cc11\u6d3b\u52a8\uff0c\u5546\u57ce\u91cc\u9762id=5\u7684\u5546\u54c1\u7684\u5e93\u5b58=10\u4e86\uff0c\u73b0\u5728\u6211\u4eec\u8981\u57fa\u4e8e\u4e50\u89c2\u9501\u548c\u60b2\u89c2\u9501\u6765\u89e3\u51b3\u4e0b\u5355\u8fc7\u7a0b\u4e2d\uff0c\u51fa\u73b0\u7684\u8d44\u6e90\u62a2\u593a\u73b0\u8c61\uff0c\u907f\u514d\u51fa\u73b0\u8d85\u5356\uff08\u5546\u54c1\u6570\u91cf\u4e0d\u80fd\u4e3a\u8d1f\u6570\uff09\u3002<br>\u200b<br>\u4e50\u89c2\u9501\uff1a<br>---&gt; begin;  \u5f00\u542f\u4e8b\u52a1<br>---&gt; \u5148\u67e5\u770b\u5e93\u5b58\uff0c\u8bb0\u5f55\u5f53\u524d\u5e93\u5b58 num=10<br>---&gt; \u8fdb\u884c\u4e0b\u5355\u64cd\u4f5c\uff0c\u4e706\u4ef6<br>---&gt; \u4ed8\u6b3e<br>---&gt; \u6263\u9664\u5e93\u5b58 update goods set num=num-6 where num=10 and id=5;  # \u589e\u52a0\u66f4\u65b0\u6761\u4ef6\uff0c\u5224\u65ad\u5e93\u5b58\u662f\u5426\u8fd8\u662f\u539f\u6765<br>---&gt; \u5982\u679c\u6267\u884c\u6210\u529f\uff0c\u5219\u8868\u793a\u6ca1\u6709\u4eba\u62a2\uff0c\u8d2d\u4e70\u6210\u529f<br> &nbsp; &nbsp; \u5982\u679c\u6267\u884c\u4e8b\u53d8\uff0c\u5219\u8868\u793a\u5df2\u7ecf\u6709\u4eba\u5148\u62a2\u8d2d<br>---&gt; commit;<br>\u200b<br>\u60b2\u89c2\u9501\uff1a<br>---&gt; begin; \u5f00\u542f\u4e8b\u52a1<br>---&gt; \u5148\u7ed9id=5\u7684\u6570\u636e\uff0c\u52a0\u9501<br> &nbsp; &nbsp; select * from goods where id=5 for update;<br>---&gt; \u8fdb\u884c\u4e0b\u5355\u64cd\u4f5c\uff0c\u4e706\u4ef6<br>---&gt; \u4ed8\u6b3e<br>---&gt; \u6263\u9664\u5e93\u5b58  update goods set num=num-6 where id=5<br>---&gt; \u6267\u884c\u6210\u529f\u89e3\u9501<br>---- commit;  \u63d0\u4ea4\u4e8b\u52a1<br>\"\"\"<\/pre>\n\n\n\n<p>\u66f4\u65b0\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask, render_template<br>from flask_sqlalchemy import SQLAlchemy<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u7ba1\u7406\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_student\" # \u8868\u540d<br> &nbsp; &nbsp;# \u5c5e\u6027\u540d = db.Column(\u5b57\u6bb5\u7c7b\u578b, \u5b57\u6bb5\u5217\u7ea6\u675f\u9009\u9879)<br> &nbsp; &nbsp;# \u5982\u679cSQL\u8bed\u53e5\u4e2d\u7684\u5b57\u6bb5\u540d\u5728python\u4e2d\u662f\u5173\u952e\u5b57\/\u4fdd\u7559\u5b57\uff0c\u5219\u5efa\u8bae\u6539\u5199\u7ed1\u5b9a\u5b57\u6bb5\u540d<br> &nbsp; &nbsp;# \u5c5e\u6027\u540d = db.Column(\"\u5b57\u6bb5\u540d\", \u5b57\u6bb5\u7c7b\u578b, \u5b57\u6bb5\u5217\u7ea6\u675f\u9009\u9879)<br> &nbsp; &nbsp;id = db.Column(\"student_id\", db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), index=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.SmallInteger, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(255), unique=True, comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10,2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;# repr()\u65b9\u6cd5\u7c7b\u4f3c\u4e8edjango\u7684__str__\uff0c\u7528\u4e8e\u6253\u5370\u6a21\u578b\u5bf9\u8c61\u65f6\u663e\u793a\u7684\u5b57\u7b26\u4e32\u4fe1\u606f<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>\u200b<br># \u6240\u6709\u7684\u6a21\u578b\u5fc5\u987b\u76f4\u63a5\u6216\u95f4\u63a5\u7ee7\u627f\u4e8edb.Model<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_course\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_course (<br> &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp;  name varchar(64) comment \"\u8bfe\u7a0b\",<br> &nbsp; &nbsp; &nbsp;  price NUMERIC(7,2) comment \"\u4ef7\u683c\",<br> &nbsp; &nbsp; &nbsp;  unique (name)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2), comment=\"\u4ef7\u683c\")<br> &nbsp; &nbsp;# repr()\u65b9\u6cd5\u7c7b\u4f3c\u4e8edjango\u7684__str__\uff0c\u7528\u4e8e\u6253\u5370\u6a21\u578b\u5bf9\u8c61\u65f6\u663e\u793a\u7684\u5b57\u7b26\u4e32\u4fe1\u606f<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>\u200b<br>class Teacher(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8001\u5e08\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_teacher\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_teacher (<br> &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp;  name varchar(64) comment \"\u59d3\u540d\",<br> &nbsp; &nbsp; &nbsp;  option enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\") comment \"\u804c\u4f4d\",<br> &nbsp; &nbsp; &nbsp;  unique (`name`)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;option = db.Column(db.Enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\"), default=\"\u8bb2\u5e08\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp; # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>\"\"\"\u4e00\u6b21\u6dfb\u52a0\u4e00\u6761\u6570\u636e\"\"\"<br>@app.route(\"\/add\")<br>def add_student():<br> &nbsp; &nbsp;# 1. \u5148\u5b9e\u4f8b\u5316\u8981\u521b\u5efa\u7684\u6a21\u578b\u5bf9\u8c61<br> &nbsp; &nbsp;student = Student(name=\"\u5c0f\u660e\", age=17, email=\"xiaoming@qq.com\", money=100) &nbsp;# \u5b9e\u4f8b\u5316\u6a21\u578b\u5bf9\u8c61<br> &nbsp; &nbsp;# 2. \u628a\u5b9e\u4f8b\u5bf9\u8c61\u6dfb\u52a0\u5230\u8fde\u63a5\u4f1a\u8bdd<br> &nbsp; &nbsp;db.session.add(student)<br> &nbsp; &nbsp;# 3. \u63d0\u4ea4\u4e8b\u52a1<br> &nbsp; &nbsp;db.session.commit()<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>\"\"\"\u4e00\u6b21\u6dfb\u52a0\u591a\u6761\u6570\u636e\"\"\"<br>@app.route(\"\/madd\")<br>def multi_add():<br> &nbsp; &nbsp;# 1. \u5148\u5b9e\u4f8b\u5316\u8981\u521b\u5efa\u7684\u6a21\u578b\u5bf9\u8c61<br> &nbsp; &nbsp;student = Student(name=\"\u5c0f\u7ea2\", age=17, email=\"xiaohong@qq.com\", money=200)<br> &nbsp; &nbsp;# 2. \u628a\u5b9e\u4f8b\u5bf9\u8c61\u6dfb\u52a0\u5230\u8fde\u63a5\u4f1a\u8bdd<br> &nbsp; &nbsp;db.session.add(student)<br> &nbsp; &nbsp;# 1. \u5148\u5b9e\u4f8b\u5316\u8981\u521b\u5efa\u7684\u6a21\u578b\u5bf9\u8c61<br> &nbsp; &nbsp;student = Student(name=\"\u5c0f\u82b1\", age=16, email=\"xiaohua@qq.com\", money=200)<br> &nbsp; &nbsp;# 2. \u628a\u5b9e\u4f8b\u5bf9\u8c61\u6dfb\u52a0\u5230\u8fde\u63a5\u4f1a\u8bdd<br> &nbsp; &nbsp;db.session.add(student)<br> &nbsp; &nbsp;# 3. \u53ea\u9700\u8981\u5728\u7ed3\u675f\u7684\u65f6\u5019\u63d0\u4ea4\u4e8b\u52a1\u5373\u53ef<br> &nbsp; &nbsp;db.session.commit()<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/madd2\")<br>def multi_add2():<br> &nbsp; &nbsp;# 1. \u5148\u521b\u5efa\u7684\u5217\u8868\u8981\u6dfb\u52a0\u7684\u5b9e\u4f8b\u5316\u6a21\u578b\u5bf9\u8c61\u5217\u8868<br> &nbsp; &nbsp;student_list = [<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='wang', email='wang@163.com', age=20),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='zhang', email='zhang@189.com', age=21),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='chen', email='chen@126.com', age=19),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='zhou', email='zhou@163.com', age=18),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='tang', email='tang@163.com', age=16),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='wu', email='wu@gmail.com', age=20),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='qian', email='qian@gmail.com', age=21),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='liu', email='liu@163.com', age=21),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='li', email='li@163.com', age=18),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student(name='sun', email='sun@163.com', age=17),<br> &nbsp;  ]<br>\u200b<br> &nbsp; &nbsp;# 2. \u4e00\u6b21\u6027\u6dfb\u52a0\u5230\u8fde\u63a5\u4f1a\u8bdd\u4e2d<br> &nbsp; &nbsp;db.session.add_all(student_list)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>\u200b<br>@app.route(\"\/del\")<br>def delete_student():<br> &nbsp; &nbsp;\"\"\"\u5220\u9664\u4e00\u6761\u6570\u636e\"\"\"<br> &nbsp; &nbsp;# \u5148\u67e5\u8be2\u51fa\u6765<br> &nbsp; &nbsp;student = Student.query.first()<br> &nbsp; &nbsp;# student = db.session.query(Student).first()<br> &nbsp; &nbsp;# \u518d\u8fdb\u884c\u5220\u9664<br> &nbsp; &nbsp;db.session.delete(student)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/mdel\")<br>def multi_delete_student():<br> &nbsp; &nbsp;\"\"\"\u6309\u6761\u4ef6\u5220\u9664\u591a\u6761\u6570\u636e\"\"\"<br> &nbsp; &nbsp;Student.query.filter(Student.id &gt; 5).delete()<br> &nbsp; &nbsp;# db.session.query(Student).filter(Student.id &gt; 5).delete()<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/update\")<br>def update():<br> &nbsp; &nbsp;\"\"\"\u66f4\u65b0\u4e00\u6761\"\"\"<br> &nbsp; &nbsp;# \u5148\u67e5\u8be2\u51fa\u6765<br> &nbsp; &nbsp;student = Student.query.filter(Student.id == 4).first()<br> &nbsp; &nbsp;student.name = \"\u5c0f\u767d\"<br> &nbsp; &nbsp;db.session.commit()<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/update2\")<br>def update2():<br> &nbsp; &nbsp;\"\"\"\u76f4\u63a5\u6839\u636e\u6761\u4ef6\u66f4\u65b0\u4e00\u6761\u6216\u591a\u6761\u6570\u636e\"\"\"<br> &nbsp; &nbsp;Student.query.filter(Student.name == 'zhang', Student.money == -99.00).update({'money': 1998})<br> &nbsp; &nbsp;db.session.commit()<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/update3\")<br>def update3():<br> &nbsp; &nbsp;# \u5b57\u6bb5\u5f15\u7528[\u5229\u7528\u5f53\u524d\u4e00\u6761\u6570\u636e\u7684\u5b57\u5178\u503c\u8fdb\u884c\u8f85\u52a9\u64cd\u4f5c,\u5b9e\u73b0\u7c7b\u4f3cdjango\u91cc\u9762F\u51fd\u6570\u7684\u6548\u679c]<br> &nbsp; &nbsp;# \u6bcf\u6b21\u81ea\u589e100<br> &nbsp; &nbsp;Student.query.filter(Student.name == \"\u5c0f\u82b1\").update({\"money\": Student.money + 100})<br> &nbsp; &nbsp;db.session.commit()<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/update4\")<br>def update4():<br> &nbsp; &nbsp;# \u5b57\u6bb5\u5f15\u7528[\u5229\u7528\u5f53\u524d\u4e00\u6761\u6570\u636e\u7684\u5b57\u5178\u503c\u8fdb\u884c\u8f85\u52a9\u64cd\u4f5c,\u5b9e\u73b0\u7c7b\u4f3cdjango\u91cc\u9762F\u51fd\u6570\u7684\u6548\u679c]<br> &nbsp; &nbsp;# \u5728\u539f\u6709money\u7684\u57fa\u7840\u4e0a\u6309age\u8865\u8d341000*age<br> &nbsp; &nbsp;Student.query.filter(Student.name == \"zhang\").update({\"money\": Student.money + 1000 * Student.age})<br> &nbsp; &nbsp;db.session.commit()<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u57fa\u672c\u67e5\u8be2<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">SQLAlchemy\u5e38\u7528\u7684\u67e5\u8be2\u8fc7\u6ee4\u5668<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8fc7\u6ee4\u5668<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><strong>filter()<\/strong><\/td><td>\u628a\u8fc7\u6ee4\u5668\u6dfb\u52a0\u5230\u539f\u67e5\u8be2\u4e0a\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u67e5\u8be2<\/td><\/tr><tr><td>filter_by()<\/td><td>\u628a\u7b49\u503c\u8fc7\u6ee4\u5668\u6dfb\u52a0\u5230\u539f\u67e5\u8be2\u4e0a\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u67e5\u8be2<\/td><\/tr><tr><td><strong>limit()<\/strong><\/td><td>\u4f7f\u7528\u6307\u5b9a\u7684\u503c\u9650\u5b9a\u539f\u67e5\u8be2\u8fd4\u56de\u7684<strong>\u7ed3\u679c\u6570\u91cf<\/strong><\/td><\/tr><tr><td><strong>offset()<\/strong><\/td><td>\u8bbe\u7f6e\u7ed3\u679c\u8303\u56f4\u7684<strong>\u5f00\u59cb\u4f4d\u7f6e<\/strong>\uff0c\u504f\u79fb\u539f\u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u67e5\u8be2<\/td><\/tr><tr><td><strong>order_by()<\/strong><\/td><td>\u6839\u636e\u6307\u5b9a\u6761\u4ef6\u5bf9\u539f\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c<strong>\u6392\u5e8f<\/strong>\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u67e5\u8be2<\/td><\/tr><tr><td><strong>group_by()<\/strong><\/td><td>\u6839\u636e\u6307\u5b9a\u6761\u4ef6\u5bf9\u539f\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c<strong>\u5206\u7ec4<\/strong>\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u67e5\u8be2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">SQLAlchemy\u5e38\u7528\u7684\u67e5\u8be2\u7ed3\u679c\u65b9\u6cd5<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><strong>all()<\/strong><\/td><td>\u4ee5<strong>\u5217\u8868\u5f62\u5f0f<\/strong>\u8fd4\u56de\u67e5\u8be2\u7684\u6240\u6709\u7ed3\u679c<\/td><\/tr><tr><td><strong>first()<\/strong><\/td><td>\u8fd4\u56de\u67e5\u8be2\u7684\u7b2c\u4e00\u4e2a\u7ed3\u679c\uff0c<strong>\u6a21\u578b\u5bf9\u8c61<\/strong>\uff0c\u5982\u679c\u672a\u67e5\u5230\uff0c\u8fd4\u56de<strong>None<\/strong><\/td><\/tr><tr><td>first_or_404()<\/td><td>\u8fd4\u56de\u67e5\u8be2\u7684\u7b2c\u4e00\u4e2a\u7ed3\u679c\uff0c<strong>\u6a21\u578b\u5bf9\u8c61<\/strong>\uff0c\u5982\u679c\u672a\u67e5\u5230\uff0c\u901a\u8fc7abort \u8fd4\u56de404\u5f02\u5e38<\/td><\/tr><tr><td><strong>get()<\/strong><\/td><td>\u8fd4\u56de<strong>\u6307\u5b9a\u4e3b\u952e<\/strong>\u5bf9\u5e94\u7684<strong>\u6a21\u578b\u5bf9\u8c61<\/strong>\uff0c\u5982\u4e0d\u5b58\u5728\uff0c\u8fd4\u56deNone<\/td><\/tr><tr><td>get_or_404()<\/td><td>\u8fd4\u56de\u6307\u5b9a\u4e3b\u952e\u5bf9\u5e94\u7684\u884c\uff0c\u5982\u4e0d\u5b58\u5728\uff0cabort \u8fd4\u56de404<\/td><\/tr><tr><td><strong>count()<\/strong><\/td><td>\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u7684<strong>\u6570\u91cf<\/strong><\/td><\/tr><tr><td><strong>paginate()<\/strong><\/td><td>\u8fd4\u56de\u4e00\u4e2aPaginate<strong>\u5206\u9875\u5668\u5bf9\u8c61<\/strong>\uff0c\u5b83\u5305\u542b\u6307\u5b9a\u8303\u56f4\u5185\u7684\u7ed3\u679c<\/td><\/tr><tr><td><strong>having()<\/strong><\/td><td>\u8fd4\u56de\u7ed3\u679c\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\uff0c<strong>\u5fc5\u987b\u8ddf\u5728group by\u540e\u9762<\/strong>\uff0c\u5176\u4ed6\u5730\u65b9\u65e0\u6cd5\u4f7f\u7528\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>get():\u53c2\u6570\u4e3a\u4e3b\u952e\uff0c\u8868\u793a\u6839\u636e\u4e3b\u952e\u67e5\u8be2\u6570\u636e\uff0c\u5982\u679c\u4e3b\u952e\u4e0d\u5b58\u5728\u8fd4\u56deNone<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u200b<br>@app.route(\"\/get\")<br>def get():<br> &nbsp; &nbsp;\"\"\"\u6309\u4e3b\u952e\u83b7\u53d6\u4e00\u6761\"\"\"<br> &nbsp; &nbsp;# student = Student.query.get({\"id\": 5})<br> &nbsp; &nbsp;# student = Student.query.get((5,))<br> &nbsp; &nbsp;# student = db.session.query(Student).get(5)<br> &nbsp; &nbsp;student = Student.query.get(5)<br> &nbsp; &nbsp;print(student)<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<\/pre>\n\n\n\n<p>\u8bfe\u5802\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u524d\u9762\u4ee3\u7801\u7701\u7565<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp; # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>@app.route(\"\/query\")<br>def query():<br> &nbsp; &nbsp;query1 = Student.query &nbsp; # \u7b80\u5199\u64cd\u4f5c<br> &nbsp; &nbsp;query2 = db.session.query(Student)<br> &nbsp; &nbsp;print(type(query1), query1)<br> &nbsp; &nbsp;print(type(query2), query2)<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/get\")<br>def get():<br> &nbsp; &nbsp;\"\"\"get\u6839\u636e\u4e3b\u952e\u83b7\u53d6\u6570\u636e\"\"\" &nbsp; &nbsp;<br> &nbsp; &nbsp;# student1 = Student.query.get({\"id\":10})<br> &nbsp; &nbsp;student1 = Student.query.get(30)<br> &nbsp; &nbsp;# student2 = db.session.query(Student).get({\"id\":10})<br> &nbsp; &nbsp;student2 = db.session.query(Student).get(30)<br> &nbsp; &nbsp;# \u7ed3\u679c\u662f\u6a21\u578b\u5bf9\u8c61<br> &nbsp; &nbsp;print(type(student1), student1)<br> &nbsp; &nbsp;print(type(student2), student2)<br> &nbsp; &nbsp;# \u67e5\u8be2\u4e0d\u5230\u7ed3\u679c\uff0c\u5219\u8fd4\u56de\u503c\u4e3aNone<br> &nbsp; &nbsp;if student1:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(student1.name, student1.age)<br> &nbsp; &nbsp;if student2:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(student2.name, student2.age)<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<br>\u200b<\/pre>\n\n\n\n<p>all()\u8fd4\u56de\u67e5\u8be2\u5230\u7684\u6240\u6709\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6a21\u578b\u7c7b.query.all()<br>\u200b<br>\"\"\"\u83b7\u53d6\u591a\u4e2a\u6570\u636e\"\"\"<br>student = Student.query.all()<br>print(student) # [dong&lt;Student&gt;, \u5c0f\u7ea2&lt;Student&gt;, wang&lt;Student&gt;, chen&lt;Student&gt;, zhou&lt;Student&gt;, tang&lt;Student&gt;, wu&lt;Student&gt;, qian&lt;Student&gt;, liu&lt;Student&gt;, li&lt;Student&gt;, sun&lt;Student&gt;]<br>\u200b<br>student = Student.query.filter(Student.id&lt;5).all() &nbsp;# \u6ca1\u6709\u7ed3\u679c\u8fd4\u56de\u7a7a\u5217\u8868[]<br>print(student) # [dong&lt;Student&gt;, \u5c0f\u7ea2&lt;Student&gt;, wang&lt;Student&gt;]<br>\u200b<br># all()\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2apython\u5217\u8868\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5207\u7247\uff0c\u4e0edjango\u7684QuerySet\u5b8c\u5168\u4e0d\u662f\u4e00\u56de\u4e8b\u3002<br>student = Student.query.filter(Student.id &lt; 5).all()[:-1] &nbsp;# \u6ca1\u6709\u7ed3\u679c\u8fd4\u56de\u7a7a\u5217\u8868[]<br>print(student) # [dong&lt;Student&gt;, \u5c0f\u7ea2&lt;Student&gt;]<\/pre>\n\n\n\n<p>count \u8fd4\u56de\u7ed3\u679c\u7684\u6570\u91cf<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u8fd4\u56de\u7ed3\u679c\u7684\u6570\u91cf<br>ret = Student.query.filter(Student.id &lt; 5).count()<br>print(f\"ret={ret}\")<\/pre>\n\n\n\n<p>first()\u8fd4\u56de\u67e5\u8be2\u5230\u7684\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u3010first\u83b7\u53d6\u4e00\u6761\u6570\u636e,all\u83b7\u53d6\u591a\u6761\u6570\u636e\u3011<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6a21\u578b\u7c7b.query.first()<br>\u200b<br>\"\"\"\u83b7\u53d6\u7b2c\u4e00\u4e2a\u6570\u636e\"\"\"<br>student = Student.query.first()<br>print(student)<br>\u200b<br>student = Student.query.filter(Student.id==5).first() # \u6ca1\u6709\u7ed3\u679c\u8fd4\u56deNone<br>print(student)<\/pre>\n\n\n\n<p>filter\u6761\u4ef6\u67e5\u8be2\uff0c\u652f\u6301\u5404\u79cd\u8fd0\u7b97\u7b26\u548c\u67e5\u8be2\u65b9\u6cd5\u6216\u8005\u6a21\u7cca\u67e5\u8be2\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u540d\u5b57\u7ed3\u5c3e\u5b57\u7b26\u4e3ag\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u6a21\u7cca\u67e5\u8be2<br># \u4f7f\u7528163\u90ae\u7bb1\u7684\u6240\u6709\u7528\u6237<br>student_list = Student.query.filter(Student.email.endswith(\"@163.com\")).all()<br>print(student_list)<br>\u200b<br># \u59d3\u540d\u4ee5\"zh\"\u5f00\u5934\u7684<br>student_list = Student.query.filter(Student.name.startswith(\"zh\")).all()<br>print(student_list)<br>\u200b<br># \u540d\u5b57\u4e2d\u5e26\u6709\"a\"\u5b57\u6bcd\u7684\u6570\u636e<br>student_list = Student.query.filter(Student.name.contains(\"a\")).all()<br>print(student_list)<br>\u200b<br>\"\"\"\u5355\u6761\u4ef6\u6bd4\u8f83\"\"\"<br># \u5219\u9700\u8981\u6307\u5b9a\u6761\u4ef6\u683c\u5f0f\u4e3a: filter(\u6a21\u578b.\u5b57\u6bb5 \u6bd4\u8f83\u8fd0\u7b97\u7b26 \u503c)\u3002<br># \u8fd0\u7b97\u7b26\u53ef\u4ee5\u662f: ==\u8868\u793a\u76f8\u7b49, !=\u4e0d\u76f8\u7b49\uff0c&gt; \u8868\u793a\u5927\u4e8e  &lt; \u8868\u793a\u5c0f\u4e8e\uff0c&gt;=\u5927\u4e8e\u7b49\u4e8e\uff0c&lt;=\u5c0f\u4e8e\u7b49\u4e8e<br># student_list = Student.query.filter(Student.age &gt; 18).all()<br># print(student_list) # [wang&lt;Student&gt;, chen&lt;Student&gt;, zhou&lt;Student&gt;,...]<br>\u200b<br>\"\"\"\u591a\u6761\u4ef6\u6bd4\u8f83\"\"\"<br># \u8981\u6c42\u591a\u4e2a\u6761\u4ef6\u90fd\u8981\u6ee1\u8db3\uff0c\u76f8\u5f53\u4e8e\u903b\u8f91\u67e5\u8be2\u4e2d\u7684 \u5e76\u4e14(and)\uff01\uff01<br>student_list = Student.query.filter(Student.age &gt; 18, Student.sex == True).all()<br>print(student_list) # [wang&lt;Student&gt;, chen&lt;Student&gt;, qian&lt;Student&gt;, liu&lt;Student&gt;]<\/pre>\n\n\n\n<p>filter_by\u7cbe\u786e\u6761\u4ef6\u67e5\u8be2<\/p>\n\n\n\n<p>filter_by \u53ea\u652f\u6301\u5b57\u6bb5\u7684<strong>\u503c\u662f\u5426\u76f8\u7b49<\/strong>\u7684\u60c5\u51b5\uff0c\u5bf9\u4e8e\u5927\u4e8e\u3001\u7b49\u4e8e\u3001\u7b49\u7b49\u5176\u4ed6\u6761\u4ef6\u662f\u4e0d\u652f\u6301\u7684\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a\u8fd4\u56deage\u7b49\u4e8e22\u7684\u5b66\u751f<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5355\u6761\u4ef6<br>student_list = Student.query.filter_by(age=22).all() &nbsp;# \u5b57\u6bb5\u6dfb\u52a0\u4e0d\u9700\u8981\u9644\u5e26\u6a21\u578b\u7c7b<br>print(student_list)<br>\u200b<br># \u591a\u6761\u4ef6<br>student_list = Student.query.filter_by(age=22,sex=True).all()<br>print(student_list)<\/pre>\n\n\n\n<p>\u7ec3\u4e60\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u67e5\u8be2\u6240\u6709\u7537\u751f[Student.sex==True]\u6570\u636e<br>\u200b<br>\u67e5\u8be2id\u4e3a4\u7684\u5b66\u751f[3\u79cd\u65b9\u5f0f]<br> <br>\u67e5\u8be2\u5e74\u9f84\u7b49\u4e8e22\u7684\u6240\u6709\u5b66\u751f\u6570\u636e<br>\u200b<br>\u67e5\u8be2name\u4e3a\u5c0f\u767d\u7684\u5b66\u751f\u6570\u636e<br>\u200b<br>\u67e5\u8be220\u5c81\u7684\u7537\u751f<\/pre>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask, render_template<br>from flask_sqlalchemy import SQLAlchemy<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u7ba1\u7406\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_student\" # \u8868\u540d<br> &nbsp; &nbsp;# \u5c5e\u6027\u540d = db.Column(\u5b57\u6bb5\u7c7b\u578b, \u5b57\u6bb5\u5217\u7ea6\u675f\u9009\u9879)<br> &nbsp; &nbsp;# \u5982\u679cSQL\u8bed\u53e5\u4e2d\u7684\u5b57\u6bb5\u540d\u5728python\u4e2d\u662f\u5173\u952e\u5b57\/\u4fdd\u7559\u5b57\uff0c\u5219\u5efa\u8bae\u6539\u5199\u7ed1\u5b9a\u5b57\u6bb5\u540d<br> &nbsp; &nbsp;# \u5c5e\u6027\u540d = db.Column(\"\u5b57\u6bb5\u540d\", \u5b57\u6bb5\u7c7b\u578b, \u5b57\u6bb5\u5217\u7ea6\u675f\u9009\u9879)<br> &nbsp; &nbsp;id = db.Column(\"student_id\", db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), index=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.SmallInteger, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(255), unique=True, comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10,2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;# repr()\u65b9\u6cd5\u7c7b\u4f3c\u4e8edjango\u7684__str__\uff0c\u7528\u4e8e\u6253\u5370\u6a21\u578b\u5bf9\u8c61\u65f6\u663e\u793a\u7684\u5b57\u7b26\u4e32\u4fe1\u606f<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"&lt;{self.__class__.__name__} {self.name}&gt;\"<br>\u200b<br>\u200b<br># \u6240\u6709\u7684\u6a21\u578b\u5fc5\u987b\u76f4\u63a5\u6216\u95f4\u63a5\u7ee7\u627f\u4e8edb.Model<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_course\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_course (<br> &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp;  name varchar(64) comment \"\u8bfe\u7a0b\",<br> &nbsp; &nbsp; &nbsp;  price NUMERIC(7,2) comment \"\u4ef7\u683c\",<br> &nbsp; &nbsp; &nbsp;  unique (name)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2), comment=\"\u4ef7\u683c\")<br> &nbsp; &nbsp;# repr()\u65b9\u6cd5\u7c7b\u4f3c\u4e8edjango\u7684__str__\uff0c\u7528\u4e8e\u6253\u5370\u6a21\u578b\u5bf9\u8c61\u65f6\u663e\u793a\u7684\u5b57\u7b26\u4e32\u4fe1\u606f<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>\u200b<br>class Teacher(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8001\u5e08\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_teacher\"<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  # \u539f\u751fSQL\u8bed\u53e5<br> &nbsp;  create table db_teacher (<br> &nbsp; &nbsp; &nbsp;  id int primary key auto_increment comment \"\u4e3b\u952e\",<br> &nbsp; &nbsp; &nbsp;  name varchar(64) comment \"\u59d3\u540d\",<br> &nbsp; &nbsp; &nbsp;  option enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\") comment \"\u804c\u4f4d\",<br> &nbsp; &nbsp; &nbsp;  unique (`name`)<br> &nbsp;  );<br> &nbsp;  # \u5b57\u6bb5\u6839\u636eSQL\u8bed\u53e5\u6765\u58f0\u660e<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;option = db.Column(db.Enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\"), default=\"\u8bb2\u5e08\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp; # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>@app.route(\"\/exam\")<br>def exam():<br> &nbsp; &nbsp;# \u67e5\u8be2\u6240\u6709\u7537\u751f[Student.sex == True]\u6570\u636e<br> &nbsp; &nbsp;# student_list = Student.query.filter(Student.sex == True).all()<br> &nbsp; &nbsp;# print(student_list)<br>\u200b<br> &nbsp; &nbsp;# \u67e5\u8be2id\u4e3a4\u7684\u5b66\u751f[3\u79cd\u65b9\u5f0f]<br> &nbsp; &nbsp;# 1. get<br> &nbsp; &nbsp;# student = Student.query.get(4)<br> &nbsp; &nbsp;# print(student)<br>\u200b<br> &nbsp; &nbsp;# 2. filter+first<br> &nbsp; &nbsp;# student = Student.query.filter(Student.id == 4).first()<br> &nbsp; &nbsp;# print(student)<br>\u200b<br> &nbsp; &nbsp;# 3. filter_by + first<br> &nbsp; &nbsp;# student = Student.query.filter_by(id=4).first()<br> &nbsp; &nbsp;# print(student)<br>\u200b<br> &nbsp; &nbsp;# \u67e5\u8be2\u5e74\u9f84\u7b49\u4e8e20\u7684\u6240\u6709\u5b66\u751f\u6570\u636e<br> &nbsp; &nbsp;# student_list = Student.query.filter(Student.age == 20).all()<br> &nbsp; &nbsp;# print(student_list)<br>\u200b<br> &nbsp; &nbsp;# \u67e5\u8be2name\u4e3a\u5c0f\u767d\u7684\u5b66\u751f\u6570\u636e<br> &nbsp; &nbsp;# student = Student.query.filter(Student.name == \"\u5c0f\u767d\").first()<br> &nbsp; &nbsp;# print(student)<br>\u200b<br> &nbsp; &nbsp;# \u67e5\u8be220\u5c81\u7684\u7537\u751f<br> &nbsp; &nbsp;student_list = Student.query.filter(Student.age == 20, Student.sex == True).all()<br> &nbsp; &nbsp;print(student_list)<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<br>\u200b<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u591a\u6761\u4ef6\u67e5\u8be2<\/h3>\n\n\n\n<p>\u903b\u8f91\u4e0e\uff0c\u9700\u8981\u5bfc\u5165<code>and_<\/code>\uff0c\u8fd4\u56de<code>and_()<\/code>\u6761\u4ef6\u6ee1\u8db3\u7684\u6240\u6709\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import and_<br>Student.query.filter(and_(Student.name!='wang',Student.email.endswith('163.com'))).all()<br>\u200b<br>\u200b<br># # and_(\u6761\u4ef61,\u6761\u4ef62,....)  \u7b49\u4ef7\u4e8e  filter(\u6761\u4ef61,\u6761\u4ef62,.....)<br># # age &gt; 18 and email like \"%163.com\"<br># # student_list = Student.query.filter(Student.age &gt; 18, Student.email.endswith(\"163.com\")).all()<br># <br># student_list = Student.query.filter(<br># &nbsp; &nbsp; and_(<br># &nbsp; &nbsp; &nbsp; &nbsp; Student.age &gt; 18,<br># &nbsp; &nbsp; &nbsp; &nbsp; Student.email.endswith(\"163.com\")<br># &nbsp; &nbsp; )<br># ).all()<\/pre>\n\n\n\n<p>\u903b\u8f91\u6216\uff0c\u9700\u8981\u5bfc\u5165or_<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import or_<br>Student.query.filter(or_(Student.name!='wang',Student.email.endswith('163.com'))).all()<br>\u200b<br># \u67e5\u8be2\u5e74\u9f84\u572820\u5c81\uff0c\u4f7f\u7528\u7684\u90ae\u7bb1\u662fqq\u6216\u8005163\u90ae\u7bb1\u7684<br>student_list = Student.query.filter(<br> &nbsp; &nbsp;Student.age == 20,<br> &nbsp; &nbsp;or_(<br> &nbsp; &nbsp; &nbsp; &nbsp;Student.email.endswith(\"qq.com\"),<br> &nbsp; &nbsp; &nbsp; &nbsp;Student.email.endswith(\"163.com\")<br> &nbsp;  )<br>).all()<br>\u200b<br>\u200b<br># \u590d\u5408\u6761\u4ef6\u7684\u67e5\u8be2\u60c5\u51b5<br># \u67e5\u8be2\u5e74\u9f84&gt;17\u5c81\u7684\u5973\u751f\u6216\u8005\u5e74\u9f84&gt;18\u5c81\u7684\u7537\u751f<br>student_list = Student.query.filter(<br> &nbsp; &nbsp;or_(<br> &nbsp; &nbsp; &nbsp; &nbsp;and_(Student.age &gt; 17, Student.sex == False),<br> &nbsp; &nbsp; &nbsp; &nbsp;and_(Student.age &gt; 18, Student.sex == True),<br> &nbsp;  )<br>).all()<br>print(student_list)<br>\u200b<br>print(student_list)<\/pre>\n\n\n\n<p>\u903b\u8f91\u975e\uff0c\u8fd4\u56de\u540d\u5b57\u4e0d\u7b49\u4e8e&#8221;\u5c0f\u767d&#8221;\u7684\u6240\u6709\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Student.query.filter(Student.name!='\u5c0f\u767d').all()<\/pre>\n\n\n\n<p>not_ \u76f8\u5f53\u4e8e\u53d6\u53cd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import not_<br>Student.query.filter(not_(Student.name=='\u5c0f\u767d')).all()<br>\u200b<br># # \u67e5\u8be2\u5e74\u9f84\u4e0d\u7b49\u4e8e22<br># student_list = Student.query.filter(Student.age != 22).all()<br># print(student_list)<br># student_list = Student.query.filter(not_(Student.age==22)).all()<br># print(student_list)<\/pre>\n\n\n\n<p>in_\u8303\u56f4\u67e5\u8be2<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u67e5\u8be2id\u662f 5, 7, 10 \u7684\u5b66\u751f\u4fe1\u606f<br>student_list = Student.query.filter(Student.id.in_([5, 7, 10])).all()<br>print(student_list)<br>\u200b<br># \u67e5\u8be2id\u4e0d\u662f 1 3 5 \u7684\u5b66\u751f\u4fe1\u606f<br>student_list = Student.query.filter(not_(Student.id.in_([1, 3, 5]))).all()<br>print( student_list )<\/pre>\n\n\n\n<p>is_\u5224\u65ad\u503c\u67e5\u8be2<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;\"\"\"\u5224\u65ad\u503c\u67e5\u8be2\"\"\"<br> &nbsp; &nbsp;student_list = Student.query.filter(Student.email.is_(None)).all()<br> &nbsp; &nbsp;print(student_list)<br>\u200b<br> &nbsp; &nbsp;student_list = Student.query.filter(Student.email == None).all()<br> &nbsp; &nbsp;print(student_list)<\/pre>\n\n\n\n<p>order_by \u6392\u5e8f<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5012\u5e8f[\u503c\u4ece\u5927\u5230\u5c0f]<br>student_list = Student.query.order_by(Student.id.desc()).all()<br># \u5347\u5e8f[\u503c\u4ece\u5c0f\u5230\u5927]<br>student_list = Student.query.order_by(Student.id.asc()).all()<br>\u200b<br># \u591a\u5b57\u6bb5\u6392\u5e8f[\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u503c\u4e00\u6837\u65f6\uff0c\u6bd4\u8f83\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\uff0c\u8fdb\u884c\u6392\u5e8f]<br>student_list = Student.query.order_by(Student.money.asc(), Student.age.asc(), Student.id.asc()).all()<br>print(student_list)<\/pre>\n\n\n\n<p>count\u7edf\u8ba1<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u67e5\u8be2age&gt;=19\u7684\u7537\u751f\u7684\u6570\u91cf<br>from sqlalchemy import and_<br># ret = Student.query.filter( and_(Student.age&gt;=19,Student.sex==True) ).count()<br>ret = Student.query.filter( Student.age&gt;=19, Student.sex==True ).count()<\/pre>\n\n\n\n<p>\u5bf9\u7ed3\u679c\u8fdb\u884c\u504f\u79fb\u91cf\u548c\u6570\u91cf\u7684\u9650\u5236<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u67e5\u8be2\u5e74\u9f84\u6700\u5927\u76843\u4e2a\u5b66\u751f<br>student_list = Student.query.order_by(Student.age.desc()).limit(3).all()<br>print(student_list)<br>\u200b<br># \u67e5\u8be2\u5e74\u9f84\u6392\u7b2c4\u5230\u7b2c7\u540d\u7684\u5b66\u751f<br>student_list = Student.query.order_by(Student.age.desc()).offset(3).limit(4).all()<br>print(student_list)<br>\u200b<br># \u67e5\u8be2\u5e74\u9f84\u6700\u5c0f\u76843\u4e2a\u4eba<br>student_list = Student.query.order_by(Student.age.asc()).limit(3).all()<br>print(student_list)<\/pre>\n\n\n\n<p>SQL<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u67e5\u8be2\u5e74\u9f84\u6700\u5927\u76843\u4e2a\u5b66\u751f<br># select * from db_student order by age desc limit 3;<br>\u200b<br># \u67e5\u8be2\u5e74\u9f84\u6392\u7b2c4\u5230\u7b2c7\u540d\u7684\u5b66\u751f<br>select * from db_student order by age desc limit 3, 4;<br># select * from db_student order by age desc limit 4 offset 3;<br>\u200b<br># \u67e5\u8be2\u5e74\u9f84\u6700\u5c0f\u76843\u4e2a\u4eba<br># select * from db_student order by age asc limit 3;<\/pre>\n\n\n\n<p>\u7ec3\u4e60<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u67e5\u8be2age\u662f18 \u6216\u8005 \u4f7f\u7528163\u90ae\u7bb1\u7684\u6240\u6709\u5b66\u751f<br>Student.query.filter(or_(Student.age==18,Student.email.endswith(\"163.com\"))).all()<br># \u67e5\u8be2id\u4e3a [1, 3, 5, 7, 9] \u7684\u5b66\u751f\u5217\u8868<br>student_list = Student.query.filter(Student.id.in_([1, 3, 5, 7, 9])).all()<br>print(student_list)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5206\u9875\u5668<\/h3>\n\n\n\n<p>manage.py\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>\u200b<br>app = Flask(__name__)<br>class Config(object):<br> &nbsp; &nbsp;DEBUG = True<br> &nbsp; &nbsp;# \u6570\u636e\u5e93\u94fe\u63a5\u914d\u7f6e = \u6570\u636e\u5e93\u540d\u79f0:\/\/\u767b\u5f55\u8d26\u53f7:\u767b\u5f55\u5bc6\u7801@\u6570\u636e\u5e93\u4e3b\u673aIP:\u6570\u636e\u5e93\u8bbf\u95ee\u7aef\u53e3\/\u6570\u636e\u5e93\u540d\u79f0?charset=\u7f16\u7801\u7c7b\u578b<br> &nbsp; &nbsp;SQLALCHEMY_DATABASE_URI = \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a<br> &nbsp; &nbsp;SQLALCHEMY_TRACK_MODIFICATIONS = False<br> &nbsp; &nbsp;# ORM\u8fd0\u884c\u65f6\u4f1a\u663e\u793aORM\u751f\u6210\u7684\u539f\u59cbSQL\u8bed\u53e5[\u8c03\u8bd5]<br> &nbsp; &nbsp;SQLALCHEMY_ECHO = True<br>\u200b<br>app.config.from_object(Config)<br>\u200b<br>\u200b<br>\"\"\"\u6a21\u578b\u7c7b\u5b9a\u4e49\"\"\"<br>db = SQLAlchemy(app=app)<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u4fe1\u606f\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;# \u58f0\u660e\u4e0e\u5f53\u524d\u6a21\u578b\u7ed1\u5b9a\u7684\u6570\u636e\u8868\u540d\u79f0<br> &nbsp; &nbsp;__tablename__ = \"db_student\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True,comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), index=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.Boolean, default=True, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(128), unique=True, comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10,2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self): # \u76f8\u5f53\u4e8edjango\u7684__str__<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>\u200b<br># \u6240\u6709\u7684\u6a21\u578b\u5fc5\u987b\u76f4\u63a5\u6216\u95f4\u63a5\u7ee7\u627f\u4e8edb.Model<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_course\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2), comment=\"\u4ef7\u683c\")<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>class Teacher(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8001\u5e08\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"db_teacher\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;option = db.Column(db.Enum(\"\u8bb2\u5e08\", \"\u52a9\u6559\", \"\u73ed\u4e3b\u4efb\"), default=\"\u8bb2\u5e08\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;{self.__class__.__name__}&gt;\"<br>\u200b<br>from flask import request,render_template<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;# \u5206\u9875\u5668<br> &nbsp; &nbsp;page  = int(request.args.get(\"page\", 1))<br> &nbsp; &nbsp;size = int(request.args.get(\"size\", 3))<br> &nbsp; &nbsp;pagination = Student.query.paginate(page, size)<br> &nbsp; &nbsp;print(pagination)<br> &nbsp; &nbsp;\"\"\"<br> &nbsp;  from flask_sqlalchemy import Pagination<br> &nbsp;  # \u5e38\u7528\u5c5e\u6027<br> &nbsp;  total  \u603b\u6570\u636e\u91cf<br> &nbsp;  items  \u6bcf\u4e00\u9875\u6570\u636e\u9879\u5217\u8868<br> &nbsp;  pages  \u603b\u9875\u7801===&gt; math.ceil( total\/per_page )<br> &nbsp; &nbsp;<br> &nbsp;  # \u5e38\u7528\u65b9\u6cd5<br> &nbsp;  prev &nbsp; &nbsp;  \u4e0a\u4e00\u9875\u5206\u9875\u5bf9\u8c61<br> &nbsp;  prev_num  \u4e0a\u4e00\u9875\u9875\u7801<br> &nbsp;  has_prev  \u662f\u5426\u6709\u4e0a\u4e00\u9875<br> &nbsp; &nbsp;<br> &nbsp;  next &nbsp; &nbsp;  \u4e0b\u4e00\u9875\u5206\u9875\u5bf9\u8c61<br> &nbsp;  next_num  \u4e0b\u4e00\u9875\u9875\u7801<br> &nbsp;  has_next  \u662f\u5426\u6709\u4e0b\u4e00\u9875<br> &nbsp;  \"\"\"<br> &nbsp; &nbsp;print(pagination.items) &nbsp;# \u5f53\u524d\u9875\u7684\u6570\u636e\u9879\u5217\u8868<br> &nbsp; &nbsp;print(pagination.total) &nbsp;# \u603b\u6570\u636e\u91cf<br> &nbsp; &nbsp;print(pagination.pages) &nbsp;# \u603b\u9875\u7801\u6570\u91cf<br> &nbsp; &nbsp;print(pagination.prev_num) &nbsp;# \u4e0a\u4e00\u9875\u9875\u7801<br> &nbsp; &nbsp;print(pagination.next_num) &nbsp;# \u4e0b\u4e00\u9875\u9875\u7801<br> &nbsp; &nbsp;print(pagination.has_prev) &nbsp;# \u662f\u5426\u6709\u4e0a\u4e00\u9875<br> &nbsp; &nbsp;print(pagination.has_next) &nbsp;# \u662f\u5426\u6709\u4e0b\u4e00\u9875<br> &nbsp; &nbsp;print(pagination.prev()) &nbsp; &nbsp;# \u4e0a\u4e00\u9875\u7684\u5206\u9875\u5bf9\u8c61<br> &nbsp; &nbsp;print(pagination.next()) &nbsp; &nbsp;# \u4e0b\u4e00\u9875\u7684\u5206\u9875\u5bf9\u8c61<br>\u200b<br> &nbsp; &nbsp;# \"\"\"\u524d\u540e\u7aef\u5206\u79bb\"\"\"<br> &nbsp; &nbsp;# data = {<br> &nbsp; &nbsp;# &nbsp; &nbsp; \"page\": pagination.page, # \u5f53\u524d\u9875\u7801<br> &nbsp; &nbsp;# &nbsp; &nbsp; \"pages\": pagination.pages, # \u603b\u9875\u7801<br> &nbsp; &nbsp;# &nbsp; &nbsp; \"has_prev\": pagination.has_prev, # \u662f\u5426\u6709\u4e0a\u4e00\u9875<br> &nbsp; &nbsp;# &nbsp; &nbsp; \"prev_num\": pagination.prev_num, # \u4e0a\u4e00\u9875\u9875\u7801<br> &nbsp; &nbsp;# &nbsp; &nbsp; \"has_next\": pagination.has_next, # \u662f\u5426\u6709\u4e0b\u4e00\u9875<br> &nbsp; &nbsp;# &nbsp; &nbsp; \"next_num\": pagination.next_num, # \u4e0b\u4e00\u9875\u9875\u7801<br> &nbsp; &nbsp;# &nbsp; &nbsp; \"items\": [{<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; \"id\": item.id,<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; \"name\": item.name,<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; \"age\": item.age,<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; \"sex\": item.sex,<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; \"money\": item.money,<br> &nbsp; &nbsp;# &nbsp; &nbsp; } for item in pagination.items]<br> &nbsp; &nbsp;# }<br> &nbsp; &nbsp;# return data<br>\u200b<br>\u200b<br> &nbsp; &nbsp;\"\"\"\u524d\u540e\u7aef\u4e0d\u5206\u79bb\"\"\"<br> &nbsp; &nbsp;return render_template(\"list.html\",**locals())<br>\u200b<br>\u200b<br>\u200b<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;with app.app_context():<br> &nbsp; &nbsp; &nbsp; &nbsp;db.create_all()<br> &nbsp; &nbsp;app.run()<\/pre>\n\n\n\n<p>list.html\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;<br>&lt;html lang=\"en\"&gt;<br>&lt;head&gt;<br> &nbsp; &nbsp;&lt;meta charset=\"UTF-8\"&gt;<br> &nbsp; &nbsp;&lt;title&gt;Title&lt;\/title&gt;<br> &nbsp; &nbsp;&lt;style&gt;<br> &nbsp; &nbsp;.page a,.page span{<br> &nbsp; &nbsp; &nbsp; &nbsp;padding: 2px 6px;<br> &nbsp; &nbsp; &nbsp; &nbsp;color: #fff;<br> &nbsp; &nbsp; &nbsp; &nbsp;background: #6666ff;<br> &nbsp; &nbsp; &nbsp; &nbsp;text-decoration: none;<br> &nbsp;  }<br> &nbsp; &nbsp;.page span{<br> &nbsp; &nbsp; &nbsp; &nbsp;color: #fff;<br> &nbsp; &nbsp; &nbsp; &nbsp;background: orange;<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;&lt;\/style&gt;<br>&lt;\/head&gt;<br>&lt;body&gt;<br> &nbsp; &nbsp;&lt;table border=\"1\" align=\"center\" width=\"600\"&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;tr&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;th&gt;ID&lt;\/th&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;th&gt;age&lt;\/th&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;th&gt;name&lt;\/th&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;th&gt;sex&lt;\/th&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;th&gt;money&lt;\/th&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/tr&gt;<br> &nbsp; &nbsp; &nbsp;  {% for student in pagination.items %}<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;tr&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;{{ student.id }}&lt;\/td&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;{{ student.age }}&lt;\/td&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;{{ student.name }}&lt;\/td&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;{{ \"\u7537\" if student.sex else \"\u5973\" }}&lt;\/td&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;{{ student.money }}&lt;\/td&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/tr&gt;<br> &nbsp; &nbsp; &nbsp;  {% endfor %}<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;tr align=\"center\"&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;td colspan=\"5\" class=\"page\"&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {% if pagination.has_prev %}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;a href=\"?page=1\"&gt;\u9996  \u9875&lt;\/a&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;a href=\"?page={{ pagination.page-1 }}\"&gt;\u4e0a\u4e00\u9875&lt;\/a&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;a href=\"?page={{ pagination.page-1 }}\"&gt;{{ pagination.page-1 }}&lt;\/a&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {% endif %}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;span&gt;{{ pagination.page }}&lt;\/span&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {% if pagination.has_next %}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;a href=\"?page={{ pagination.page+1 }}\"&gt;{{ pagination.page+1 }}&lt;\/a&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;a href=\"?page={{ pagination.page+1 }}\"&gt;\u4e0b\u4e00\u9875&lt;\/a&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;a href=\"?page={{ pagination.pages }}\"&gt;\u5c3e  \u9875&lt;\/a&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {% endif %}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/td&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/tr&gt;<br> &nbsp; &nbsp;&lt;\/table&gt;<br>&lt;\/body&gt;<br>&lt;\/html&gt;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u805a\u5408\u5206\u7ec4<\/h3>\n\n\n\n<p>\u5206\u7ec4\u67e5\u8be2\u548c\u5206\u7ec4\u67e5\u8be2\u7ed3\u679c\u8fc7\u6ee4<\/p>\n\n\n\n<p>\u4e00\u822c\u5206\u7ec4\u90fd\u4f1a\u7ed3\u5408<strong>\u805a\u5408\u51fd\u6570<\/strong>\u6765\u4e00\u8d77\u4f7f\u7528\u3002SQLAlchemy\u4e2d\u6240\u6709\u7684\u805a\u5408\u51fd\u6570\u90fd\u5728<code>func<\/code>\u6a21\u5757\u4e2d\u58f0\u660e\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import func<\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u51fd\u6570\u540d<\/th><th>\u8bf4\u660e<\/th><th><\/th><\/tr><\/thead><tbody><tr><td>func.count<\/td><td>\u7edf\u8ba1\u603b\u6570<\/td><td><\/td><\/tr><tr><td>func.avg<\/td><td>\u5e73\u5747\u503c<\/td><td><\/td><\/tr><tr><td>func.min<\/td><td>\u6700\u5c0f\u503c<\/td><td><\/td><\/tr><tr><td>func.max<\/td><td>\u6700\u5927\u503c<\/td><td><\/td><\/tr><tr><td>func.sum<\/td><td>\u6c42\u548c<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u805a\u5408\u51fd\u6570\"\"\"<br>from sqlalchemy import func<br># \u83b7\u53d6\u6240\u6709\u5b66\u751f\u7684money\u603b\u6570<br># SELECT sum(db_student.money) AS sum_1 FROM db_student LIMIT %s<br># ret = db.session.query(func.sum(Student.money)).first()[0]<br># print(ret) # 3998.0<br># # \u67e5\u8be2\u5973\u751f\u7684\u6570\u91cf<br># ret = db.session.query(func.count(Student.id)).filter(Student.sex==False).first()[0]<br># print(ret) # 7<br># # \u67e5\u8be2\u6240\u6709\u5b66\u751f\u7684\u5e73\u5747\u5e74\u9f84<br># ret = db.session.query(func.avg(Student.age)).first()[0]<br># print(ret) # 18.2727<br>\u200b<br>\"\"\"<br>\u805a\u5408\u5206\u7ec4<br>\u5728\u805a\u5408\u5206\u7ec4\u7684\u60c5\u51b5\u4e0b\uff0cdb.session.query\u4e2d\u7684\u53c2\u6570\u53ea\u80fd\u662f\u88ab\u5206\u7ec4\u7684\u5b57\u6bb5\u6216\u8005\u662f\u88ab\u805a\u5408\u7684\u6570\u636e<br>\"\"\"<br># # \u67e5\u8be2\u5f53\u524d\u6240\u6709\u7537\u751f\u5973\u751f\u7684\u6570\u91cf<br># ret = db.session.query(Student.sex,func.count(Student.id)).group_by(Student.sex).all()<br># print(ret) # [(False, 7), (True, 4)]<br>\u200b<br># # \u67e5\u8be2\u5404\u4e2a\u5e74\u9f84\u6bb5\u7684\u5b66\u751f\u6570\u91cf<br># ret = db.session.query(Student.age, func.count(Student.id)).group_by(Student.age).all()<br># print(ret) # [(15, 2), (13, 1), (22, 4), (19, 1), (18, 1), (16, 1), (17, 1)]<br>#<br># # \u67e5\u770b\u5f53\u524d\u7537\u751f\u5973\u751f\u7684\u5e73\u5747\u5e74\u9f84<br># ret = db.session.query(Student.sex, func.avg(Student.age)).group_by(Student.sex).all()<br># ret = [{\"sex\":\"\u7537\" if item[0] else \"\u5973\",\"age\":float(item[1])} for item in ret]<br># print(ret) # [{'sex': '\u5973', 'age': 18.0}, {'sex': '\u7537', 'age': 18.75}]<br>\u200b<br># # \u5206\u7ec4\u540e\u7684\u8fc7\u6ee4\u64cd\u4f5c having<br># # \u5728\u6240\u6709\u5b66\u751f\u4e2d\uff0c\u627e\u51fa\u5404\u4e2a\u5e74\u9f84\u4e2d\u62e5\u6709\u6700\u591a\u94b1\u7684\u540c\u5b66\uff0c\u5e76\u5728\u8fd9\u4e9b\u540c\u5b66\u91cc\u9762\u7b5b\u9009\u51famoney &gt; 500\u7684\u6570\u636e<br># subquery = func.max(Student.money)<br># print(subquery) # max(db_student.money)<br># ret = db.session.query(Student.age, subquery).group_by(Student.age).having(subquery &gt; 500).all()<br># print(ret)  # [(18, Decimal('1000.00')), (22, Decimal('26000.00')), (23, Decimal('1998.00'))]<br>\u200b<br>\"\"\"<br>\u591a\u5b57\u6bb5\u5206\u7ec4<br> &nbsp;  \u5b57\u6bb51 &nbsp; \u5b57\u6bb52<br> &nbsp;  1 &nbsp; &nbsp;  3<br> &nbsp;  2 &nbsp; &nbsp;  4<br>\u200b<br> &nbsp;  \u5206\u7ec4\u5982\u4e0b\uff1a<br> &nbsp;  13<br> &nbsp;  14<br> &nbsp;  23<br> &nbsp;  24<br>\"\"\"<br># \u5404\u4e2a\u5e74\u9f84\u91cc\uff0c\u7537\u751f\u548c\u5973\u751f\u7684money\u603b\u6570<br>subquery = func.sum(Student.money)<br>ret = db.session.query(Student.sex, Student.age, subquery).group_by(Student.sex, Student.age).all()<br>print(ret) # [(False, 15, 1000.0), (False, 13, 600.0), (True, 15, 0.0), (True, 22, 1998.0), (False, 19, 0.0), (False, 22, 400.0), (False, 18, 0.0), (True, 16, 0.0), (False, 17, 0.0)]<\/pre>\n\n\n\n<p>SQL\u65b9\u6cd5\u4e2d\u7684\u5173\u952e\u5b57\u987a\u5e8f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6a21\u578b.query. &nbsp; \/\/ db.session.query.<br>filter\/ filter_by<br>group by<br>having<br>order_by<br>limit &nbsp;offset<br>all \/ get \/ first \/ count \/ paginate<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6267\u884c\u539f\u751fSQL\u8bed\u53e5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u6267\u884c\u539f\u751fSQL\u8bed\u53e5\"\"\"<br># # \u67e5\u8be2\u591a\u6761\u6570\u636e<br># ret = db.session.execute(\"select * from db_student\").fetchall()<br># print(ret)<br># # \u67e5\u8be2\u4e00\u6761\u6570\u636e<br># ret = db.session.execute(\"select * from db_student\").fetchone()<br># print(ret)<br>\u200b<br>\"\"\"<br> &nbsp;  name  age  achievement<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 80<br> &nbsp;  \u5c0f\u660e &nbsp; 17 &nbsp; 81<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 83<br>\u200b<br> &nbsp;  group_concat \u9017\u53f7\u5408\u5e76<br> &nbsp;  \u5c0f\u660e &nbsp; 17 &nbsp; 80,81,83<br>\u200b<br> &nbsp;  concat  \u5b57\u7b26\u4e32\u62fc\u63a5<br> &nbsp;  \u5c0f\u660e &nbsp; 17 &nbsp; 808183<br> &nbsp;  \"\"\"<br>\u200b<br># # \u6dfb\u52a0\u6570\u636e<br># db.session.execute(\"insert db_student (name,age,sex,email,money) select name,age,sex,concat(now(),email),money from db_student\")<br># db.session.commit()<br>\u200b<br># # # \u66f4\u65b0\/\u5220\u9664<br># db.session.execute(\"UPDATE db_student SET money=(db_student.money + %s) WHERE db_student.age = %s\" % (200, 22))<br># db.session.commit()<br>\u200b<br>\u200b<br>\"\"\"\u5206\u7ec4\u5408\u5e76\"\"\"<br># \u7edf\u8ba1\u5404\u4e2a\u5e74\u9f84\u6bb5\u7684\u5b66\u751f\u4eba\u6570\uff0c\u5e76\u8bb0\u5f55\u5bf9\u5e94\u5e74\u9f84\u6bb5\u7684\u5b66\u751fID<br>ret = db.session.execute(\"select age,count(id),group_concat(id) from db_student group by age\").fetchall()<br>print(ret)<br>return \"ok\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5173\u8054\u67e5\u8be2<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u7528\u7684SQLAlchemy\u5173\u7cfb\u9009\u9879<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9009\u9879\u540d<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>backref<\/td><td>\u5728\u5173\u7cfb\u7684\u53e6\u4e00\u6a21\u578b\u4e2d\u6dfb\u52a0<strong>\u53cd\u5411\u5f15\u7528<\/strong>,\u7528\u4e8e\u8bbe\u7f6e\u5916\u952e\u540d\u79f0,\u57281\u67e5\u591a\u7684<\/td><\/tr><tr><td>primary join<\/td><td>\u660e\u786e\u6307\u5b9a\u4e24\u4e2a\u6a21\u578b\u4e4b\u95f4\u4f7f\u7528\u7684\u8fde\u8868\u6761\u4ef6, \u7528\u4e8e1\u5bf91 \u6216\u80051\u5bf9\u591a\u8fde\u8868\u4e2d<\/td><\/tr><tr><td>lazy<\/td><td>\u6307\u5b9a\u5982\u4f55\u52a0\u8f7d\u5173\u8054\u6a21\u578b\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e1\u5bf91\u62161\u5bf9\u591a\u94fe\u8868\u4e2d\u3002\u53c2\u6570\u503c: select\uff08\u7acb\u5373\u52a0\u8f7d\uff0c\u67e5\u8be2\u6240\u6709\u76f8\u5173\u6570\u636e\u663e\u793a\uff0c\u76f8\u5f53\u4e8elazy=True\uff09 subquery\uff08\u7acb\u5373\u52a0\u8f7d\uff0c\u4f46\u4f7f\u7528\u5b50\u67e5\u8be2\uff09 dynamic\uff08\u4e0d\u7acb\u5373\u52a0\u8f7d\uff0c\u4f46\u63d0\u4f9b\u52a0\u8f7d\u8bb0\u5f55\u7684\u67e5\u8be2\u5bf9\u8c61\uff09<\/td><\/tr><tr><td>uselist<\/td><td>\u6307\u5b9a1\u5bf91\u62161\u5bf9\u591a\u8fde\u8868\u65f6\uff0c\u8fd4\u56de\u7684\u6570\u636e\u7ed3\u679c\u662f\u6a21\u578b\u5bf9\u8c61\u8fd8\u662f\u6a21\u578b\u5217\u8868\uff0c\u5982\u679c\u4e3aFalse\uff0c\u4e0d\u4f7f\u7528\u5217\u8868\uff0c\u800c\u4f7f\u7528\u6a21\u578b\u5bf9\u8c61\u3002 1\u5bf91\u6216\u591a\u5bf91\u5173\u7cfb\u4e2d\uff0c\u9700\u8981\u8bbe\u7f6erelationship\u4e2d\u7684uselist=Flase\uff0c1\u5bf9\u591a\u6216\u591a\u5bf9\u591a\u5173\u7cfb\u4e2d\uff0c\u9700\u8981\u8bbe\u7f6erelationshio\u4e2d\u7684uselist=True\u3002<\/td><\/tr><tr><td>secondary<\/td><td>\u6307\u5b9a\u591a\u5bf9\u591a\u5173\u7cfb\u4e2d\u5173\u7cfb\u8868\u7684\u540d\u5b57\u3002 \u591a\u5bf9\u591a\u5173\u7cfb\u4e2d\uff0c\u9700\u5efa\u7acb\u5173\u7cfb\u8868\uff0c\u8bbe\u7f6e secondary=\u5173\u7cfb\u8868<\/td><\/tr><tr><td>secondary join<\/td><td>\u5728SQLAlchemy\u4e2d\u65e0\u6cd5\u81ea\u884c\u51b3\u5b9a\u65f6\uff0c\u6307\u5b9a\u591a\u5bf9\u591a\u5173\u7cfb\u4e2d\u7684\u4e8c\u7ea7\u8fde\u8868\u6761\u4ef6\uff0c\u7ed1\u5b9a\u4e3b\u5916\u952e\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8303\u5f0f\u7406\u8bba\uff1a\u4e00\u5957\u63d0\u4f9b\u7ed9\u6570\u636e\u5e93\u5f00\u53d1\u8005\u8bbe\u7f6e\u6807\u51c6\u3001\u89c4\u8303\u7684\u6570\u636e\u5e93\u7684\u7406\u8bba\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1NF. \u6570\u636e\u4e0d\u53ef\u518d\u5206\uff0c\u5fc5\u987b\u4fdd\u8bc1\u539f\u5b50\u6027\u3002\u6570\u636e\u7684\u503c\u4fdd\u8bc1\u53ef\u4ee5\u65b9\u4fbf\u5b58\u50a8\uff0c\u4e0d\u53ef\u518d\u5206\u3002<br>2NF. \u6570\u636e\u4e0d\u80fd\u91cd\u590d\uff0c\u5fc5\u987b\u4fdd\u8bc1\u552f\u4e00\u6027\u3002\u5fc5\u987b\u4f7f\u7528\u4e3b\u952e\u6765\u8fdb\u884c\u533a\u5206\u6bcf\u4e00\u884c\u6570\u636e\u3002<br>3NF. \u6570\u636e\u4e0d\u80fd\u5197\u4f59\uff0c\u5fc5\u987b\u4fdd\u8bc1\u5173\u8054\u6027\u3002\u5197\u4f59\u7684\u6570\u636e\u5fc5\u987b\u4f7f\u7528\u53e6\u5916\u7684\u6570\u636e\u8868\u5b58\u653e\uff0c\u5e76\u4e0e\u5f53\u524d\u8868\u8fdb\u884c\u5173\u8054\u3002<br>\u200b<br>\u57fa\u4e8e\u5b9e\u9645\u4e1a\u52a1\u7684\u89d2\u5ea6\u51fa\u53d1\uff0c\u8bbe\u8ba1\u51fa\u8fdd\u80cc\u4e86\u8303\u5f0f\u7406\u8bba\u7684\u8868\u7ed3\u6784\u3002<br>\u9006\u8303\u5f0f\uff1a\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4<br>\u200b<br>class table:<br> &nbsp;  id &nbsp; name &nbsp; &nbsp;  teacher &nbsp; &nbsp; &nbsp;<br> &nbsp;  1 &nbsp;  301\u73ed &nbsp; &nbsp;  \u674e\u8001\u5e08 &nbsp; &nbsp; &nbsp; <br> &nbsp;  2 &nbsp;  302\u73ed &nbsp; &nbsp;  \u738b\u8001\u5e08 &nbsp; &nbsp; &nbsp;<br>\u200b<br>student table:<br> &nbsp;  id &nbsp; name &nbsp; &nbsp; &nbsp;  class_id<br> &nbsp;  1 &nbsp;  xiaoming &nbsp;  1<br> &nbsp;  2 &nbsp;  xiaohong &nbsp;  2<br> &nbsp;  3 &nbsp;  xiaohui &nbsp; &nbsp; 3<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;<br>\u200b<\/pre>\n\n\n\n<p>\u4e09\u8303\u5f0f+\u9006\u8303\u5f0f<\/p>\n\n\n\n<p>\u7b2c\u4e09\u8303\u5f0f\uff1a\u6570\u636e\u4e0d\u80fd\u5197\u4f59\uff0c\u628a\u5173\u8054\u6027\u4e0d\u5f3a\u7684\u6570\u636e\u53ef\u4ee5\u79fb\u9664\u5230\u53e6\u4e00\u4e2a\u8868\u4e2d\u3002\u4f7f\u7528\u5916\u952e\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\u5bf91\uff1a\u628a\u4e3b\u8868\u7684\u4e3b\u952e\u653e\u5230\u9644\u52a0\u8868\u4e2d\u4f5c\u4e3a\u5916\u952e\u5b58\u5728\u3002<br>\u200b<br> &nbsp; &nbsp; &nbsp;\u5546\u54c1\u4fe1\u606f\u8868<br> &nbsp; &nbsp; &nbsp;id &nbsp; name &nbsp; &nbsp; &nbsp; &nbsp;price &nbsp; image &nbsp; &nbsp;\u63cf\u8ff0 &nbsp; &nbsp;\u552e\u540e &nbsp; \u914d\u7f6e &nbsp; &nbsp;\u5305\u88c5<br> &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp;PC-1-gr &nbsp; &nbsp; &nbsp;100 &nbsp; &nbsp;1.png &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp;PC-1-re &nbsp; &nbsp; &nbsp;100 &nbsp; &nbsp;1.png<br> &nbsp; &nbsp; &nbsp;3 &nbsp; &nbsp;PC-1-he &nbsp; &nbsp; &nbsp;100 &nbsp; &nbsp;1.png<br> &nbsp; &nbsp; &nbsp;4 &nbsp; &nbsp;PC-1-bu &nbsp; &nbsp; &nbsp;100 &nbsp; &nbsp;1.png<br>\u200b<br>1\u5bf9\u591a\uff1a\u628a\u4e3b\u8868(1) \u7684\u4e3b\u952e\u653e\u5230\u9644\u52a0\u8868(\u591a)\u4f5c\u4e3a\u5916\u952e\u5b58\u5728\u3002<br> &nbsp; &nbsp; \u8ba2\u5355\u4fe1\u606f\u8868 &nbsp; &nbsp; &nbsp; &nbsp;\u8ba2\u5355\u8be6\u60c5\u8868<br> &nbsp; &nbsp; 1\u4e2a\u8ba2\u5355 &nbsp; &nbsp; ----&gt; \u591a\u4e2a\u5546\u54c1<br>\u200b<br>\u591a\u5bf9\u591a\uff1a\u628a\u4e3b\u8868(\u591a)\u7684\u4e3b\u952e\u548c\u9644\u52a0\u8868\u7684(\u591a)\u4e3b\u952e\uff0c\u653e\u5230\u7b2c\u4e09\u65b9\u8868(\u5173\u7cfb\u8868)\u4e2d\u4f5c\u4e3a\u5916\u952e\u3002<br> &nbsp; &nbsp; \u7528\u6237\u8868 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\u8bfe\u7a0b\u8868<br> &nbsp; &nbsp; &nbsp;1 &nbsp;xiaoming &nbsp; &nbsp; &nbsp;1 &nbsp; python<br> &nbsp; &nbsp; &nbsp;2 &nbsp;xiaohong &nbsp; &nbsp; &nbsp;2 &nbsp; django<br> &nbsp; &nbsp; &nbsp;3 &nbsp;xiaolong &nbsp; &nbsp; &nbsp;3 &nbsp; flask<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; \u7528\u6237\u4e0e\u8bfe\u7a0b\u7684\u8d2d\u4e70\u5173\u7cfb\u8868<br> &nbsp; &nbsp; user_id &nbsp; course<br> &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br> &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2<br> &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u8054<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e00\u5bf9\u4e00<\/h4>\n\n\n\n<p>\u5e38\u89c1\u7684\u4e1a\u52a1\uff1a\u4e3b\u8868\u548c\u8be6\u60c5\u8868\uff08\u7528\u6237\u3001\u4f1a\u5458\u3001\u5b66\u751f\u3001\u5546\u54c1\u3001\u6587\u7ae0\u3001\u4e3b\u673a\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u4e2a\u4eba\u4fe1\u606f\u4e3b\u8868\"\"\"<br>    ....<br> &nbsp; &nbsp;# \u5173\u8054\u5c5e\u6027\uff0c\u8fd9\u4e2a\u4e0d\u4f1a\u88ab\u89c6\u4f5c\u8868\u5b57\u6bb5\uff0c\u53ea\u662f\u6a21\u578b\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<br> &nbsp; &nbsp;# \u56e0\u4e3aStudentInfo\u548cStudent\u662f\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u6240\u4ee5uselist=False\u8868\u793a\u5173\u8054\u4e00\u4e2a\u6570\u636e<br> &nbsp; &nbsp;info = db.relationship(\"StudentInfo\", uselist=False, backref=\"own\")<br>\u200b<br>\u200b<br>class StudentInfo(db.Model):<br> &nbsp; &nbsp;\"\"\"\u4e2a\u4eba\u4fe1\u606f\u9644\u52a0\u8868\"\"\"<br>\u200b<br> &nbsp; &nbsp;# \u5916\u952e\uff0c<br> &nbsp; &nbsp;# \u5982\u679c\u662f\u4e00\u5bf9\u4e00\uff0c\u5219\u5916\u952e\u653e\u5728\u9644\u52a0\u8868\u5bf9\u5e94\u7684\u6a21\u578b\u4e2d<br> &nbsp; &nbsp;# \u5982\u679c\u662f\u4e00\u5bf9\u591a\uff0c\u5219\u5916\u952e\u653e\u5728\u591a\u7684\u8868\u5bf9\u8c61\u7684\u6a21\u578b\u4e2d<br> &nbsp; &nbsp;# sid = db.Column(db.Integer, db.ForeignKey(Student.id),comment=\"\u5916\u952e\")<br> &nbsp; &nbsp;sid = db.Column(db.Integer, db.ForeignKey(\"student\u8868\u540d.\u4e3b\u952e\"),comment=\"\u5916\u952e\")<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5173\u8054\u5c5e\u6027\u58f0\u660e\u5728\u4e3b\u6a21\u578b\u4e2d\u3010\u6700\u5e38\u7528\u3011<\/h5>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from sqlalchemy import func<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u7ba1\u7406\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"tb_student\" &nbsp;# \u8868\u540d<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), index=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.SmallInteger, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(255), unique=True, comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10,2), default=0.0, comment=\"\u94b1\u5305\")<br> &nbsp; &nbsp;# \u6a21\u578b\u7684\u5173\u8054\u5c5e\u6027\uff0c\u4e0d\u4f1a\u5728\u6570\u636e\u8868\u521b\u5efa\u5b57\u6bb5<br> &nbsp; &nbsp;# \u56e0\u4e3aStudentInfo\u548cStudent\u662f\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u6240\u4ee5uselist=False\u8868\u793a\u5173\u8054\u4e00\u4e2a\u6570\u636e<br> &nbsp; &nbsp;info = db.relationship(\"StudentInfo\", uselist=False, backref=\"student\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name\": self.name,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"age\": self.age,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"sex\": self.sex,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"email\": self.email,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"money\": float(self.money),<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>class StudentInfo(db.Model):<br> &nbsp; &nbsp;__tablename__ = \"tb_student_info\" &nbsp;# \u8868\u540d<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;# \u9644\u52a0\u8868\u7684\u5916\u952e\uff0c\u5c31\u662f\u4e3b\u8868\u7684\u4e3b\u952e<br> &nbsp; &nbsp;# \u5982\u679c\u662f\u4e00\u5bf9\u4e00\uff0c\u5219\u5916\u952e\u653e\u5728\u9644\u52a0\u8868\u5bf9\u5e94\u7684\u6a21\u578b\u4e2d<br> &nbsp; &nbsp;# \u5982\u679c\u662f\u4e00\u5bf9\u591a\uff0c\u5219\u5916\u952e\u653e\u5728\u591a\u7684\u9644\u52a0\u8868\u5bf9\u5e94\u7684\u6a21\u578b\u4e2d<br> &nbsp; &nbsp;sid = db.Column(db.Integer, db.ForeignKey(\"tb_student.id\"), comment=\"\u5916\u952e\") &nbsp;# mysql\u5b57\u6bb5<br> &nbsp; &nbsp;# student = db.relationship(\"Student\", uselist=False, backref=\"info\") &nbsp; &nbsp; # ORM\u5173\u8054\u5c5e\u6027<br> &nbsp; &nbsp;mobile = db.Column(db.String(15), index=True, comment=\"\u624b\u673a\u53f7\u7801\")<br> &nbsp; &nbsp;address = db.Column(db.String(255), nullable=True, comment=\"\u5bb6\u5ead\u5730\u5740\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"sid\": self.sid,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"mobile\": self.mobile,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"address\": self.address,<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp; # \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a1\")<br>def a1():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# \u6dfb\u52a0\u4e3b\u8868\u4fe1\u606f\u7684\u65f6\u5019\u901a\u8fc7\u5173\u8054\u5c5e\u6027db.relationship\u540c\u6b65\u6dfb\u52a0\u9644\u4ef6\u8868\u4fe1\u606f<br> &nbsp; &nbsp;student = Student(<br> &nbsp; &nbsp; &nbsp; &nbsp;name=\"xiaolan01\",<br> &nbsp; &nbsp; &nbsp; &nbsp;age=16,<br> &nbsp; &nbsp; &nbsp; &nbsp;sex=False,<br> &nbsp; &nbsp; &nbsp; &nbsp;money=10000,<br> &nbsp; &nbsp; &nbsp; &nbsp;email=\"xiaolan01@qq.com\",<br> &nbsp; &nbsp; &nbsp; &nbsp;info=StudentInfo(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def203\u53f7\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mobile=\"13312345672\"<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  )<br>\u200b<br> &nbsp; &nbsp;db.session.add(student)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a2\")<br>def a2():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# \u5047\u8bbe\u5df2\u7ecf\u5b58\u5728\u4e3b\u8868\u4fe1\u606f\uff0c\u540e\u9762\u518d\u8865\u5145\u9644\u52a0\u8868\u4fe1\u606f<br> &nbsp; &nbsp;# student = Student(<br> &nbsp; &nbsp;# &nbsp; &nbsp; name=\"xiaolan03\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; age=16,<br> &nbsp; &nbsp;# &nbsp; &nbsp; sex=False,<br> &nbsp; &nbsp;# &nbsp; &nbsp; money=10000,<br> &nbsp; &nbsp;# &nbsp; &nbsp; email=\"xiaolan03@qq.com\",<br> &nbsp; &nbsp;# )<br> &nbsp; &nbsp;#<br> &nbsp; &nbsp;# db.session.add(student)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# \u4e0a\u9762\u4ee3\u7801\u5148\u6267\u884c\u4e86\uff0c\u73b0\u67091\u4e2a\u5b66\u751f\u6ca1\u6709\u9644\u52a0\u4fe1\u606f\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u540e\u7eed\u4ee3\u7801\u901a\u8fc7\u67e5\u8be2\u4e3b\u8868\u7684\u4e3b\u952e\uff0c\u8865\u5145\u9644\u52a0\u8868\u6570\u636e<br> &nbsp; &nbsp;student = Student.query.get(3)<br> &nbsp; &nbsp;if not student.info:<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u9644\u52a0\u8868\u6570\u636e\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u65b9\u5f0f1\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# student.info = StudentInfo(mobile=13300010002, address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def205\u53f7\",)<br> &nbsp; &nbsp; &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u65b9\u5f0f2\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# info = StudentInfo(<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; student=student,  # \u5173\u8054\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; mobile=13300010002,<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def205\u53f7\",<br> &nbsp; &nbsp; &nbsp; &nbsp;# )<br> &nbsp; &nbsp; &nbsp; &nbsp;#<br> &nbsp; &nbsp; &nbsp; &nbsp;# db.session.add(info)<br> &nbsp; &nbsp; &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u65b9\u5f0f3\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;info = StudentInfo(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sid=student.id, &nbsp;# \u5916\u952e<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mobile=13300010002,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def205\u53f7\",<br> &nbsp; &nbsp; &nbsp;  )<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;db.session.add(info)<br> &nbsp; &nbsp; &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a3\")<br>def a3():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# \u6dfb\u52a0\u9644\u52a0\u6a21\u578b\u6570\u636e\u7684\u540c\u65f6\uff0c\u628a\u4e3b\u6a21\u578b\u4e5f\u8fdb\u884c\u6dfb\u52a0<br> &nbsp; &nbsp;info = StudentInfo(<br> &nbsp; &nbsp; &nbsp; &nbsp;mobile=13300010003,<br> &nbsp; &nbsp; &nbsp; &nbsp;address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def206\u53f7\",<br> &nbsp; &nbsp; &nbsp; &nbsp;student=Student(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name=\"xiaolan04\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;age=17,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sex=False,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;money=10000,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;email=\"xiaolan04@qq.com\",<br> &nbsp; &nbsp; &nbsp;  ),<br> &nbsp;  )<br>\u200b<br> &nbsp; &nbsp;db.session.add(info)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/g1\")<br>def g1():<br> &nbsp; &nbsp;\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# # \u6b63\u5411\u5173\u8054----&gt; \u4ece\u4e3b\u6a21\u578b\u67e5\u8be2\u5916\u952e\u6a21\u578b<br> &nbsp; &nbsp;# student = Student.query.get(2)<br> &nbsp; &nbsp;# print(student.name) &nbsp; &nbsp; &nbsp; &nbsp;  # xiaolan01<br> &nbsp; &nbsp;# # \u5982\u679c\u4e3b\u6a21\u578b\u6ca1\u6709\u8c03\u7528\u5173\u8054\u5c5e\u6027\u67e5\u8be2\u9644\u52a0\u6a21\u578b\u5bf9\u8c61\uff0c\u5219ORM\u4e0d\u4f1a\u6267\u884c\u67e5\u8be2\u5173\u8054\u6a21\u578b\u7684SQL\u8bed\u53e5<br> &nbsp; &nbsp;# print(student.info) &nbsp; &nbsp; &nbsp; &nbsp;  # {\"id\": 2, \"sid\": 2, \"mobile\": \"13312345672\", \"address\": \"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def203\u53f7\"}<br> &nbsp; &nbsp;# print(student.info.address)  # \u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def203\u53f7<br>\u200b<br> &nbsp; &nbsp;# \u53cd\u5411\u5173\u8054----&gt; \u4ece\u5916\u952e\u6a21\u578b\u67e5\u8be2\u4e3b\u6a21\u578b<br> &nbsp; &nbsp;student_info = StudentInfo.query.filter(StudentInfo.mobile == \"15012345678\").first()<br> &nbsp; &nbsp;print(student_info.address) &nbsp; &nbsp; &nbsp; # \u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def206\u53f7<br> &nbsp; &nbsp;# \u5982\u679c\u9644\u52a0\u6a21\u578b\u6ca1\u6709\u8c03\u7528\u5173\u8054\u5c5e\u6027\u67e5\u8be2\u4e3b\u6a21\u578b\u5bf9\u8c61\uff0c\u5219ORM\u4e0d\u4f1a\u6267\u884c\u67e5\u8be2\u4e3b\u6a21\u578b\u7684SQL\u8bed\u53e5<br> &nbsp; &nbsp;print(student_info.student) &nbsp; &nbsp; &nbsp; # {\"id\": 4, \"name\": \"xiaolan04\", \"age\": 17, \"sex\": 0, \"email\": \"xiaolan04@qq.com\", \"money\": 10000.0}<br> &nbsp; &nbsp;print(student_info.student.name) &nbsp;# xiaolan04<br> &nbsp; &nbsp;print(student_info.sid) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 4<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/u1\")<br>def u1():<br> &nbsp; &nbsp;\"\"\"\u4fee\u6539\u6570\u636e\"\"\"<br> &nbsp; &nbsp;# # \u901a\u8fc7\u4e3b\u6a21\u578b\u4f7f\u7528\u5173\u8054\u5c5e\u6027\u4fee\u6539\u9644\u52a0\u6a21\u578b\u7684\u6570\u636e<br> &nbsp; &nbsp;# student = Student.query.get(2)<br> &nbsp; &nbsp;# student.info.address = \"\u5e7f\u5dde\u5e02\u5929\u6cb3\u533a\u5929\u6cb3\u4e1c\u8def103\u53f7\"<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# \u4e5f\u53ef\u4ee5\u901a\u8fc7\u9644\u52a0\u6a21\u578b\u76f4\u63a5\u4fee\u6539\u4e3b\u6a21\u578b\u7684\u6570\u636e<br> &nbsp; &nbsp;student_info = StudentInfo.query.filter(StudentInfo.mobile == \"13312345678\").first()<br> &nbsp; &nbsp;# \u5982\u679c\u8981\u4fee\u6539\u7684\u6570\u636e\uff0c\u4e0e\u6570\u636e\u8868\u7684\u6ca1\u6709\u6539\u52a8\uff0c\u5219\u4e0d\u4f1a\u6267\u884c\u66f4\u65b0\u7684SQL\u8bed\u53e5<br> &nbsp; &nbsp;student_info.student.age = 23<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/d1\")<br>def d1():<br> &nbsp; &nbsp;\"\"\"\u5220\u9664\u6570\u636e\"\"\"<br>\u200b<br> &nbsp; &nbsp;# # 1. \u5982\u679c\u5220\u9664\u4e3b\u6a21\u578b\u6570\u636e\uff0c\u5219\u4f1a\u5148\u5c06\u9644\u52a0\u6a21\u578b\u7684\u5916\u952e\u8bbe\u7f6e\u4e3anull\uff0c\u7136\u540e\u624d\u4f1a\u5220\u9664\u4e3b\u6a21\u578b\u5bf9\u8c61<br> &nbsp; &nbsp;# student = Student.query.get(4)<br> &nbsp; &nbsp;# db.session.delete(student)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# 2. \u5982\u679c\u5220\u9664\u9644\u52a0\u6a21\u578b\u6570\u636e\uff0c\u5219\u76f4\u63a5\u5220\u9664\uff0c\u4e0d\u4f1a\u4fee\u6539\u4e3b\u6a21\u578b\u6570\u636e<br> &nbsp; &nbsp;StudentInfo.query.filter(StudentInfo.mobile == \"13312345678\").delete()<br> &nbsp; &nbsp;db.session.commit()<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5728\u5916\u952e\u6a21\u578b\u4e2d\u58f0\u660e\u5173\u8054\u5c5e\u6027<\/h5>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from sqlalchemy.orm import backref<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u7ba1\u7406\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"tb_student\" &nbsp;# \u8868\u540d<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), index=True, comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.SmallInteger, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(255), unique=True, comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10, 2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name\": self.name,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"age\": self.age,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"sex\": self.sex,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"email\": self.email,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"money\": float(self.money),<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>class StudentInfo(db.Model):<br> &nbsp; &nbsp;__tablename__ = \"tb_student_info\" &nbsp;# \u8868\u540d<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;# \u9644\u52a0\u8868\u7684\u5916\u952e\uff0c\u5c31\u662f\u4e3b\u8868\u7684\u4e3b\u952e<br> &nbsp; &nbsp;# \u5982\u679c\u662f\u4e00\u5bf9\u4e00\uff0c\u5219\u5916\u952e\u653e\u5728\u9644\u52a0\u8868\u5bf9\u5e94\u7684\u6a21\u578b\u4e2d<br> &nbsp; &nbsp;# \u5982\u679c\u662f\u4e00\u5bf9\u591a\uff0c\u5219\u5916\u952e\u653e\u5728\u591a\u7684\u9644\u52a0\u8868\u5bf9\u5e94\u7684\u6a21\u578b\u4e2d<br> &nbsp; &nbsp;sid = db.Column(db.Integer, db.ForeignKey(\"tb_student.id\"), comment=\"\u5916\u952e\") &nbsp;# mysql\u5b57\u6bb5<br> &nbsp; &nbsp;# \u6a21\u578b\u7684\u5173\u8054\u5c5e\u6027\uff0c\u4e0d\u4f1a\u5728\u6570\u636e\u8868\u521b\u5efa\u5b57\u6bb5<br> &nbsp; &nbsp;# \u56e0\u4e3aStudentInfo\u548cStudent\u662f\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u6240\u4ee5uselist=False\u8868\u793a\u5173\u8054\u4e00\u4e2a\u6570\u636e<br> &nbsp; &nbsp;student = db.relationship(\"Student\", uselist=False, backref=backref(\"info\", uselist=False)) &nbsp;# ORM\u5173\u8054\u5c5e\u6027<br> &nbsp; &nbsp;mobile = db.Column(db.String(15), index=True, comment=\"\u624b\u673a\u53f7\u7801\")<br> &nbsp; &nbsp;address = db.Column(db.String(255), nullable=True, comment=\"\u5bb6\u5ead\u5730\u5740\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"sid\": self.sid,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"mobile\": self.mobile,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"address\": self.address,<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a1\")<br>def a1():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# \u6dfb\u52a0\u4e3b\u8868\u4fe1\u606f\u7684\u65f6\u5019\u901a\u8fc7\u5173\u8054\u5c5e\u6027db.relationship\u540c\u6b65\u6dfb\u52a0\u9644\u4ef6\u8868\u4fe1\u606f<br> &nbsp; &nbsp;student = Student(<br> &nbsp; &nbsp; &nbsp; &nbsp;name=\"xiaolan01\",<br> &nbsp; &nbsp; &nbsp; &nbsp;age=16,<br> &nbsp; &nbsp; &nbsp; &nbsp;sex=False,<br> &nbsp; &nbsp; &nbsp; &nbsp;money=10000,<br> &nbsp; &nbsp; &nbsp; &nbsp;email=\"xiaolan01@qq.com\",<br> &nbsp; &nbsp; &nbsp; &nbsp;info=StudentInfo(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def203\u53f7\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mobile=\"13312345672\"<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  )<br>\u200b<br> &nbsp; &nbsp;db.session.add(student)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a2\")<br>def a2():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# \u5047\u8bbe\u5df2\u7ecf\u5b58\u5728\u4e3b\u8868\u4fe1\u606f\uff0c\u540e\u9762\u518d\u8865\u5145\u9644\u52a0\u8868\u4fe1\u606f<br> &nbsp; &nbsp;# student = Student(<br> &nbsp; &nbsp;# &nbsp; &nbsp; name=\"xiaolan03\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; age=16,<br> &nbsp; &nbsp;# &nbsp; &nbsp; sex=False,<br> &nbsp; &nbsp;# &nbsp; &nbsp; money=10000,<br> &nbsp; &nbsp;# &nbsp; &nbsp; email=\"xiaolan03@qq.com\",<br> &nbsp; &nbsp;# )<br> &nbsp; &nbsp;#<br> &nbsp; &nbsp;# db.session.add(student)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# \u4e0a\u9762\u4ee3\u7801\u5148\u6267\u884c\u4e86\uff0c\u73b0\u67091\u4e2a\u5b66\u751f\u6ca1\u6709\u9644\u52a0\u4fe1\u606f\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u540e\u7eed\u4ee3\u7801\u901a\u8fc7\u67e5\u8be2\u4e3b\u8868\u7684\u4e3b\u952e\uff0c\u8865\u5145\u9644\u52a0\u8868\u6570\u636e<br> &nbsp; &nbsp;student = Student.query.get(2)<br> &nbsp; &nbsp;if student and not student.info:<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u9644\u52a0\u8868\u6570\u636e\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u65b9\u5f0f1\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# student.info = StudentInfo(mobile=13300010002, address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def205\u53f7\",)<br> &nbsp; &nbsp; &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u65b9\u5f0f2\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# info = StudentInfo(<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; student=student,  # \u5173\u8054\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; mobile=13300010002,<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def205\u53f7\",<br> &nbsp; &nbsp; &nbsp; &nbsp;# )<br> &nbsp; &nbsp; &nbsp; &nbsp;#<br> &nbsp; &nbsp; &nbsp; &nbsp;# db.session.add(info)<br> &nbsp; &nbsp; &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u65b9\u5f0f3\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;info = StudentInfo(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sid=student.id, &nbsp;# \u5916\u952e<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mobile=13300010002,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def205\u53f7\",<br> &nbsp; &nbsp; &nbsp;  )<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;db.session.add(info)<br> &nbsp; &nbsp; &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a3\")<br>def a3():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# \u6dfb\u52a0\u9644\u52a0\u6a21\u578b\u6570\u636e\u7684\u540c\u65f6\uff0c\u628a\u4e3b\u6a21\u578b\u4e5f\u8fdb\u884c\u6dfb\u52a0<br> &nbsp; &nbsp;info = StudentInfo(<br> &nbsp; &nbsp; &nbsp; &nbsp;mobile=13300010003,<br> &nbsp; &nbsp; &nbsp; &nbsp;address=\"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def206\u53f7\",<br> &nbsp; &nbsp; &nbsp; &nbsp;student=Student(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name=\"xiaolan04\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;age=17,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sex=False,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;money=10000,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;email=\"xiaolan04@qq.com\",<br> &nbsp; &nbsp; &nbsp;  ),<br> &nbsp;  )<br>\u200b<br> &nbsp; &nbsp;db.session.add(info)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/g1\")<br>def g1():<br> &nbsp; &nbsp;\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# # \u6b63\u5411\u5173\u8054----&gt; \u4ece\u4e3b\u6a21\u578b\u67e5\u8be2\u5916\u952e\u6a21\u578b<br> &nbsp; &nbsp;# student = Student.query.get(2)<br> &nbsp; &nbsp;# print(student.name) &nbsp; &nbsp; &nbsp; &nbsp;  # xiaolan01<br> &nbsp; &nbsp;# # \u5982\u679c\u4e3b\u6a21\u578b\u6ca1\u6709\u8c03\u7528\u5173\u8054\u5c5e\u6027\u67e5\u8be2\u9644\u52a0\u6a21\u578b\u5bf9\u8c61\uff0c\u5219ORM\u4e0d\u4f1a\u6267\u884c\u67e5\u8be2\u5173\u8054\u6a21\u578b\u7684SQL\u8bed\u53e5<br> &nbsp; &nbsp;# print(student.info) &nbsp; &nbsp; &nbsp; &nbsp;  # {\"id\": 2, \"sid\": 2, \"mobile\": \"13312345672\", \"address\": \"\u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def203\u53f7\"}<br> &nbsp; &nbsp;# print(student.info.address)  # \u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def203\u53f7<br>\u200b<br> &nbsp; &nbsp;# \u53cd\u5411\u5173\u8054----&gt; \u4ece\u5916\u952e\u6a21\u578b\u67e5\u8be2\u4e3b\u6a21\u578b<br> &nbsp; &nbsp;student_info = StudentInfo.query.filter(StudentInfo.mobile == \"13300010003\").first()<br> &nbsp; &nbsp;print(student_info.address) &nbsp;# \u5317\u4eac\u5e02\u660c\u5e73\u533a\u767e\u6c99\u8def206\u53f7<br> &nbsp; &nbsp;# \u5982\u679c\u9644\u52a0\u6a21\u578b\u6ca1\u6709\u8c03\u7528\u5173\u8054\u5c5e\u6027\u67e5\u8be2\u4e3b\u6a21\u578b\u5bf9\u8c61\uff0c\u5219ORM\u4e0d\u4f1a\u6267\u884c\u67e5\u8be2\u4e3b\u6a21\u578b\u7684SQL\u8bed\u53e5<br> &nbsp; &nbsp;print(<br> &nbsp; &nbsp; &nbsp; &nbsp;student_info.student) &nbsp;# {\"id\": 3, \"name\": \"xiaolan04\", \"age\": 17, \"sex\": 0, \"email\": \"xiaolan04@qq.com\", \"money\": 10000.0}<br> &nbsp; &nbsp;print(student_info.student.name) &nbsp;# xiaolan04<br> &nbsp; &nbsp;print(student_info.sid) &nbsp;# 3<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/u1\")<br>def u1():<br> &nbsp; &nbsp;\"\"\"\u4fee\u6539\u6570\u636e\"\"\"<br> &nbsp; &nbsp;# \u901a\u8fc7\u4e3b\u6a21\u578b\u4f7f\u7528\u5173\u8054\u5c5e\u6027\u4fee\u6539\u9644\u52a0\u6a21\u578b\u7684\u6570\u636e<br> &nbsp; &nbsp;student = Student.query.get(2)<br> &nbsp; &nbsp;student.info.address = \"\u5e7f\u5dde\u5e02\u5929\u6cb3\u533a\u5929\u6cb3\u4e1c\u8def103\u53f7\"<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# # \u4e5f\u53ef\u4ee5\u901a\u8fc7\u9644\u52a0\u6a21\u578b\u76f4\u63a5\u4fee\u6539\u4e3b\u6a21\u578b\u7684\u6570\u636e<br> &nbsp; &nbsp;# student_info = StudentInfo.query.filter(StudentInfo.mobile == \"13312345672\").first()<br> &nbsp; &nbsp;# # \u5982\u679c\u8981\u4fee\u6539\u7684\u6570\u636e\uff0c\u4e0e\u6570\u636e\u8868\u7684\u6ca1\u6709\u6539\u52a8\uff0c\u5219\u4e0d\u4f1a\u6267\u884c\u66f4\u65b0\u7684SQL\u8bed\u53e5<br> &nbsp; &nbsp;# student_info.student.age = 23<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/d1\")<br>def d1():<br> &nbsp; &nbsp;\"\"\"\u5220\u9664\u6570\u636e\"\"\"<br>\u200b<br> &nbsp; &nbsp;# # 1. \u5982\u679c\u5220\u9664\u4e3b\u6a21\u578b\u6570\u636e\uff0c\u5219\u4f1a\u5148\u5c06\u9644\u52a0\u6a21\u578b\u7684\u5916\u952e\u8bbe\u7f6e\u4e3anull\uff0c\u7136\u540e\u624d\u4f1a\u5220\u9664\u4e3b\u6a21\u578b\u5bf9\u8c61<br> &nbsp; &nbsp;student = Student.query.get(2)<br> &nbsp; &nbsp;db.session.delete(student)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# 2. \u5982\u679c\u5220\u9664\u9644\u52a0\u6a21\u578b\u6570\u636e\uff0c\u5219\u76f4\u63a5\u5220\u9664\uff0c\u4e0d\u4f1a\u4fee\u6539\u4e3b\u6a21\u578b\u6570\u636e<br> &nbsp; &nbsp;# StudentInfo.query.filter(StudentInfo.mobile == \"13312345672\").delete()<br> &nbsp; &nbsp;# db.session.commit()<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<br>\u200b<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e00\u5bf9\u591a<\/h4>\n\n\n\n<p>\u5e38\u89c1\u4e1a\u52a1\uff1a\u5546\u54c1\u5206\u7c7b\u548c\u5546\u54c1\u3001\u6587\u7ae0\u5206\u7c7b\u548c\u6587\u7ae0\u3001\u73ed\u7ea7\u4e0e\u5b66\u751f\u3001\u90e8\u95e8\u4e0e\u5458\u5de5\u3001\u89d2\u8272\u4e0e\u4f1a\u5458\u3001\u8ba2\u5355\u4e0e\u8ba2\u5355\u8be6\u60c5\u3001\u7528\u6237\u4e0e\u6536\u8d27\u5730\u5740\u3002\u3002\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class User(db.Model):<br>    ...<br> &nbsp; &nbsp;# \u5173\u8054\u5c5e\u6027\uff0c\u4e00\u7684\u4e00\u65b9\u6dfb\u52a0\u6a21\u578b\u5173\u8054\u5c5e\u6027<br> &nbsp; &nbsp;address_list = db.relationship(\"UserAddress\", uselist=True, backref=\"user\", lazy='dynamic')<br> &nbsp; <br>class UsertAddress(db.Model):<br>    ...<br> &nbsp; &nbsp;# \u5916\u952e\uff0c\u591a\u7684\u4e00\u65b9\u6a21\u578b\u4e2d\u6dfb\u52a0\u5916\u95f4<br> &nbsp; &nbsp;user_id = db.Column(db.ForeignKey(User.id))<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5176\u4e2drealtionship\u63cf\u8ff0\u4e86Student\u548cStudentAddress\u7684\u5173\u7cfb\u3002\u7b2c1\u4e2a\u53c2\u6570\u4e3a\u5bf9\u5e94\u53c2\u7167\u7684\u7c7b&#8221;StudentAddress&#8221;<\/li>\n\n\n\n<li>\u7b2c3\u4e2a\u53c2\u6570backref\u4e3a\u7c7bStudentAddress\u58f0\u660e\u5173\u8054\u5c5e\u6027<\/li>\n\n\n\n<li>\u7b2c4\u4e2a\u53c2\u6570lazy\u51b3\u5b9a\u4e86\u4ec0\u4e48\u65f6\u5019SQLALchemy\u4ec0\u4e48\u65f6\u5019\u6267\u884c\u8bfb\u53d6\u5173\u8054\u6a21\u578b\u7684SQL\u8bed\u53e5\n<ul class=\"wp-block-list\">\n<li>lazy=&#8217;subquery&#8217;\uff0c\u67e5\u8be2\u5f53\u524d\u6570\u636e\u6a21\u578b\u65f6\uff0c\u91c7\u7528\u5b50\u67e5\u8be2(subquery)\uff0c\u628a\u5916\u952e\u6a21\u578b\u7684\u5c5e\u6027\u4e5f\u540c\u65f6\u67e5\u8be2\u51fa\u6765\u4e86\u3002<\/li>\n\n\n\n<li>lazy=True\u6216lazy=&#8217;select&#8217;\uff0c\u67e5\u8be2\u5f53\u524d\u6570\u636e\u6a21\u578b\u65f6\uff0c\u4e0d\u4f1a\u628a\u5916\u952e\u6a21\u578b\u7684\u6570\u636e\u67e5\u8be2\u51fa\u6765\uff0c\u53ea\u6709\u64cd\u4f5c\u5230\u5916\u952e\u5173\u8054\u5c5e\u6027\u65f6\uff0c\u624d\u8fdb\u884c\u8fde\u8868\u67e5\u8be2\u6570\u636e[\u6267\u884cSQL]<\/li>\n\n\n\n<li>lazy=&#8217;dynamic&#8217;\uff0c\u67e5\u8be2\u5f53\u524d\u6570\u636e\u6a21\u578b\u65f6\uff0c\u4e0d\u4f1a\u628a\u5916\u952e\u6a21\u578b\u7684\u6570\u636e\u7acb\u523b\u67e5\u8be2\u51fa\u6765\uff0c\u53ea\u6709\u64cd\u4f5c\u5230\u5916\u952e\u5173\u8054\u5c5e\u6027\u5e76\u64cd\u4f5c\u5916\u952e\u6a21\u578b\u5177\u4f53\u5b57\u6bb5\u65f6\uff0c\u624d\u8fdb\u884c\u8fde\u8868\u67e5\u8be2\u6570\u636e[\u6267\u884cSQL]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5e38\u7528\u7684lazy\u9009\u9879\uff1adynamic\u548cselect<\/li>\n<\/ul>\n\n\n\n<p>\u8bfe\u5802\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>manage.py\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from sqlalchemy.orm import backref<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br>\u200b<br>class User(db.Model):<br> &nbsp; &nbsp;__tablename__ = \"tb_user\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;username = db.Column(db.String(50), unique=True, comment=\"\u7528\u6237\u540d\")<br> &nbsp; &nbsp;nickname = db.Column(db.String(50), index=True, comment=\"\u6635\u79f0\")<br> &nbsp; &nbsp;sex = db.Column(db.Boolean, default=True, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(8,2), default=0.0, comment=\"\u94b1\u5305\u4f59\u989d\")<br> &nbsp; &nbsp;address_list = db.relationship(\"UserAddress\", uselist=True, backref=\"user\", lazy=\"dynamic\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"username\": self.username,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"nickname\": self.nickname,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"sex\": self.sex,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"money\": float(self.money),<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>class UserAddress(db.Model):<br> &nbsp; &nbsp;__tablename__ = \"tb_user_address\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(50), default=\"\u9ed8\u8ba4\", comment=\"\u540d\u79f0\")<br> &nbsp; &nbsp;province = db.Column(db.String(50), comment=\"\u7701\u4efd\")<br> &nbsp; &nbsp;city = db.Column(db.String(50), comment=\"\u57ce\u5e02\")<br> &nbsp; &nbsp;area = db.Column(db.String(50), comment=\"\u5730\u533a\")<br> &nbsp; &nbsp;address = db.Column(db.String(500), comment=\"\u8be6\u7ec6\u5730\u5740\")<br> &nbsp; &nbsp;mobile = db.Column(db.String(15), comment=\"\u6536\u8d27\u4eba\u7535\u8bdd\")<br> &nbsp; &nbsp;user_id = db.Column(db.Integer, db.ForeignKey(\"tb_user.id\"), comment=\"\u5916\u952e\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name\": self.name,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"province\": self.province,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"city\": self.city,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"area\": self.area,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"address\": self.address,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"mobile\": self.mobile,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"user_id\": self.user_id,<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a1\")<br>def a1():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u6570\u636e\"\"\"<br> &nbsp; &nbsp;# # \u6dfb\u52a0\u4e3b\u6a21\u578b\u7684\u540c\u65f6\u4e5f\u4f1a\u7ed9\u9644\u52a0\u6a21\u578b\u6dfb\u52a0\u6570\u636e\uff0c\u8fd9\u79cd\u60c5\u51b5\uff0c\u9644\u52a0\u6a21\u578b\u7684\u503c\u53ef\u4ee5\u4ee5\u5217\u8868\u5f62\u5f0f\u8fdb\u884c\u6dfb\u52a0\uff0c\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u9644\u52a0\u6a21\u578b\u6570\u636e\u8fdb\u53bb\u3002<br> &nbsp; &nbsp;# user = User(<br> &nbsp; &nbsp;# &nbsp; &nbsp; username=\"xiaolan001\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; nickname=\"xiaolan001\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; sex=False,<br> &nbsp; &nbsp;# &nbsp; &nbsp; money=10000,<br> &nbsp; &nbsp;# &nbsp; &nbsp; address_list=[<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; UserAddress(name=\"\u516c\u53f8\", province=\"\u5317\u4eac\u5e02\", city=\"\u5317\u4eac\u5e02\", area=\"\u660c\u5e73\u533a\", address=\"\u767e\u6c99\u8def201\", mobile=\"13012345678\"),<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; UserAddress(name=\"\u95e8\u53e3\u5c0f\u5356\u90e8\", province=\"\u5317\u4eac\u5e02\", city=\"\u5317\u4eac\u5e02\", area=\"\u660c\u5e73\u533a\", address=\"\u767e\u6c99\u8def202\", mobile=\"13012345677\"),<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; UserAddress(name=\"\u5c0f\u533a\u95e8\u53e3\", province=\"\u5317\u4eac\u5e02\", city=\"\u5317\u4eac\u5e02\", area=\"\u660c\u5e73\u533a\", address=\"\u767e\u6c99\u8def203\", mobile=\"13012345676\"),<br> &nbsp; &nbsp;# &nbsp; &nbsp; ]<br> &nbsp; &nbsp;# )<br> &nbsp; &nbsp;# db.session.add(user)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# \u6dfb\u52a0\u5916\u952e\u6a21\u578b\u6570\u636e\u7684\u540c\u65f6\uff0c\u6dfb\u52a0\u4e3b\u6a21\u578b\u6570\u636e<br> &nbsp; &nbsp;address = UserAddress(<br> &nbsp; &nbsp; &nbsp; &nbsp;province=\"\u5929\u6d25\u5e02\",<br> &nbsp; &nbsp; &nbsp; &nbsp;city=\"\u5929\u6d25\u5e02\",<br> &nbsp; &nbsp; &nbsp; &nbsp;area=\"\u9759\u6d77\u533a\",<br> &nbsp; &nbsp; &nbsp; &nbsp;address=\"\u9759\u5b89\u8def1103\u53f7\",<br> &nbsp; &nbsp; &nbsp; &nbsp;user=User(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;username=\"xiaolan02\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nickname=\"xiaolan02\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;money=10000,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sex=False,<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  )<br> &nbsp; &nbsp;db.session.add(address)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/q1\")<br>def q1():<br> &nbsp; &nbsp;# \u6b63\u5411\u5173\u8054----&gt; \u4ece\u4e3b\u6a21\u578b\u67e5\u8be2\u5916\u952e\u6a21\u578b<br> &nbsp; &nbsp;student = User.query.filter(User.username == \"xiaolan001\").first()<br>\u200b<br>\u200b<br> &nbsp; &nbsp;# \u89c2\u5bdf\u8fde\u8868SQL\u8bed\u53e5\u7684\u6267\u884c<br> &nbsp; &nbsp;\"\"\" lazy=\"select\" || lazy=True \"\"\"<br> &nbsp; &nbsp;# print(student) # \u6ca1\u6709\u67e5\u8be2\u9644\u52a0\u8868<br> &nbsp; &nbsp;# print(student.address_list) # \u67e5\u8be2\u4e86\u9644\u52a0\u8868<br>\u200b<br> &nbsp; &nbsp;\"\"\" lazy=\"subquery\" \"\"\"<br> &nbsp; &nbsp;# print(student)  # \u5df2\u7ecf\u67e5\u8be2\u4e86\u9644\u52a0\u8868\uff0c\u5982\u679c\u6ca1\u6709\u4f7f\u7528\u5230\u5916\u952e\u6a21\u578b\u7684\u6570\u636e\uff0c\u5219\u672c\u6b21\u67e5\u8be2\u5b58\u5728\u8d44\u6e90\u6d6a\u8d39\uff01\uff01<br>\u200b<br> &nbsp; &nbsp;\"\"\" lazy=\"dynamic\" \"\"\"<br> &nbsp; &nbsp;# print(student)  # \u6ca1\u6709\u67e5\u8be2\u9644\u52a0\u8868<br> &nbsp; &nbsp;# student.address_list  # \u6ca1\u6709\u67e5\u8be2\u9644\u52a0\u6a21\u578b\u7684SQL\u8bed\u53e5<br> &nbsp; &nbsp;# print( student.address_list ) # \u53ea\u8981\u8bbf\u95ee\u5173\u8054\u6a21\u578b\u7684\u5177\u4f53\u5b57\u6bb5\u624d\u771f\u6b63\u6267\u884c\uff0c\u5982\u679c\u6d77\u91cf\u6570\u91cf\u67e5\u8be2\uff0c\u5219\u672c\u6b21\u67e5\u8be2\u4f1a\u5f71\u54cd\u8fd4\u56de\u6570\u636e\u7ed9\u5ba2\u6237\u7aef\u7684\u65f6\u95f4<br>\u200b<br>\u200b<br> &nbsp; &nbsp;print( student.address_list[0] ) # \u83b7\u53d6\u8fd4\u56de\u5217\u8868\u7684\u7b2c1\u4e2a\u6210\u5458<br>\u200b<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u591a\u5bf9\u591a<\/h4>\n\n\n\n<p>\u5e38\u89c1\u4e1a\u52a1\uff1a\u7528\u6237\u6536\u85cf\u6587\u7ae0\u3001\u7528\u6237\u4e0e\u7528\u6237\u4e4b\u95f4\u7684\u597d\u53cb\u5173\u7cfb\u3001\u70b9\u8d5e\u3001\u8bc4\u8bba\u3001\u5173\u6ce8\u3001\u7528\u6237\u6d4f\u89c8\u5546\u54c1\u7684\u5386\u53f2\u8bb0\u5f55\u3001\u8ba2\u9605\u6587\u7ae0\u3001\u4e13\u9898\u4e0e\u5546\u54c1\/\u6587\u7ae0\u7684\u5173\u7cfb\u3001\u6d3b\u52a8\u4e0e\u5546\u54c1\u3002\u3002\u3002\u3002\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5173\u7cfb\u8868[\u8fd9\u79cd\u8868\uff0c\u65e0\u6cd5\u63d0\u4f9b\u7ed9python\u8fdb\u884c\u64cd\u4f5c\u7684\uff0c\u4ec5\u4ec5\u7528\u4e8e\u5728\u6570\u636e\u5e93\u4e2d\u8bb0\u5f55\u4e24\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb]<br># \u5173\u7cfb\u8868[\u8fd9\u79cd\u8868\uff0c\u65e0\u6cd5\u63d0\u4f9b\u7ed9python\u8fdb\u884c\u64cd\u4f5c\u7684\uff0c\u4ec5\u4ec5\u7528\u4e8e\u5728\u6570\u636e\u5e93\u4e2d\u8bb0\u5f55\u4e24\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb]<br>student_and_course = db.Table(<br> &nbsp; &nbsp;\"table_student_course\",<br> &nbsp; &nbsp;db.Column(\"id\", db.Integer, primary_key=True, comment=\"\u4e3b\u952eID\"),<br> &nbsp; &nbsp;db.Column(\"sid\", db.Integer, db.ForeignKey(\"table_student.id\"), comment=\"\u5b66\u751f\"),<br> &nbsp; &nbsp;db.Column(\"cid\", db.Integer, db.ForeignKey(\"table_course.id\"), comment=\"\u8bfe\u7a0b\"),<br> &nbsp; &nbsp;db.Column(\"created_time\", db.DateTime, default=datetime.now, comment=\"\u8d2d\u4e70\u65f6\u95f4\"), # \u5f53\u524d\u5b57\u6bb5\u65e0\u6cd5\u64cd\u4f5c<br>)<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True,comment=\"\u4e3b\u952e\")<br> &nbsp;  ...<br> &nbsp; &nbsp;course_list = db.relationship(\"Course\", secondary=student_and_course, backref=\"student_list\", lazy=\"dynamic\")<br>\u200b<br>class Course(db.Model):<br> &nbsp;  ...<br> &nbsp; &nbsp;<br># \u5173\u7cfb\u6a21\u578b\uff0c[\u5173\u7cfb\u6a21\u578b\u548c\u5173\u7cfb\u8868\uff0c\u4efb\u9009\u5176\u4e00]<br>class Achievement(db.Model):<br> &nbsp;  ...<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u57fa\u4e8e\u7b2c\u4e09\u65b9\u5173\u7cfb\u8868\u6784\u5efa\u591a\u5bf9\u591a<\/h5>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from sqlalchemy.orm import backref<br>from datetime import datetime<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br># \u5173\u7cfb\u8868[\u8fd9\u79cd\u8868\uff0c\u65e0\u6cd5\u63d0\u4f9b\u7ed9python\u8fdb\u884c\u64cd\u4f5c\u7684\uff0c\u4ec5\u4ec5\u7528\u4e8e\u5728\u6570\u636e\u5e93\u4e2d\u8bb0\u5f55\u4e24\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb]<br>student_and_course = db.Table(<br> &nbsp; &nbsp;\"table_student_course\",<br> &nbsp; &nbsp;db.Column(\"id\", db.Integer, primary_key=True, comment=\"\u4e3b\u952eID\"),<br> &nbsp; &nbsp;db.Column(\"sid\", db.Integer, db.ForeignKey(\"table_student.id\"), comment=\"\u5b66\u751f\"),<br> &nbsp; &nbsp;db.Column(\"cid\", db.Integer, db.ForeignKey(\"table_course.id\"), comment=\"\u8bfe\u7a0b\"),<br> &nbsp; &nbsp;db.Column(\"created_time\", db.DateTime, default=datetime.now, comment=\"\u8d2d\u4e70\u65f6\u95f4\"), # \u5f53\u524d\u5b57\u6bb5\u65e0\u6cd5\u64cd\u4f5c<br>)<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u4fe1\u606f\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;# \u58f0\u660e\u4e0e\u5f53\u524d\u6a21\u578b\u7ed1\u5b9a\u7684\u6570\u636e\u8868\u540d\u79f0<br> &nbsp; &nbsp;__tablename__ = \"table_student\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True,comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.Boolean, default=True, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(128), comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10, 2), default=0.0, comment=\"\u94b1\u5305\")<br> &nbsp; &nbsp;course_list = db.relationship(\"Course\", secondary=student_and_course, backref=\"student_list\", lazy=\"dynamic\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name\": self.name,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"age\": self.age,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"sex\": self.sex,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"email\": self.email,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"money\": float(self.money),<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"table_course\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2), default=0.0, comment=\"\u4ef7\u683c\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name\": self.name,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"price\": self.price,<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a1\")<br>def a1():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u6570\u636e\"\"\"<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u5176\u4e2d\u4e00\u4e2a\u4e3b\u6a21\u578b\u6570\u636e\u65f6\uff0c\u540c\u65f6\u7ed1\u5b9a\u6dfb\u52a0\u53e6\u5916\u4e00\u4e2a\u4e3b\u6a21\u578b\u7684\u6570\u636e\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5173\u7cfb\u8868\u4f1a\u81ea\u52a8\u5199\u51652\u8005\u7684\u5173\u7cfb\u6570\u636e\uff0c\u7ed1\u5b9a2\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\"\"\"<br> &nbsp; &nbsp;# student = Student(<br> &nbsp; &nbsp;# &nbsp; &nbsp; name=\"xiaozhao\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; age=13,<br> &nbsp; &nbsp;# &nbsp; &nbsp; sex=False,<br> &nbsp; &nbsp;# &nbsp; &nbsp; money=30000,<br> &nbsp; &nbsp;# &nbsp; &nbsp; email=\"xiaozhao@qq.com\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; course_list=[<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; Course(name=\"python\u5165\u95e8\", price=99.99),<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; Course(name=\"python\u521d\u7ea7\", price=199.99),<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; Course(name=\"python\u8fdb\u9636\", price=299.99),<br> &nbsp; &nbsp;# &nbsp; &nbsp; ]<br> &nbsp; &nbsp;# )<br> &nbsp; &nbsp;# db.session.add(student)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;\"\"\"\u5728\u5b66\u751f\u62a5\u8bfb\u8bfe\u7a0b\u7684\u57fa\u7840\u4e0a\uff0c\u65b0\u589e\u62a5\u8bfb\u8bfe\u7a0b\u3002\"\"\"<br> &nbsp; &nbsp;# student = Student(<br> &nbsp; &nbsp;# &nbsp; &nbsp; name=\"xiaohong\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; age=14,<br> &nbsp; &nbsp;# &nbsp; &nbsp; sex=False,<br> &nbsp; &nbsp;# &nbsp; &nbsp; money=30000,<br> &nbsp; &nbsp;# &nbsp; &nbsp; email=\"300000@qq.com\",<br> &nbsp; &nbsp;# )<br> &nbsp; &nbsp;# db.session.add(student)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# student = Student.query.filter(Student.name==\"xiaohong\").first()<br> &nbsp; &nbsp;# # \u76f4\u63a5\u91c7\u7528python\u5185\u7f6e\u7684list\u65b9\u6cd5\u64cd\u4f5c<br> &nbsp; &nbsp;# student.course_list.append(Course.query.get(3)) # \u65b0\u589e\u5df2\u7ecf\u5b58\u5728\u7684\u8bfe\u7a0b<br> &nbsp; &nbsp;# student.course_list.append(Course(name=\"python\u9876\u7ea7\", price=399.99))  # \u5df2\u6709\u8bfe\u7a0b\uff0c\u5e76\u8ba9\u5f53\u524d\u5b66\u751f\u62a5\u8bfb\u8be5\u8bfe\u7a0b<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u5b66\u751f\u62a5\u8bfb\u8bfe\u7a0b\u7684\u6d4b\u8bd5\u6570\u636e\"\"\"<br> &nbsp; &nbsp;# student1 = Student.query.get(1)<br> &nbsp; &nbsp;# course_list = Course.query.filter(Course.id.in_([1,2])).all()<br> &nbsp; &nbsp;# student1.course_list.extend(course_list)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;student2 = Student.query.get(2)<br> &nbsp; &nbsp;course_list = Course.query.filter(Course.id.in_([3,2])).all()<br> &nbsp; &nbsp;student2.course_list.extend(course_list)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/q1\")<br>def q1():<br> &nbsp; &nbsp;\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# \u67e5\u8be2ID=4\u7684\u5b66\u751f\u62a5\u8bfb\u7684\u8bfe\u7a0b\u5217\u8868<br> &nbsp; &nbsp;# student = Student.query.get(2)<br> &nbsp; &nbsp;# course_list = [{\"name\":item.name,\"price\":float(item.price)} for item in student.course_list]<br> &nbsp; &nbsp;# print(course_list)<br>\u200b<br> &nbsp; &nbsp;# \u67e5\u8be2\u51fa2\u53f7\u8bfe\u7a0b\uff0c\u90fd\u6709\u90a3\u4e9b\u5b66\u751f\u5728\u8bfb\uff1f<br> &nbsp; &nbsp;course = Course.query.get(2)<br> &nbsp; &nbsp;student_list = [{\"name\":item.name,\"age\":item.age} for item in course.student_list]<br> &nbsp; &nbsp;print(student_list)<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>@app.route(\"\/u1\")<br>def u1():<br> &nbsp; &nbsp;\"\"\"\u66f4\u65b0\u6570\u636e\"\"\"<br> &nbsp; &nbsp;# # \u7ed9\u62a5\u8bfb\u4e863\u53f7\u8bfe\u7a0b\u7684\u540c\u5b66\uff0c\u8fd4\u73b0\u7ea2\u5305200\u5757\u94b1<br> &nbsp; &nbsp;# course = Course.query.get(3)<br> &nbsp; &nbsp;# for student in course.student_list:<br> &nbsp; &nbsp;# &nbsp; &nbsp; student.money+=200<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# db.Table\u7684\u7f3a\u9677: \u65e0\u6cd5\u901a\u8fc7\u4e3b\u6a21\u578b\u76f4\u63a5\u64cd\u4f5cdb.Table\u4e2d\u7684\u5916\u952e\u4e4b\u5916\u7684\u5176\u4ed6\u5b57\u6bb5\uff0c\u4f8b\u5982\uff1a\u62a5\u8bfb\u8bfe\u7a0b\u7684\u65f6\u95f4<br> &nbsp; &nbsp;course = Course.query.get(3)<br> &nbsp; &nbsp;print(course.student_list)<br>\u200b<br> &nbsp; &nbsp;# \u89e3\u51b3\uff1a\u5728\u58f0\u660e2\u4e2a\u6a21\u578b\u662f\u591a\u5bf9\u591a\u7684\u5173\u8054\u5173\u7cfb\u65f6\uff0c\u628a\u5173\u8054\u5173\u7cfb\u4f7f\u7528\u7b2c\u4e09\u4e2a\u6a21\u578b\u6765\u521b\u5efa\u58f0\u660e\uff0c<br> &nbsp; &nbsp;# \u5c31\u662f\u4e0d\u8981\u4f7f\u7528db.Table\u6539\u6210\u6a21\u578b\u6765\u7ed1\u5b9a\u5173\u7cfb\uff0c\u628a\u6a21\u578b\u7684\u5bf9\u591a\u5bf9\u62c6\u5206\u62102\u4e2a1\u5bf9\u591a<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<br>\u200b<\/pre>\n\n\n\n<p>\u591a\u5bf9\u591a\uff0c\u4e5f\u53ef\u4ee5\u62c6\u89e3\u62103\u4e2a\u6a21\u578b\uff082\u4e2a\u4e3b\u6a21\u578b\uff0c1\u4e2a\u5173\u7cfb\u6a21\u578b\uff0c\u5173\u7cfb\u6a21\u578b\u4fdd\u5b58\u4e862\u4e2a\u4e3b\u6a21\u578b\u7684\u5916\u952e\uff09\uff0c\u5176\u4e2dtb_achievement\u4f5c\u4e3a\u5355\u72ec\u6a21\u578b\u5b58\u5728\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u57fa\u4e8e\u7b2c\u4e09\u65b9\u5173\u7cfb\u6a21\u578b\u6784\u5efa\u591a\u5bf9\u591a<\/h5>\n\n\n\n<p>\u5728SQLAlchemy\u4e2d\uff0c\u57fa\u4e8edb.Table\u521b\u5efa\u7684\u5173\u7cfb\u8868\uff0c\u5982\u679c\u9700\u8981\u65b0\u589e\u9664\u4e86\u5916\u952e\u4ee5\u5916\u5176\u4ed6\u5b57\u6bb5\uff0c\u65e0\u6cd5\u64cd\u4f5c\u3002\u6240\u4ee5\u5c06\u6765\u5b9e\u73b0\u591a\u5bf9\u591a\u7684\u65f6\u5019\uff0c\u9664\u4e86\u4e0a\u9762db.Table\u65b9\u6848\u4ee5\u5916\uff0c\u8fd8\u53ef\u4ee5\u628a\u5173\u7cfb\u8868\u58f0\u660e\u6210\u6a21\u578b\u7684\u65b9\u6cd5\uff0c\u5982\u679c\u58f0\u660e\u6210\u6a21\u578b\uff0c\u5219\u539f\u6765\u8bfe\u7a0b\u548c\u5b66\u751f\u4e4b\u95f4\u7684\u591a\u5bf9\u591a\u7684\u5173\u7cfb\uff0c\u5c31\u4f1a\u53d8\u6210\u8fdc\u7a0b\u76841\u5bf9\u591a\u4e86\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from sqlalchemy.orm import backref<br>from datetime import datetime<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br># \u5173\u7cfb\u8868[\u8fd9\u79cd\u8868\uff0c\u65e0\u6cd5\u63d0\u4f9b\u7ed9python\u8fdb\u884c\u64cd\u4f5c\u7684\uff0c\u4ec5\u4ec5\u7528\u4e8e\u5728\u6570\u636e\u5e93\u4e2d\u8bb0\u5f55\u4e24\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb]<br># student_and_course = db.Table(<br># &nbsp; &nbsp; \"demo_student_course\",<br># &nbsp; &nbsp; db.Column(\"id\", db.Integer, primary_key=True, comment=\"\u4e3b\u952eID\"),<br># &nbsp; &nbsp; db.Column(\"sid\", db.Integer, db.ForeignKey(\"demo_student.id\"), comment=\"\u5b66\u751f\"),<br># &nbsp; &nbsp; db.Column(\"cid\", db.Integer, db.ForeignKey(\"demo_course.id\"), comment=\"\u8bfe\u7a0b\"),<br># &nbsp; &nbsp; db.Column(\"created_time\", db.DateTime, default=datetime.now, comment=\"\u8d2d\u4e70\u65f6\u95f4\"), # \u5f53\u524d\u5b57\u6bb5\u65e0\u6cd5\u64cd\u4f5c<br># )<br>\u200b<br>\u200b<br>class StudentCourse(db.Model):<br> &nbsp; &nbsp;__tablename__ = \"demo_student_course\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;sid = db.Column(db.Integer, db.ForeignKey(\"demo_student.id\"))<br> &nbsp; &nbsp;cid = db.Column(db.Integer, db.ForeignKey(\"demo_course.id\"))<br> &nbsp; &nbsp;created_time = db.Column(db.DateTime, default=datetime.now, comment=\"\u8d2d\u4e70\u65f6\u95f4\")<br>\u200b<br> &nbsp; &nbsp;# \u5173\u8054\u5c5e\u6027<br> &nbsp; &nbsp;student = db.relationship(\"Student\", uselist=False, backref=backref(\"to_relation\", uselist=True))<br> &nbsp; &nbsp;course = db.relationship(\"Course\", uselist=False, backref=backref(\"to_relation\", uselist=True))<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u4fe1\u606f\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;# \u58f0\u660e\u4e0e\u5f53\u524d\u6a21\u578b\u7ed1\u5b9a\u7684\u6570\u636e\u8868\u540d\u79f0<br> &nbsp; &nbsp;__tablename__ = \"demo_student\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True,comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.Boolean, default=True, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(128), comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10, 2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name\": self.name,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"age\": self.age,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"sex\": self.sex,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"email\": self.email,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"money\": float(self.money),<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"demo_course\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2), default=0.0, comment=\"\u4ef7\u683c\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return json.dumps(self.__to_dict__, ensure_ascii=False)<br>\u200b<br> &nbsp; &nbsp;@property<br> &nbsp; &nbsp;def __to_dict__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"id\": self.id,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name\": self.name,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"price\": self.price,<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/a1\")<br>def a1():<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u6570\u636e\"\"\"<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u5176\u4e2d\u4e00\u4e2a\u4e3b\u6a21\u578b\u6570\u636e\u65f6\uff0c\u540c\u65f6\u7ed1\u5b9a\u6dfb\u52a0\u53e6\u5916\u4e00\u4e2a\u4e3b\u6a21\u578b\u7684\u6570\u636e\"\"\"<br> &nbsp; &nbsp;# student = Student(<br> &nbsp; &nbsp;# &nbsp; &nbsp; name=\"xiaozhao\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; age=13,<br> &nbsp; &nbsp;# &nbsp; &nbsp; sex=False,<br> &nbsp; &nbsp;# &nbsp; &nbsp; money=30000,<br> &nbsp; &nbsp;# &nbsp; &nbsp; email=\"xiaozhao@qq.com\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; to_relation=[<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; StudentCourse(<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; course=Course(name=\"python\u57fa\u7840\", price=99.9)<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; )<br> &nbsp; &nbsp;# &nbsp; &nbsp; ]<br> &nbsp; &nbsp;# )<br> &nbsp; &nbsp;# db.session.add(student)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;\"\"\"\u5728\u5b66\u751f\u62a5\u8bfb\u8bfe\u7a0b\u7684\u57fa\u7840\u4e0a\uff0c\u65b0\u589e\u62a5\u8bfb\u8bfe\u7a0b\u3002\"\"\"<br> &nbsp; &nbsp;# student = Student(<br> &nbsp; &nbsp;# &nbsp; &nbsp; name=\"xiaohong\",<br> &nbsp; &nbsp;# &nbsp; &nbsp; age=14,<br> &nbsp; &nbsp;# &nbsp; &nbsp; sex=False,<br> &nbsp; &nbsp;# &nbsp; &nbsp; money=30000,<br> &nbsp; &nbsp;# &nbsp; &nbsp; email=\"300000@qq.com\",<br> &nbsp; &nbsp;# )<br> &nbsp; &nbsp;# db.session.add(student)<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# student = Student.query.filter(Student.name == \"xiaohong\").first()<br> &nbsp; &nbsp;# # \u76f4\u63a5\u91c7\u7528python\u5185\u7f6e\u7684list\u65b9\u6cd5\u64cd\u4f5c<br> &nbsp; &nbsp;# student.to_relation.extend([<br> &nbsp; &nbsp;# &nbsp; &nbsp; StudentCourse(<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; course=Course.query.get(1) # \u5df2\u7ecf\u5b58\u5728\u7684\u8bfe\u7a0b\uff0c\u7ed9\u5b66\u751f\u62a5\u8bfb<br> &nbsp; &nbsp;# &nbsp; &nbsp; ),<br> &nbsp; &nbsp;# &nbsp; &nbsp; StudentCourse(<br> &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; course=Course(name=\"python\u8fdb\u9636\", price=399.99)  # \u65b0\u589e\u8bfe\u7a0b\uff0c\u5e76\u8ba9\u5f53\u524d\u5b66\u751f\u62a5\u8bfb\u8be5\u8bfe\u7a0b<br> &nbsp; &nbsp;# &nbsp; &nbsp; )<br> &nbsp; &nbsp;# ])<br> &nbsp; &nbsp;#<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br>\u200b<br> &nbsp; &nbsp;\"\"\"\u6dfb\u52a0\u5b66\u751f\u62a5\u8bfb\u8bfe\u7a0b\u7684\u6d4b\u8bd5\u6570\u636e\"\"\"<br> &nbsp; &nbsp;student1 = Student.query.get(1)<br> &nbsp; &nbsp;course_list = Course.query.filter(Course.id.in_([1,2])).all()<br> &nbsp; &nbsp;student1.to_relation.extend([StudentCourse(course=course) for course in course_list])<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/q1\")<br>def q1():<br> &nbsp; &nbsp;\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"<br> &nbsp; &nbsp;# \u67e5\u8be2ID=2\u7684\u5b66\u751f\u62a5\u8bfb\u7684\u8bfe\u7a0b\u5217\u8868<br> &nbsp; &nbsp;# student = Student.query.get(2)<br> &nbsp; &nbsp;# course_list = [{\"name\":item.course.name,\"price\":float(item.course.price)} for item in student.to_relation]<br> &nbsp; &nbsp;# print(course_list)<br>\u200b<br> &nbsp; &nbsp;# \u67e5\u8be2\u51fa2\u53f7\u8bfe\u7a0b\uff0c\u90fd\u6709\u90a3\u4e9b\u5b66\u751f\u5728\u8bfb\uff1f<br> &nbsp; &nbsp;course = Course.query.get(2)<br> &nbsp; &nbsp;student_list = [{\"name\":item.student.name,\"age\":item.student.age} for item in course.to_relation]<br> &nbsp; &nbsp;print(student_list)<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>@app.route(\"\/u1\")<br>def u1():<br> &nbsp; &nbsp;\"\"\"\u66f4\u65b0\u6570\u636e\"\"\"<br> &nbsp; &nbsp;# # \u7ed9\u62a5\u8bfb\u4e862\u53f7\u8bfe\u7a0b\u7684\u540c\u5b66\uff0c\u8fd4\u73b0\u7ea2\u5305200\u5757\u94b1<br> &nbsp; &nbsp;# course = Course.query.get(2)<br> &nbsp; &nbsp;# for relation in course.to_relation:<br> &nbsp; &nbsp;# &nbsp; &nbsp; relation.student.money += 200<br> &nbsp; &nbsp;# db.session.commit()<br>\u200b<br> &nbsp; &nbsp;# \u83b7\u53d6\u4e2d\u95f4\u7684\u5173\u7cfb\u6a21\u578b\u7684\u5b57\u6bb5<br> &nbsp; &nbsp;course = Course.query.get(2)<br> &nbsp; &nbsp;for relation in course.to_relation:<br> &nbsp; &nbsp; &nbsp; &nbsp;print(relation.created_time)<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<br>\u200b<\/pre>\n\n\n\n<p>relationship\u8fd8\u6709\u4e00\u4e2a\u8bbe\u7f6e\u5916\u952e\u7ea7\u8054\u7684\u5c5e\u6027\uff1acascade=&#8221;all, delete, delete-orphan&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4f5c\u4e1a\uff1a<br>1. \u6211\u4eec\u73b0\u5728\u5b66\u4e60\u7684flask\u6846\u67b6\u96c6\u6210SQLAlchemy\u64cd\u4f5c\u6570\u636e\u5e93\u4f7f\u7528\u7684\u662fflask-SQLAlchemy\u6a21\u5757\u3002\u5982\u679c\u539f\u751f\u7684python\u4e0b\u9762\u5e94\u8be5\u5982\u4f55\u4f7f\u7528SQLAlchemy\u8fdb\u884c\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\u548c\u58f0\u660e\u6a21\u578b\u5e76\u5b9e\u73b0\u6a21\u578b\u7684\u57fa\u672c\u64cd\u4f5c[\u589e\u5220\u67e5\u6539\uff0c\u5173\u8054\u67e5\u8be2]\uff1f<br>\u200b<br>2. flask\u4e2d\u7684SQLAlchemy\u5982\u4f55\u8fdb\u884c\u81ea\u5173\u8054\u67e5\u8be2\uff1f \u8fd9\u91cc\u81ea\u5df1\u5199\u4e00\u4e2a\u5173\u4e8e\u884c\u653f\u533a\u5212\u7684\u81ea\u5173\u8054\u64cd\u4f5c\u3002<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u903b\u8f91\u5916\u952e<\/h2>\n\n\n\n<p>\u4e5f\u53eb\u865a\u62df\u5916\u952e\u3002\u4e3b\u8981\u5c31\u662f\u5728\u5f00\u53d1\u4e2d\u4e3a\u4e86\u51cf\u5c11\u6570\u636e\u5e93\u7684\u6027\u80fd\u6d88\u8017\uff0c\u63d0\u5347\u7cfb\u7edf\u8fd0\u884c\u6548\u7387\uff0c\u4e00\u822c\u9879\u76ee\u4e2d\u5982\u679c\u5355\u8868\u6570\u636e\u592a\u5927[\u5343\u4e07\u7ea7\u522b]\u5c31\u4e0d\u4f1a\u4f7f\u7528\u6570\u636e\u5e93\u672c\u8eab\u7ef4\u62a4\u7684\u7269\u7406\u5916\u952e\uff0c\u800c\u662f\u91c7\u7528\u7531ORM\u6216\u8005\u6211\u4eec\u903b\u8f91\u4ee3\u7801\u8fdb\u884c\u67e5\u8be2\u5173\u8054\u7684\u903b\u8f91\u5916\u952e\u3002<\/p>\n\n\n\n<p>SQLAlchemy\u8bbe\u7f6e\u5916\u952e\u6a21\u578b\u7684\u865a\u62df\u5916\u952e\uff0c\u67092\u79cd\u65b9\u6848\uff1a<\/p>\n\n\n\n<p>\u65b9\u68481\uff0c\u67e5\u8be2\u6570\u636e\u65f6<strong>\u4e34\u65f6\u6307\u5b9a\u903b\u8f91\u5916\u952e<\/strong>\u7684\u6620\u5c04\u5173\u7cfb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6a21\u578b\u7c7b.query.join(\u6a21\u578b\u7c7b,\u4e3b\u6a21\u578b.\u4e3b\u952e==\u5916\u952e\u6a21\u578b.\u5916\u952e).join(\u6a21\u578b\u7c7b,\u4e3b\u6a21\u578b.\u4e3b\u952e==\u5916\u952e\u6a21\u578b.\u5916\u952e).with_entities(\u5b57\u6bb51,\u5b57\u6bb52.label(\"\u5b57\u6bb5\u522b\u540d\"),....).all()<\/pre>\n\n\n\n<p>\u65b9\u68482\uff0c\u5728<strong>\u6a21\u578b\u58f0\u660e\u65f6\u6307\u5b9a\u903b\u8f91\u5916\u952e\u7684\u6620\u5c04\u5173\u7cfb<\/strong>(\u6700\u5e38\u7528\uff0c\u8fd9\u79cd\u8bbe\u7f6e\u65b9\u6848\uff0c\u5728\u64cd\u4f5c\u6a21\u578b\u65f6\u4e0e\u539f\u6765\u9ed8\u8ba4\u8bbe\u7f6e\u7684\u7269\u7406\u5916\u952e\u7684\u5173\u8054\u64cd\u4f5c\u662f\u4e00\u6a21\u4e00\u6837\u7684\u5199\u6cd5)\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student(db.Model):<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;# \u865a\u62df\u5916\u952e\uff0c\u539f\u6709\u53c2\u6570\u4e0d\u53d8\uff0c\u65b0\u589e2\u4e2a\u8868\u8fbe\u5173\u8054\u5173\u7cfb\u7684\u5c5e\u6027\uff1a<br> &nbsp; &nbsp;# primaryjoin, \u6307\u5b9a2\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u4e3b\u5916\u952e\u5173\u7cfb\uff0c\u76f8\u5f53\u4e8e\u539f\u751fSQL\u8bed\u53e5\u4e2d\u7684join<br> &nbsp; &nbsp;# foreign_keys\uff0c\u6307\u5b9a\u5916\u952e<br> &nbsp; &nbsp;address_list = db.relationship(\"StudentAddress\", uselist=True, backref=\"student\", lazy=\"subquery\", primaryjoin=\"Student.id==StudentAddress.student_id\", foreign_keys=\"StudentAddress.student_id\")<br>\u200b<br>class StudentAddress(db.Model):<br> &nbsp; &nbsp;# \u539f\u6765\u7684\u5916\u952e\u8bbe\u7f6e\u4e3a\u666e\u901a\u7d22\u5f15\u5373\u53ef\u3002<br> &nbsp; &nbsp;student_id = db.Column(db.Integer, comment=\"\u5b66\u751fid\")<\/pre>\n\n\n\n<p>\u4f8b1\uff0c\u865a\u62df\u5916\u952e\u4f7f\u7528\u7684\u65b9\u68481\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from sqlalchemy.orm import backref<br>from datetime import datetime<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u4fe1\u606f\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;# \u58f0\u660e\u4e0e\u5f53\u524d\u6a21\u578b\u7ed1\u5b9a\u7684\u6570\u636e\u8868\u540d\u79f0<br> &nbsp; &nbsp;__tablename__ = \"td_student\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True,comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.Boolean, default=True, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(128), comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10,2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;Student&gt;\"<br>\u200b<br>\u200b<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"td_course\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2))<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f'{self.name}&lt;{self.__class__.__name__}&gt;'<br>\u200b<br>\u200b<br>class StudentCourse(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u548c\u8bfe\u7a0b\u4e4b\u95f4\u7684\u5173\u7cfb\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"td_student_course\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;student_id = db.Column(db.Integer, index=True)<br> &nbsp; &nbsp;course_id = db.Column(db.Integer, index=True)<br> &nbsp; &nbsp;score = db.Column(db.Numeric(4,1), default=0, comment=\"\u6210\u7ee9\")<br> &nbsp; &nbsp;time = db.Column(db.DateTime, default=datetime.now, comment=\"\u8003\u8bd5\u65f6\u95f4\")<br>\u200b<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>@app.route(\"\/a1\")<br>def a1():<br> &nbsp; &nbsp;# \u6dfb\u52a0\u6d4b\u8bd5\u6570\u636e<br> &nbsp; &nbsp;stu0 = Student(name=\"xiaoming-0\",age=15,sex=True,email=\"xiaoming0@qq.com\", money=1000)<br> &nbsp; &nbsp;stu1 = Student(name=\"xiaoming-1\",age=15,sex=True,email=\"xiaoming1@qq.com\", money=1000)<br> &nbsp; &nbsp;stu2 = Student(name=\"xiaoming-2\",age=15,sex=True,email=\"xiaoming2@qq.com\", money=1000)<br> &nbsp; &nbsp;stu3 = Student(name=\"xiaoming-3\",age=15,sex=True,email=\"xiaoming3@qq.com\", money=1000)<br> &nbsp; &nbsp;stu4 = Student(name=\"xiaoming-4\",age=15,sex=True,email=\"xiaoming4@qq.com\", money=1000)<br>\u200b<br> &nbsp; &nbsp;db.session.add_all([stu0,stu1,stu2,stu3,stu4])<br> &nbsp; &nbsp;course1 = Course(name=\"python\u57fa\u7840\u7b2c1\u5b63\", price=1000)<br> &nbsp; &nbsp;course2 = Course(name=\"python\u57fa\u7840\u7b2c2\u5b63\", price=1000)<br> &nbsp; &nbsp;course3 = Course(name=\"python\u57fa\u7840\u7b2c3\u5b63\", price=1000)<br> &nbsp; &nbsp;course4 = Course(name=\"python\u57fa\u7840\u7b2c4\u5b63\", price=1000)<br> &nbsp; &nbsp;course5 = Course(name=\"python\u57fa\u7840\u7b2c5\u5b63\", price=1000)<br> &nbsp; &nbsp;db.session.add_all([course1, course2, course3, course4, course5])<br>\u200b<br> &nbsp; &nbsp;data = [<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=1,course_id=1,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=1,course_id=2,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=1,course_id=3,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=2,course_id=1,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=2,course_id=2,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=3,course_id=3,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=3,course_id=4,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=4,course_id=5,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=4,course_id=1,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=4,course_id=2,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=5,course_id=1,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=5,course_id=2,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=5,course_id=3,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=5,course_id=4,score=60,time=datetime.now()),<br> &nbsp;  ]<br> &nbsp; &nbsp;db.session.add_all(data)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/q1\")<br>def q1():<br> &nbsp; &nbsp;# \u4f7f\u7528\u903b\u8f91\u5916\u952e\u6765\u67e5\u8be2\u6570\u636e<br> &nbsp; &nbsp;# \u4e3b\u6a21\u578b.query.join(\u4ece\u6a21\u578b\u7c7b\u540d, \u5173\u7cfb\u8bed\u53e5)<br> &nbsp; &nbsp;# \u4e3b\u6a21\u578b.query.join(\u4ece\u6a21\u578b\u7c7b\u540d, \u4e3b\u6a21\u578b.\u4e3b\u952e==\u4ece\u6a21\u578b\u7c7b\u540d.\u5916\u952e)<br>\u200b<br> &nbsp; &nbsp;# \u8bfe\u7a0b[python\u57fa\u7840\u7b2c3\u5b63]\u6709\u591a\u5c11\u5b66\u751f\u5728\u8bfb\uff1f<br> &nbsp; &nbsp;# \u52062\u6b65\u67e5\u8be2<br> &nbsp; &nbsp;course = Course.query.filter(Course.name == \"python\u57fa\u7840\u7b2c3\u5b63\").first()<br> &nbsp; &nbsp;num = StudentCourse.query.filter(StudentCourse.course_id == course.id).count()<br> &nbsp; &nbsp;print(course, num)<br>\u200b<br> &nbsp; &nbsp;# \u5173\u8054\u67e5\u8be2, 2\u8868\u5173\u8054<br> &nbsp; &nbsp;# data = Course.query.join(<br> &nbsp; &nbsp;# &nbsp; &nbsp; StudentCourse,<br> &nbsp; &nbsp;# &nbsp; &nbsp; Course.id==StudentCourse.course_id<br> &nbsp; &nbsp;# ).with_entities(<br> &nbsp; &nbsp;# &nbsp; &nbsp; Course.id, Course.name, Course.price, StudentCourse.student_id, StudentCourse.score<br> &nbsp; &nbsp;# ).filter(Course.name == \"python\u57fa\u7840\u7b2c3\u5b63\").all()<br> &nbsp; &nbsp;# print(data)<br>\u200b<br> &nbsp; &nbsp;# \u76f4\u63a5\u7edf\u8ba1\uff0c\u4e0d\u9700\u8981\u4efb\u4f55\u5b57\u6bb5<br> &nbsp; &nbsp;ret = Course.query.join(<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse,<br> &nbsp; &nbsp; &nbsp; &nbsp;Course.id==StudentCourse.course_id<br> &nbsp;  ).filter(Course.name == \"python\u57fa\u7840\u7b2c3\u5b63\").count()<br>\u200b<br> &nbsp; &nbsp;print(ret)<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/q2\")<br>def q2():<br> &nbsp; &nbsp;# \u67e5\u8be2\u8bfe\u7a0b[python\u57fa\u7840\u7b2c3\u5b63]\u6709\u54ea\u4e9b\u5b66\u751f\u5728\u8bfb\uff1f3\u8868\u5173\u8054<br> &nbsp; &nbsp;# \u6b63\u5411\u67e5\u8be2\u548c\u9006\u5411\u67e5\u8be2\u90fd\u662f\u901a\u8fc7\u58f0\u660e\u4e34\u65f6\u5916\u952e\u5173\u7cfb\u6765\u5b8c\u6210\u5173\u8054\u67e5\u8be2\u64cd\u4f5c\u3002<br> &nbsp; &nbsp;student_list = Course.query.join(<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse,<br> &nbsp; &nbsp; &nbsp; &nbsp;Course.id == StudentCourse.course_id<br> &nbsp;  ).join(<br> &nbsp; &nbsp; &nbsp; &nbsp;Student,<br> &nbsp; &nbsp; &nbsp; &nbsp;Student.id == StudentCourse.student_id<br> &nbsp;  ).with_entities(<br> &nbsp; &nbsp; &nbsp; &nbsp;Course.name.label(\"course_name\"), StudentCourse.score.label(\"score\"), Student.name.label(\"student_name\"),<br> &nbsp;  ).filter(Course.name == \"python\u57fa\u7840\u7b2c3\u5b63\").all()<br>\u200b<br> &nbsp; &nbsp;print(student_list)<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/q3\")<br>def q3():<br> &nbsp; &nbsp;# xiaoming-2 \u8d2d\u4e70\u4e86\u90a3\u4e9b\u8bfe\u7a0b\uff1f<br> &nbsp; &nbsp;course_list = Student.query.join(<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse,<br> &nbsp; &nbsp; &nbsp; &nbsp;Student.id == StudentCourse.student_id<br> &nbsp;  ).join(<br> &nbsp; &nbsp; &nbsp; &nbsp;Course,<br> &nbsp; &nbsp; &nbsp; &nbsp;Course.id == StudentCourse.course_id<br> &nbsp;  ).with_entities(<br> &nbsp; &nbsp; &nbsp; &nbsp;Course.name.label(\"course_name\"),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse.score.label(\"score\")<br> &nbsp;  ).filter(Student.name == \"xiaoming-2\").all()<br>\u200b<br> &nbsp; &nbsp;print(course_list)<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<br>\u200b<\/pre>\n\n\n\n<p>\u4f8b2\uff0c\u865a\u62df\u5916\u952e\u4f7f\u7528\u7684\u65b9\u68482\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import json<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from sqlalchemy.orm import backref<br>from datetime import datetime<br>\u200b<br>db = SQLAlchemy()<br>app = Flask(__name__, template_folder=\"templates\", static_folder=\"static\")<br>\u200b<br># \u914d\u7f6e<br>app.config.update({<br> &nbsp; &nbsp;\"DEBUG\": True,<br> &nbsp; &nbsp;\"SQLALCHEMY_DATABASE_URI\": \"mysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\",<br> &nbsp; &nbsp;# \u5982\u679c\u4f7f\u7528pymysql\uff0c\u5219\u9700\u8981\u5728\u8fde\u63a5\u65f6\u6307\u5b9apymysql<br> &nbsp; &nbsp;# \"SQLALCHEMY_DATABASE_URI\": \"mysql+pymysql:\/\/root:123@127.0.0.1:3306\/flaskdemo?charset=utf8mb4\"<br> &nbsp; &nbsp;# \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e\uff0c\u5982\u672a\u8bbe\u7f6e\u53ea\u4f1a\u63d0\u793a\u8b66\u544a\uff0c\u8bbe\u7f6eFalse\u5373\u53ef<br> &nbsp; &nbsp;\"SQLALCHEMY_TRACK_MODIFICATIONS\": False,<br> &nbsp; &nbsp;# ORM\u6267\u884cSQL\u67e5\u8be2\u65f6\u662f\u54e6\u5426\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5\uff0cdebug\u6a21\u5f0f\u4e0b\u53ef\u4ee5\u5f00\u542f<br> &nbsp; &nbsp;\"SQLALCHEMY_ECHO\": True,<br>})<br>\u200b<br>db.init_app(app)<br>\u200b<br>\u200b<br>class Student(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u4fe1\u606f\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;# \u58f0\u660e\u4e0e\u5f53\u524d\u6a21\u578b\u7ed1\u5b9a\u7684\u6570\u636e\u8868\u540d\u79f0<br> &nbsp; &nbsp;__tablename__ = \"ts_student\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True,comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(15), comment=\"\u59d3\u540d\")<br> &nbsp; &nbsp;age = db.Column(db.SmallInteger, comment=\"\u5e74\u9f84\")<br> &nbsp; &nbsp;sex = db.Column(db.Boolean, default=True, comment=\"\u6027\u522b\")<br> &nbsp; &nbsp;email = db.Column(db.String(128), comment=\"\u90ae\u7bb1\u5730\u5740\")<br> &nbsp; &nbsp;money = db.Column(db.Numeric(10,2), default=0.0, comment=\"\u94b1\u5305\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f\"{self.name}&lt;Student&gt;\"<br>\u200b<br>\u200b<br>class Course(db.Model):<br> &nbsp; &nbsp;\"\"\"\u8bfe\u7a0b\u6570\u636e\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"ts_course\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;name = db.Column(db.String(64), unique=True, comment=\"\u8bfe\u7a0b\")<br> &nbsp; &nbsp;price = db.Column(db.Numeric(7, 2))<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return f'{self.name}&lt;{self.__class__.__name__}&gt;'<br>\u200b<br>\u200b<br>class StudentCourse(db.Model):<br> &nbsp; &nbsp;\"\"\"\u5b66\u751f\u548c\u8bfe\u7a0b\u4e4b\u95f4\u7684\u5173\u7cfb\u6a21\u578b\"\"\"<br> &nbsp; &nbsp;__tablename__ = \"ts_student_course\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952e\")<br> &nbsp; &nbsp;student_id = db.Column(db.Integer, index=True)<br> &nbsp; &nbsp;course_id = db.Column(db.Integer, index=True)<br> &nbsp; &nbsp;score = db.Column(db.Numeric(4,1), default=0, comment=\"\u6210\u7ee9\")<br> &nbsp; &nbsp;time = db.Column(db.DateTime, default=datetime.now, comment=\"\u8003\u8bd5\u65f6\u95f4\")<br>\u200b<br> &nbsp; &nbsp;student = db.relationship(<br> &nbsp; &nbsp; &nbsp; &nbsp;\"Student\",<br> &nbsp; &nbsp; &nbsp; &nbsp;uselist=False,<br> &nbsp; &nbsp; &nbsp; &nbsp;backref=backref(\"to_course\", uselist=True),<br> &nbsp; &nbsp; &nbsp; &nbsp;lazy=\"subquery\",<br> &nbsp; &nbsp; &nbsp; &nbsp;primaryjoin=\"Student.id==StudentCourse.student_id\",<br> &nbsp; &nbsp; &nbsp; &nbsp;foreign_keys=\"StudentCourse.student_id\"<br> &nbsp;  )<br>\u200b<br> &nbsp; &nbsp;course = db.relationship(<br> &nbsp; &nbsp; &nbsp; &nbsp;\"Course\",<br> &nbsp; &nbsp; &nbsp; &nbsp;uselist=False,<br> &nbsp; &nbsp; &nbsp; &nbsp;backref=backref(\"to_student\", uselist=True),<br> &nbsp; &nbsp; &nbsp; &nbsp;lazy=\"subquery\",<br> &nbsp; &nbsp; &nbsp; &nbsp;primaryjoin=\"Course.id==StudentCourse.course_id\",<br> &nbsp; &nbsp; &nbsp; &nbsp;foreign_keys=\"StudentCourse.course_id\"<br> &nbsp;  )<br>\u200b<br>@app.route(\"\/create\")<br>def create_table():<br> &nbsp; &nbsp;db.create_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/drop\")<br>def drop_table():<br> &nbsp; &nbsp;db.drop_all() &nbsp;# \u4e3a\u9879\u76ee\u4e2d\u88ab\u8bc6\u522b\u7684\u6240\u6709\u6a21\u578b\u5220\u9664\u6570\u636e\u8868<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/\")<br>def index():<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>@app.route(\"\/a1\")<br>def a1():<br> &nbsp; &nbsp;# \u6dfb\u52a0\u6d4b\u8bd5\u6570\u636e<br> &nbsp; &nbsp;stu0 = Student(name=\"xiaoming-0\",age=15,sex=True,email=\"xiaoming0@qq.com\", money=1000)<br> &nbsp; &nbsp;stu1 = Student(name=\"xiaoming-1\",age=15,sex=True,email=\"xiaoming1@qq.com\", money=1000)<br> &nbsp; &nbsp;stu2 = Student(name=\"xiaoming-2\",age=15,sex=True,email=\"xiaoming2@qq.com\", money=1000)<br> &nbsp; &nbsp;stu3 = Student(name=\"xiaoming-3\",age=15,sex=True,email=\"xiaoming3@qq.com\", money=1000)<br> &nbsp; &nbsp;stu4 = Student(name=\"xiaoming-4\",age=15,sex=True,email=\"xiaoming4@qq.com\", money=1000)<br>\u200b<br> &nbsp; &nbsp;db.session.add_all([stu0,stu1,stu2,stu3,stu4])<br> &nbsp; &nbsp;course1 = Course(name=\"python\u57fa\u7840\u7b2c1\u5b63\", price=1000)<br> &nbsp; &nbsp;course2 = Course(name=\"python\u57fa\u7840\u7b2c2\u5b63\", price=1000)<br> &nbsp; &nbsp;course3 = Course(name=\"python\u57fa\u7840\u7b2c3\u5b63\", price=1000)<br> &nbsp; &nbsp;course4 = Course(name=\"python\u57fa\u7840\u7b2c4\u5b63\", price=1000)<br> &nbsp; &nbsp;course5 = Course(name=\"python\u57fa\u7840\u7b2c5\u5b63\", price=1000)<br> &nbsp; &nbsp;db.session.add_all([course1, course2, course3, course4, course5])<br>\u200b<br> &nbsp; &nbsp;data = [<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=1,course_id=1,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=1,course_id=2,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=1,course_id=3,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=2,course_id=1,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=2,course_id=2,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=3,course_id=3,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=3,course_id=4,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=4,course_id=5,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=4,course_id=1,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=4,course_id=2,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=5,course_id=1,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=5,course_id=2,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=5,course_id=3,score=60,time=datetime.now()),<br> &nbsp; &nbsp; &nbsp; &nbsp;StudentCourse(student_id=5,course_id=4,score=60,time=datetime.now()),<br> &nbsp;  ]<br> &nbsp; &nbsp;db.session.add_all(data)<br> &nbsp; &nbsp;db.session.commit()<br>\u200b<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>@app.route(\"\/q1\")<br>def q1():<br> &nbsp; &nbsp;student = Student.query.filter(Student.name == \"xiaoming-0\").first()<br> &nbsp; &nbsp;# print(student.to_course)<br> &nbsp; &nbsp;# [&lt;StudentCourse 1&gt;, &lt;StudentCourse 2&gt;, &lt;StudentCourse 3&gt;]<br>\u200b<br> &nbsp; &nbsp;print([{<br> &nbsp; &nbsp; &nbsp; &nbsp;\"course_name\": item.course.name,<br> &nbsp; &nbsp; &nbsp; &nbsp;\"score\": item.score,<br> &nbsp; &nbsp; &nbsp; &nbsp;\"student_name\": item.student.name,<br> &nbsp;  } for item in student.to_course])<br> &nbsp; &nbsp;return \"ok\"<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Flask-SQLAlchemy flask\u9ed8\u8ba4\u63d0\u4f9b\u6a21\u578b\u64cd\u4f5c\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u63d0\u4f9bORM\uff0c\u6240\u4ee5\u4e00\u822c\u5f00\u53d1\u7684\u65f6\u5019\u6211\u4eec\u4f1a\u91c7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/22z.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":1,"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":15,"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions\/15"}],"wp:attachment":[{"href":"https:\/\/22z.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/22z.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/22z.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}