{"id":7775,"date":"2024-11-14T15:52:02","date_gmt":"2024-11-14T07:52:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7775\/"},"modified":"2024-11-14T15:52:02","modified_gmt":"2024-11-14T07:52:02","slug":"flask%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad%e7%9a%84%e5%9b%be%e7%89%87%e5%b9%b6%e8%bf%94%e5%9b%9e%e7%bb%99%e5%89%8d%e7%ab%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7775\/","title":{"rendered":"Flask\u5982\u4f55\u8bfb\u53d6MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u8fd4\u56de\u7ed9\u524d\u7aef\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173122920485985.jpg\" class=\"aligncenter\" title=\"Flask\u5982\u4f55\u8bfb\u53d6MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u8fd4\u56de\u7ed9\u524d\u7aef\uff1f\u63d2\u56fe\" alt=\"Flask\u5982\u4f55\u8bfb\u53d6MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u8fd4\u56de\u7ed9\u524d\u7aef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>flask\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u8fd4\u56de\u7ed9\u524d\u7aef<\/strong><\/p>\n<p>\u5728\u672c\u95ee\u9898\u4e2d\uff0c\u63d0\u95ee\u8005\u5e0c\u671b\u4e86\u89e3\u5982\u4f55\u4f7f\u7528flask\u8bfb\u53d6mysql\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u5c06\u5176\u8fd4\u56de\u7ed9\u524d\u7aef\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u67e5\u8be2\u6570\u636e\u5e93\u4ee5\u83b7\u53d6\u56fe\u50cf\u6570\u636e\uff1a<\/strong><br \/>\u4f7f\u7528sql\u67e5\u8be2\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u56fe\u50cf\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u67e5\u8be2\u53ef\u80fd\u7c7b\u4f3c\u4e8e select image from table where id = 1\u3002<\/li>\n<li> <strong>\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u68c0\u7d22\u56fe\u50cf\u6570\u636e\uff1a<\/strong><br \/>\u67e5\u8be2\u7ed3\u679c\u5c06\u662f\u5143\u7ec4\uff0c\u56fe\u50cf\u4e8c\u8fdb\u5236\u6570\u636e\u4f5c\u4e3a\u5143\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5b58\u50a8\u3002\u4f7f\u7528\u5143\u7ec4\u7d22\u5f15\uff08\u901a\u5e38\u4e3a [0]\uff09\u63d0\u53d6\u56fe\u50cf\u6570\u636e\u3002<\/li>\n<li> <strong>\u53d1\u9001\u56fe\u50cf\u6570\u636e\u7ed9\u524d\u7aef\uff1a<\/strong><br \/>\u5c06\u56fe\u50cf\u6570\u636e\u4f5c\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u6216\u6587\u4ef6\u683c\u5f0f\u53d1\u9001\u56de\u524d\u7aef\u3002\u53ef\u4ee5\u4f7f\u7528 send_file() \u65b9\u6cd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/li>\n<\/ol>\n<pre>row = MySql_t2.execute_query(sq1)\n\nif row == None:\n    raise Exception('...')\n\nimage_bytes = row[0]\n\nreturn send_file(image_bytes, mime_type='...')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li> <strong>\u5728\u524d\u7aef\u8bf7\u6c42\u56fe\u50cf\uff1a<\/strong><br \/>\u524d\u7aef\u5e94\u4f7f\u7528\u9002\u5f53\u7684\u8bf7\u6c42\u65b9\u6cd5\uff08\u5982 get \u6216 post\uff09\u5411\u670d\u52a1\u5668\u8bf7\u6c42\u56fe\u50cf\u3002\u8bf7\u6c42\u53ef\u4ee5\u5305\u62ec\u56fe\u50cf\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff08\u5982 id\uff09\u3002<\/li>\n<\/ol>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u786e\u4fdd mysql_\u20202 \u662f\u4e00\u4e2a\u5177\u6709 execute_query() \u65b9\u6cd5\u7684\u53ef\u7528\u6765\u67e5\u8be2\u6570\u636e\u5e93\u7684\u7c7b\u6216\u51fd\u6570\u3002<\/li>\n<li>\u6ce8\u610f\u56fe\u50cf\u6570\u636e\u4ee5\u4e8c\u8fdb\u5236\u683c\u5f0f\u5b58\u50a8\uff0c\u56e0\u6b64\u5728\u8fd4\u56de\u7ed9\u524d\u7aef\u65f6\u9700\u8981\u4f7f\u7528\u6b63\u786e\u7684mime\u7c7b\u578b\uff08\u5982 image\/jpeg\uff09\u3002<\/li>\n<li>\u660e\u786e\u4ee3\u7801\u7684\u7c7b\u578b\u53ef\u4ee5\u907f\u514d\u9519\u8bef\uff0c\u5982\u786e\u4fdd row \u662f\u5143\u7ec4\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fFlask\u5982\u4f55\u8bfb\u53d6MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u8fd4\u56de\u7ed9\u524d\u7aef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>flask\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u8fd4\u56de\u7ed9\u524d\u7aef \u5728\u672c\u95ee\u9898\u4e2d\uff0c\u63d0\u95ee\u8005\u5e0c\u671b\u4e86\u89e3\u5982\u4f55\u4f7f\u7528flask\u8bfb\u53d6mysql\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u5c06\u5176\u8fd4\u56de\u7ed9\u524d\u7aef\u3002 \u6b65\u9aa4\uff1a \u67e5\u8be2\u6570\u636e\u5e93\u4ee5\u83b7\u53d6\u56fe\u50cf\u6570\u636e\uff1a\u4f7f\u7528sql\u67e5\u8be2\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u56fe\u50cf\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u67e5\u8be2\u53ef\u80fd\u7c7b\u4f3c\u4e8e select image from table where id = 1\u3002 \u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u68c0\u7d22\u56fe\u50cf\u6570\u636e\uff1a\u67e5\u8be2\u7ed3\u679c\u5c06\u662f\u5143\u7ec4\uff0c\u56fe\u50cf\u4e8c\u8fdb\u5236\u6570\u636e\u4f5c\u4e3a\u5143\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5b58\u50a8\u3002\u4f7f\u7528\u5143\u7ec4\u7d22\u5f15\uff08\u901a\u5e38\u4e3a [0]\uff09\u63d0\u53d6\u56fe\u50cf\u6570\u636e\u3002 \u53d1\u9001\u56fe\u50cf\u6570\u636e\u7ed9\u524d\u7aef\uff1a\u5c06\u56fe\u50cf\u6570\u636e\u4f5c\u4e3a\u4e8c\u8fdb\u5236\u6570\u636e\u6216\u6587\u4ef6\u683c\u5f0f\u53d1\u9001\u56de\u524d\u7aef\u3002\u53ef\u4ee5\u4f7f\u7528 send_file() \u65b9\u6cd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a row = MySql_t2.execute_query(sq1) if row == None: raise Exception(&#8216;&#8230;&#8217;) image_bytes = row[0] return send_file(image_bytes, mime_type=&#8217;&#8230;&#8217;) \u767b\u5f55\u540e\u590d\u5236 \u5728\u524d\u7aef\u8bf7\u6c42\u56fe\u50cf\uff1a\u524d\u7aef\u5e94\u4f7f\u7528\u9002\u5f53\u7684\u8bf7\u6c42\u65b9\u6cd5\uff08\u5982 get \u6216 post\uff09\u5411\u670d\u52a1\u5668\u8bf7\u6c42\u56fe\u50cf\u3002\u8bf7\u6c42\u53ef\u4ee5\u5305\u62ec\u56fe\u50cf\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff08\u5982 id\uff09\u3002 \u63d0\u793a\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u786e\u4fdd mysql_\u20202 \u662f\u4e00\u4e2a\u5177\u6709 execute_query() \u65b9\u6cd5\u7684\u53ef\u7528\u6765\u67e5\u8be2\u6570\u636e\u5e93\u7684\u7c7b\u6216\u51fd\u6570\u3002 \u6ce8\u610f\u56fe\u50cf\u6570\u636e\u4ee5\u4e8c\u8fdb\u5236\u683c\u5f0f\u5b58\u50a8\uff0c\u56e0\u6b64\u5728\u8fd4\u56de\u7ed9\u524d\u7aef\u65f6\u9700\u8981\u4f7f\u7528\u6b63\u786e\u7684mime\u7c7b\u578b\uff08\u5982 image\/jpeg\uff09\u3002 \u660e\u786e\u4ee3\u7801\u7684\u7c7b\u578b\u53ef\u4ee5\u907f\u514d\u9519\u8bef\uff0c\u5982\u786e\u4fdd row \u662f\u5143\u7ec4\u7c7b\u578b\u3002 \u4ee5\u4e0a\u5c31\u662fFlask\u5982\u4f55\u8bfb\u53d6MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u5e76\u8fd4\u56de\u7ed9\u524d\u7aef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-7775","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=7775"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7775\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}