{"id":6389,"date":"2021-04-14T22:38:00","date_gmt":"2021-04-14T14:38:00","guid":{"rendered":"https:\/\/vip.fxxkpython.com\/?p=6389"},"modified":"2021-04-15T22:51:21","modified_gmt":"2021-04-15T14:51:21","slug":"%e4%bd%bf%e7%94%a8-python-%e4%bb%8e-0-%e5%bc%80%e5%8f%91%e5%b1%9e%e4%ba%8e%e4%bd%a0%e7%9a%84%e4%b8%aa%e4%ba%ba%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f-07-%e7%af%87-%ef%bd%9c-%e5%a6%82%e4%bd%95%e4%bd%bf","status":"publish","type":"post","link":"https:\/\/vip.fxxkpython.com\/?p=6389","title":{"rendered":"\u4f7f\u7528 Python \u4ece 0 \u5f00\u53d1\u5c5e\u4e8e\u4f60\u7684\u4e2a\u4eba\u7ba1\u7406\u7cfb\u7edf 07 \u7bc7 \uff5c \u5982\u4f55\u4f7f\u7528 flask-sqlalchemy \u521b\u5efa\u6570\u636e\u8868\uff0c\u5b9e\u73b0\u589e\u5220\u6539\u67e5"},"content":{"rendered":"\n<p>\u76f8\u4fe1\u4f60\u5df2\u7ecf\u5b89\u88c5\u597d\u4e86 Mysql \u4ee5\u53ca flask_sqlalchemy\uff0c\u914d\u7f6e\u597d\u4e86\u6570\u636e\u5e93\u7684\u8fde\u63a5\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u4f7f\u7528\u5b83\u521b\u5efa\u6570\u636e\u5e93\u8868\uff0c\u5b9e\u73b0\u589e\u5220\u6539\u67e5\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728 app.py \u4e2d\uff0c\u5bfc\u5165\u76f8\u5173\u7684\u5e93\uff0c\u83b7\u53d6 db \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">\n\nfrom flask import Flask\nfrom flask_sqlalchemy import SQLAlchemy\nimport config\n\napp = Flask(__name__)\n# \u4f7f\u7528\u914d\u7f6e\napp.config.from_object(config)\n\n# \u5b9e\u4f8b\u5316\u6570\u636e\u5e93\u5bf9\u8c61\ndb = SQLAlchemy(app)\n\n@app.route('\/')\ndef index():\n    return '\u8fd9\u662f\u9996\u9875\uff01\uff01'\n\nif __name__ == '__main__':\n    app.run(debug=True)\n<\/pre><\/div>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u9700\u8981\u4e00\u4e2a\u6570\u636e\u8868\uff0c\u7528\u6765\u5b58\u50a8\u81ea\u5df1\u7684\u5065\u8eab\u6570\u636e\uff0c\u6bd4\u5982\u8fd0\u52a8\u7684\u9879\u76ee\u540d\u79f0\uff0c\u8fd0\u52a8\u7684\u65f6\u95f4\uff0c\u71c3\u70e7\u7684\u5361\u8def\u91cc\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7c7b Model\uff0c\u5b9a\u4e49\u76f8\u5173\u7684\u5b57\u6bb5\uff0c\u8ba9\u7c7b\u7684\u5c5e\u6027\u548c\u6570\u636e\u8868\u7684\u5b57\u6bb5\u5bf9\u5e94\u8d77\u6765\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">\n\nfrom flask import Flask\nfrom flask_sqlalchemy import SQLAlchemy\nimport config\n\napp = Flask(__name__)\n# \u4f7f\u7528\u914d\u7f6e\napp.config.from_object(config)\n\n# \u5b9e\u4f8b\u5316\u6570\u636e\u5e93\u5bf9\u8c61\ndb = SQLAlchemy(app)\n\n# \u6570\u636e\u6a21\u578b\nclass Fitness(db.Model):\n    __tablename__ = 'fitness'\n\n    id = db.Column(db.Integer, primary_key=True)\n    sport_name = db.Column(db.String(100))\n    calorie = db.Column(db.Float)\n\n@app.route('\/')\ndef index():\n    return '\u8fd9\u662f\u9996\u9875\uff01\uff01'\n\nif __name__ == '__main__':\n    app.run(debug=True)\n\n<\/pre><\/div>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86 Fitness \u7c7b\uff0c\u96c6\u6210\u4e86 Model\uff0c\u5728\u8fd9\u4e2a\u7c7b\u91cc\u9762\u5b9a\u4e49\u7684\u5c5e\u6027\u53ef\u4ee5\u4f5c\u4e3a\u6570\u636e\u5e93\u8868\u7684\u5b57\u6bb5\uff0c\u5176\u4e2d\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><code>__tablename__<\/code> \u6307\u5b9a\u4e86\u8868\u540d<\/p><p>sport_name \u6307\u5b9a\u4e86\u8fd0\u52a8\u9879\u76ee\u540d\u79f0\u5b57\u6bb5<\/p><p>calorie \u6307\u5b9a\u4e86\u5361\u8def\u91cc\u5b57\u6bb5<\/p><\/blockquote>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 db.creat_all() \u5c06\u6570\u636e\u6a21\u578b\u76f4\u63a5\u521b\u5efa\u5230 MySQL \u4e2d\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528 flask \u7684 shell \u547d\u4ee4\u6765\u7ed9\u4f60\u6f14\u793a\u4e00\u4e0b\uff0c\u5728\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u4e0b\u4f7f\u7528 flask shell \u547d\u4ee4\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">\n\nflask shell\nPython 3.9.1 (default, Dec 28 2020, 11:24:06) \n[Clang 12.0.0 (clang-1200.0.32.28)] on darwin\nApp: mysystem.app [production]\nInstance: \/Users\/wistbean\/vip_tutorial\/personal_system\/instance\n&gt;&gt;&gt; from mysystem.app import db\n&gt;&gt;&gt; db.create_all()\n&gt;&gt;&gt; \n\n\n<\/pre><\/div>\n\n\n\n<p>\u8fd9\u65f6\u5019\u4f60\u53ef\u4ee5\u8fdb\u5165<div style=\"text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;\">\u672c\u6587\u9690\u85cf\u5185\u5bb9 <a href=\"https:\/\/vip.fxxkpython.com\/wp-login.php?redirect_to=https%3A%2F%2Fvip.fxxkpython.com%2F%3Fp%3D6389\">\u767b\u9646<\/a> \u540e\u624d\u53ef\u4ee5\u6d4f\u89c8<\/div>\u73b0\u5728\u6570\u636e\u5e93\u4e2d\u7684\u8868\u6570\u636e\u5c31\u53d8\u4e86\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">\nmysql&gt; select * from fitness;\n+----+--------------+---------+\n| id | sport_name   | calorie |\n+----+--------------+---------+\n|  1 | \u4fef\u5367\u6491       |     100 |\n|  2 | \u5f15\u4f53\u5411\u4e0a     |     200 |\n+----+--------------+---------+\n2 rows in set (0.00 sec)\n\n<\/pre><\/div>\n\n\n\n<h1>\u5220<\/h1>\n\n\n\n<p>\u60f3\u8981\u5220\u9664\u8bb0\u5f55\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 delete \u65b9\u6cd5\u5c06\u6570\u636e\u5bf9\u8c61\u5220\u9664\uff0c\u7136\u540e\u63d0\u4ea4\u5373\u53ef\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">\n\n&gt;&gt;&gt; fit1 = Fitness.query.get(1)\n&gt;&gt;&gt; db.session.delete(fit1)\n&gt;&gt;&gt; db.session.commit()\n&gt;&gt;&gt; \n\n<\/pre><\/div>\n\n\n\n<p>\u90a3\u4e48\u73b0\u5728\u73b0\u5728\u6570\u636e\u5e93\u4e2d\u7684\u8868\u6570\u636e 1 \u5c31\u88ab\u5220\u9664\u4e86\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;wpum_restrict_type&quot;:&quot;wpum_restrict_type_state&quot;,&quot;wpum_hide_state_in&quot;:false,&quot;wpum_hide_state_out&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">\nmysql&gt; select * from fitness;\n+----+--------------+---------+\n| id | sport_name   | calorie |\n+----+--------------+---------+\n|  2 | \u5f15\u4f53\u5411\u4e0a     |     200 |\n+----+--------------+---------+\n1 row in set (0.00 sec)\n\n<\/pre><\/div>\n\n\n\n<p>OK\uff0c\u4ee5\u4e0a\u5c31\u662fflask-sqlalchemy \u521b\u5efa\u6570\u636e\u8868\uff0c\u4ee5\u53ca\u589e\u5220\u6539\u67e5\u7684\u4f7f\u7528\uff0c\u5f53\u7136\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u6d89\u53ca\u5230\u4e86\u6570\u636e\u5e93\u7684\u57fa\u672c\u7c7b\u578b\uff0c\u4ee5\u53ca\u6570\u636e\u5e93\u7684\u7b80\u5355\u64cd\u4f5c\uff0c\u6211\u4eec\u540e\u9762\u8fd8\u4f1a\u7ee7\u7eed\u5e38\u5e38\u4f7f\u7528\u5230\u5b83\u4eec\uff0c\u751a\u81f3\u4e8e\u8868\u6570\u636e\u95f4\u7684\u5173\u8054\uff0c\u8fd9\u7bc7\u4f60\u6bd4\u8f83\u9700\u8981\u8ba4\u8bc6\u5230\u7684\u7684\u662f\uff0c\u8868\u548c\u5bf9\u8c61\u4e4b\u95f4\u7684\u6570\u636e\u5173\u8054\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u4e0b\u7bc7\u7ee7\u7eed\uff0cpeace\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76f8\u4fe1\u4f60\u5df2\u7ecf\u5b89\u88c5\u597d\u4e86 Mysql \u4ee5\u53ca flask_sqlalchemy\uff0c\u914d\u7f6e\u597d\u4e86\u6570\u636e\u5e93\u7684\u8fde\u63a5\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u4f7f\u7528\u5b83\u521b\u5efa\u6570\u636e\u5e93\u8868\uff0c\u5b9e\u73b0\u589e\u5220\u6539\u67e5\u3002 \u4f60\u53ef\u4ee5\u5728 app.py \u4e2d\uff0c\u5bfc\u5165\u76f8\u5173\u7684\u5e93\uff0c\u83b7\u53d6 db \u5bf9\u8c61\uff1a \u5047\u8bbe\u6211\u4eec\u73b0\u5728\u9700\u8981\u4e00\u4e2a\u6570\u636e\u8868\uff0c\u7528\u6765\u5b58\u50a8\u81ea\u5df1\u7684&hellip;<\/p>\n","protected":false},"author":1,"featured_media":6391,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[99],"tags":[549,582,346,583,584],"_links":{"self":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6389"}],"collection":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6389"}],"version-history":[{"count":1,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6389\/revisions"}],"predecessor-version":[{"id":6392,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/6389\/revisions\/6392"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/media\/6391"}],"wp:attachment":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}