{"id":27606,"date":"2024-11-24T10:33:47","date_gmt":"2024-11-24T02:33:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27606\/"},"modified":"2024-11-24T10:33:47","modified_gmt":"2024-11-24T02:33:47","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8flask%e5%b0%86mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad%e7%9a%84%e5%9b%be%e7%89%87%e8%bf%94%e5%9b%9e%e8%87%b3%e5%89%8d%e7%ab%af%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27606\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Flask\u5c06MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u8fd4\u56de\u81f3\u524d\u7aef\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173129580411023.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Flask\u5c06MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u8fd4\u56de\u81f3\u524d\u7aef\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Flask\u5c06MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u8fd4\u56de\u81f3\u524d\u7aef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>flask\u5c06\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u8fd4\u56de\u81f3\u524d\u7aef\u7684\u6b65\u9aa4<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><br \/>\u5728\u4f7f\u7528flask\u6784\u5efaweb\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u6709\u65f6\u9700\u8981\u4ecemysql\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u56fe\u7247\u5e76\u5c06\u5176\u8fd4\u56de\u7ed9\u524d\u7aef\u3002\u4ee5\u4e0b\u6b65\u9aa4\u5c06\u6307\u5bfc\u4f60\u5b8c\u6210\u6b64\u64cd\u4f5c\uff1a<\/p>\n<p><strong>1. \u6570\u636e\u5e93\u8fde\u63a5<\/strong><br \/>\u9996\u5148\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5230\u4f60\u7684mysql\u6570\u636e\u5e93\uff1a<\/p>\n<pre>import mysql.connector\n\nconnection = mysql.connector.connect(\n    host='\u4f60\u7684\u4e3b\u673a\u5730\u5740',\n    user='\u4f60\u7684\u7528\u6237\u540d',\n    password='\u4f60\u7684\u5bc6\u7801',\n    database='\u4f60\u7684\u6570\u636e\u5e93\u540d\u79f0',\n    buffered=true\n)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u67e5\u8be2\u56fe\u7247<\/strong><br \/>\u63a5\u4e0b\u6765\uff0c\u7f16\u5199\u4e00\u4e2asql\u67e5\u8be2\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u56fe\u7247\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>select image_data from table_name where id = 1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u6267\u884c\u67e5\u8be2<\/strong><br \/>\u4f7f\u7528\u5bf9\u8c61\u8fd0\u884c\u67e5\u8be2\uff1a<\/p>\n<pre>cursor = connection.cursor()\ncursor.execute(sql)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u63d0\u53d6\u56fe\u7247<\/strong><br \/>\u67e5\u8be2\u7ed3\u679c\u5c06\u8fd4\u56de\u5143\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u7ed3\u679c\u884c\u3002\u63d0\u53d6\u56fe\u7247\u6570\u636e\uff1a<\/p>\n<pre>image_bytes = row[0]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u8fd4\u56de\u56fe\u7247<\/strong><br \/>\u4f7f\u7528flask\u7684send_file()\u51fd\u6570\u5c06\u56fe\u7247\u8fd4\u56de\u7ed9\u524d\u7aef\uff1a<\/p>\n<pre>return send_file(image_bytes, mimetype='image\/png')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fddimage_data\u5217\u7684\u6570\u636e\u7c7b\u578b\u4e3a\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff08blob\uff09\u3002<\/li>\n<li>\u56fe\u7247\u4e8c\u8fdb\u5236\u6570\u636e\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u4e3a\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u89e3\u7801\u6210bytes\u624d\u80fd\u6b63\u786e\u5904\u7406\u3002<\/li>\n<li>\u6ce8\u610f\u4ee3\u7801\u7f16\u8f91\u5668\u7684\u7c7b\u578b\u63d0\u793a\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u7c7b\u578b\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Flask\u5c06MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u8fd4\u56de\u81f3\u524d\u7aef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>flask\u5c06\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u8fd4\u56de\u81f3\u524d\u7aef\u7684\u6b65\u9aa4 \u524d\u8a00\u5728\u4f7f\u7528flask\u6784\u5efaweb\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u6709\u65f6\u9700\u8981\u4ecemysql\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u56fe\u7247\u5e76\u5c06\u5176\u8fd4\u56de\u7ed9\u524d\u7aef\u3002\u4ee5\u4e0b\u6b65\u9aa4\u5c06\u6307\u5bfc\u4f60\u5b8c\u6210\u6b64\u64cd\u4f5c\uff1a 1. \u6570\u636e\u5e93\u8fde\u63a5\u9996\u5148\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u5230\u4f60\u7684mysql\u6570\u636e\u5e93\uff1a import mysql.connector connection = mysql.connector.connect( host=&#8217;\u4f60\u7684\u4e3b\u673a\u5730\u5740&#8217;, user=&#8217;\u4f60\u7684\u7528\u6237\u540d&#8217;, password=&#8217;\u4f60\u7684\u5bc6\u7801&#8217;, database=&#8217;\u4f60\u7684\u6570\u636e\u5e93\u540d\u79f0&#8217;, buffered=true ) \u767b\u5f55\u540e\u590d\u5236 2. \u67e5\u8be2\u56fe\u7247\u63a5\u4e0b\u6765\uff0c\u7f16\u5199\u4e00\u4e2asql\u67e5\u8be2\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u56fe\u7247\u3002\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b select image_data from table_name where id = 1; \u767b\u5f55\u540e\u590d\u5236 3. \u6267\u884c\u67e5\u8be2\u4f7f\u7528\u5bf9\u8c61\u8fd0\u884c\u67e5\u8be2\uff1a cursor = connection.cursor() cursor.execute(sql) \u767b\u5f55\u540e\u590d\u5236 4. \u63d0\u53d6\u56fe\u7247\u67e5\u8be2\u7ed3\u679c\u5c06\u8fd4\u56de\u5143\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u7ed3\u679c\u884c\u3002\u63d0\u53d6\u56fe\u7247\u6570\u636e\uff1a image_bytes = row[0] \u767b\u5f55\u540e\u590d\u5236 5. \u8fd4\u56de\u56fe\u7247\u4f7f\u7528flask\u7684send_file()\u51fd\u6570\u5c06\u56fe\u7247\u8fd4\u56de\u7ed9\u524d\u7aef\uff1a return send_file(image_bytes, mimetype=&#8217;image\/png&#8217;) \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\u4e8b\u9879 \u786e\u4fddimage_data\u5217\u7684\u6570\u636e\u7c7b\u578b\u4e3a\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff08blob\uff09\u3002 \u56fe\u7247\u4e8c\u8fdb\u5236\u6570\u636e\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u4e3a\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u89e3\u7801\u6210bytes\u624d\u80fd\u6b63\u786e\u5904\u7406\u3002 \u6ce8\u610f\u4ee3\u7801\u7f16\u8f91\u5668\u7684\u7c7b\u578b\u63d0\u793a\uff0c\u6709\u52a9\u4e8e\u907f\u514d\u7c7b\u578b\u9519\u8bef\u3002 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Flask\u5c06MySQL\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u8fd4\u56de\u81f3\u524d\u7aef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\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-27606","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27606","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=27606"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27606\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}