{"id":6952,"date":"2024-11-14T13:27:33","date_gmt":"2024-11-14T05:27:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6952\/"},"modified":"2024-11-14T13:27:33","modified_gmt":"2024-11-14T05:27:33","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","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6952\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173017265556035.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  \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  \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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 Python \u5c06\u6570\u636e\u5bfc\u5165 PostgreSQL \u6570\u636e\u5e93\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>\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 \u76f8\u6bd4\uff0cpostgresql \u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002 \u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 \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;) # \u51c6\u5907 INSERT \u8bed\u53e5 stmt = cur.prepare(&#8220;INSERT INTO info VALUES (%s, %s, %s)&#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-6952","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6952","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=6952"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6952\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}