{"id":44752,"date":"2024-12-01T09:58:04","date_gmt":"2024-12-01T01:58:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44752\/"},"modified":"2024-12-01T09:58:04","modified_gmt":"2024-12-01T01:58:04","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e5%b0%86%e6%95%b0%e6%8d%ae%e5%af%bc%e5%85%a5-postgresql-%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/44752\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6570\u636e\u5e93\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/1730434544672455f0da0f1.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 postgresql \u4e2d\u5bfc\u5165\u6570\u636e<\/strong><\/p>\n<p>\u5982\u679c\u60f3\u8981\u5c06\u7c7b\u4f3c\u4e8e\u95ee\u9898\u4e2d\u7ed9\u51fa\u7684\u6570\u636e\u683c\u5f0f\u5bfc\u5165\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 postgresql\u3002\u4e0e mysql \u76f8\u6bd4\uff0cpostgresql \u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 python \u7684 psycopg \u9a71\u52a8\u7a0b\u5e8f\u5c06\u6570\u636e\u5bfc\u5165 postgresql \u4e2d\uff1a<\/p>\n<pre>import psycopg2\n\n# \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5\nconn = psycopg2.connect(\n    host=\"localhost\",\n    port=\"5432\",\n    database=\"my_database\",\n    user=\"my_user\",\n    password=\"my_password\",\n)\n# \u83b7\u53d6\u6e38\u6807\ncur = conn.cursor()\n\n# \u521b\u5efa\u8868\ncur.execute(\"CREATE TABLE info (code VARCHAR(50), topic VARCHAR(50), author VARCHAR(50))\")\n\n# \u51c6\u5907 INSERT \u8bed\u53e5\nstmt = cur.prepare(\"INSERT INTO info VALUES (%s, %s, %s)\")\n\n# \u6267\u884c INSERT \u8bed\u53e5\nfor code, topic, author in data:\n    stmt(code, topic, author)\n\n# \u63d0\u4ea4\u66f4\u6539\nconn.commit()\n\n# \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5\ncur.close()\nconn.close()<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5c06\u6570\u636e\u6210\u529f\u5bfc\u5165\u5230 postgresql \u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6570\u636e\u5e93\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u5982\u4f55\u5728 postgresql \u4e2d\u5bfc\u5165\u6570\u636e \u5982\u679c\u60f3\u8981\u5c06\u7c7b\u4f3c\u4e8e\u95ee\u9898\u4e2d\u7ed9\u51fa\u7684\u6570\u636e\u683c\u5f0f\u5bfc\u5165\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528 postgresql\u3002\u4e0e mysql \u76f8\u6bd4\uff0cpostgresql \u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002 \u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 python \u7684 psycopg \u9a71\u52a8\u7a0b\u5e8f\u5c06\u6570\u636e\u5bfc\u5165 postgresql \u4e2d\uff1a import psycopg2 # \u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5 conn = psycopg2.connect( host=&#8221;localhost&#8221;, port=&#8221;5432&#8243;, database=&#8221;my_database&#8221;, user=&#8221;my_user&#8221;, password=&#8221;my_password&#8221;, ) # \u83b7\u53d6\u6e38\u6807 cur = conn.cursor() # \u521b\u5efa\u8868 cur.execute(&#8220;CREATE TABLE info (code VARCHAR(50), topic VARCHAR(50), author VARCHAR(50))&#8221;) # [&hellip;]<\/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-44752","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44752","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=44752"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44752\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}