{"id":7040,"date":"2024-11-14T12:03:22","date_gmt":"2024-11-14T04:03:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7040\/"},"modified":"2024-11-14T12:03:22","modified_gmt":"2024-11-14T04:03:22","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e6%8d%ae%e8%bd%bb%e6%9d%be%e5%af%bc%e5%85%a5-postgresql%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7040\/","title":{"rendered":"\u5982\u4f55\u5c06\u6570\u636e\u8f7b\u677e\u5bfc\u5165 PostgreSQL\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173034601898184.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u6570\u636e\u8f7b\u677e\u5bfc\u5165 PostgreSQL\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u6570\u636e\u8f7b\u677e\u5bfc\u5165 PostgreSQL\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u6b64\u7c7b\u6570\u636e\u8f7b\u677e\u5bfc\u5165 postgresql<\/strong><\/p>\n<p>\u4f5c\u4e3a\u65b0\u624b\uff0c\u60a8\u53ef\u80fd\u60f3\u4e86\u89e3\u5982\u4f55\u5c06\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e\u5bfc\u5165\u6570\u636e\u5e93\uff0c\u4f8b\u5982 postgresql\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u64cd\u4f5c\uff1a<\/p>\n<p><strong><\/strong><\/p>\n<pre># \u521b\u5efa\u8868\ncreate table info (\n  code char(50),\n  topic varchar(50),\n  author varchar(50)\n);\n# \u4f7f\u7528 load data infile \u547d\u4ee4\u5bfc\u5165\u6570\u636e\nload data infile 'data.txt'\ninto table info\nfields terminated by ','\nlines terminated by '\n';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>postgresql<\/strong><\/p>\n<p>\u4f7f\u7528 postgresql \u7684  \u9a71\u52a8\u7a0b\u5e8f\uff1a<\/p>\n<pre>import psycopg2\n\n# \u8fde\u63a5\u5230\u6570\u636e\u5e93\nconn = psycopg2.connect(\n    \"host=localhost\",\n    \"user=postgres\",\n    \"password=password\",\n    \"database=mydatabase\"\n)\n\n# \u521b\u5efa\u6e38\u6807\ncur = conn.cursor()\n\n# \u521b\u5efa\u8868\ncur.execute(\"CREATE TABLE info (code CHAR(50), topic VARCHAR(50), author VARCHAR(50))\")\n\n# \u51c6\u5907\u63d2\u5165\u8bed\u53e5\nstmt = conn.prepare(\"INSERT INTO info VALUES ($1, $2, $3)\")\n\n# \u9010\u884c\u63d2\u5165\u6570\u636e\nwith open('data.txt') as f:\n    for line in f:\n        parts = line.split(',')\n        stmt(parts[0], parts[1], parts[2])\n\n# \u63d0\u4ea4\u4e8b\u52a1\nconn.commit()\n\n# \u5173\u95ed\u8fde\u63a5\nconn.close()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u6570\u636e\u8f7b\u677e\u5bfc\u5165 PostgreSQL\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\u5c06\u6b64\u7c7b\u6570\u636e\u8f7b\u677e\u5bfc\u5165 postgresql \u4f5c\u4e3a\u65b0\u624b\uff0c\u60a8\u53ef\u80fd\u60f3\u4e86\u89e3\u5982\u4f55\u5c06\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e\u5bfc\u5165\u6570\u636e\u5e93\uff0c\u4f8b\u5982 postgresql\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u64cd\u4f5c\uff1a # \u521b\u5efa\u8868 create table info ( code char(50), topic varchar(50), author varchar(50) ); # \u4f7f\u7528 load data infile \u547d\u4ee4\u5bfc\u5165\u6570\u636e load data infile &#8216;data.txt&#8217; into table info fields terminated by &#8216;,&#8217; lines terminated by &#8216; &#8216;; \u767b\u5f55\u540e\u590d\u5236 postgresql \u4f7f\u7528 postgresql \u7684 \u9a71\u52a8\u7a0b\u5e8f\uff1a import psycopg2 # \u8fde\u63a5\u5230\u6570\u636e\u5e93 conn = psycopg2.connect( &#8220;host=localhost&#8221;, &#8220;user=postgres&#8221;, &#8220;password=password&#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-7040","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7040","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=7040"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7040\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}