{"id":204072,"date":"2025-05-29T10:58:27","date_gmt":"2025-05-29T02:58:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204072\/"},"modified":"2025-05-29T10:58:27","modified_gmt":"2025-05-29T02:58:27","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e5%ae%a2%e6%88%b7%e7%ab%af%e8%ae%be%e7%bd%ae-sql-%e6%9f%a5%e8%af%a2%e8%b6%85%e6%97%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204072\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u5ba2\u6237\u7aef\u8bbe\u7f6e SQL \u67e5\u8be2\u8d85\u65f6\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u5ba2\u6237\u7aef\u8bbe\u7f6e SQL \u67e5\u8be2\u8d85\u65f6\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729951063671cf55796117.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 python \u5ba2\u6237\u7aef\u5b9e\u73b0 sql \u67e5\u8be2\u8d85\u65f6<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 python orm\uff08\u5982 django orm\u3001peewee\u3001sqlalchemy\uff09\u4e0e\u6570\u636e\u5e93\u9a71\u52a8\uff08\u5982 pymysql\uff09\u4ee5\u53ca fastapi\u3001flask\u3001django \u7b49\u5e94\u7528\u6846\u67b6\u65f6\uff0c\u5b58\u5728\u4e00\u4e2a\u6f5c\u5728\u7684\u5b89\u5168\u9690\u60a3\uff1asql \u67e5\u8be2\u53ef\u80fd\u9700\u8981\u957f\u65f6\u95f4\u8fd0\u884c\uff0c\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u6302\u8d77\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u67e5\u8be2\u65f6\u95f4\u4e0a\u9650\uff0c\u4ee5\u4fbf\u5728\u67e5\u8be2\u8d85\u8fc7\u4e00\u5b9a\u65f6\u95f4\u540e\u81ea\u52a8\u7ec8\u6b62\u3002<\/p>\n<p>\u4e00\u79cd\u4f18\u96c5\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f\u5229\u7528\u6570\u636e\u5e93\u9a71\u52a8\u5e93\u4e2d\u7684\u8d85\u65f6\u8bbe\u7f6e\uff1a<\/p>\n<p>\u5bf9\u4e8e pymysql\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4ee5\u4e0b\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>&#8220;connection_timeout&#8221;: \u8fde\u63a5\u8d85\u65f6\u65f6\u95f4<\/li>\n<li>&#8220;read_timeout&#8221;: \u8bfb\u64cd\u4f5c\u8d85\u65f6\u65f6\u95f4<\/li>\n<li>&#8220;write_timeout&#8221;: \u5199\u64cd\u4f5c\u8d85\u65f6\u65f6\u95f4<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u8981\u5c06\u67e5\u8be2\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u4e3a 60 \u79d2\uff0c\u53ef\u4ee5\u5982\u4e0b\u8bbe\u7f6e\uff1a<\/p>\n<pre>import pymysql\n\n# \u8fde\u63a5\u5230\u6570\u636e\u5e93\nconnection = pymysql.connect(\n    host='localhost',\n    user='user',\n    password='password',\n    database='database',\n    connection_timeout=60,\n    read_timeout=60,\n    write_timeout=60\n)<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5f53 sql \u67e5\u8be2\u8fd0\u884c\u65f6\u95f4\u8d85\u8fc7 60 \u79d2\u65f6\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u5c06\u81ea\u52a8\u7ec8\u6b62\uff0c\u4ece\u800c\u907f\u514d\u5e94\u7528\u7a0b\u5e8f\u6302\u8d77\u3002\u8fd9\u662f\u5728\u5ba2\u6237\u7aef\u8bbe\u7f6e\u67e5\u8be2\u8d85\u65f6\u7684\u4f18\u96c5\u4e14\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u800c\u65e0\u9700\u4fee\u6539 mysql \u670d\u52a1\u5668\u914d\u7f6e\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4f7f\u7528 Python \u5ba2\u6237\u7aef\u8bbe\u7f6e SQL \u67e5\u8be2\u8d85\u65f6\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u5ba2\u6237\u7aef\u8bbe&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204072","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204072"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204072\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}