{"id":171899,"date":"2024-11-07T08:36:26","date_gmt":"2024-11-07T00:36:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/171899\/"},"modified":"2024-11-07T08:36:27","modified_gmt":"2024-11-07T00:36:27","slug":"%e8%bc%95%e9%ac%86%e5%ad%b8%e7%bf%92python%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e5%85%a5%e9%96%80%e6%8c%87%e5%8d%97-python%e6%95%b8%e6%93%9a%e5%ba%ab%e5%85%a5%e9%96%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/171899\/","title":{"rendered":"\u8f15\u9b06\u5b78\u7fd2Python\u6578\u64da\u5eab\uff1a\u5165\u9580\u6307\u5357 (python\u6578\u64da\u5eab\u5165\u9580)"},"content":{"rendered":"<h1 id=\"%e8%bc%95%e9%ac%86%e5%ad%b8%e7%bf%92python%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e5%85%a5%e9%96%80%e6%8c%87%e5%8d%97-iDwNxuqrUI\">\u8f15\u9b06\u5b78\u7fd2Python\u6578\u64da\u5eab\uff1a\u5165\u9580\u6307\u5357<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5de5\u5177\u548c\u5eab\u4f86\u8f15\u9b06\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u4ecb\u7d39Python\u6578\u64da\u5eab\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\uff0c\u5e6b\u52a9\u521d\u5b78\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-iDwNxuqrUI\">\u4ec0\u9ebc\u662f\u6578\u64da\u5eab\uff1f<\/h2>\n<p>\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u6709\u7d44\u7e54\u7684\u6578\u64da\u96c6\u5408\uff0c\u901a\u5e38\u7528\u65bc\u5b58\u5132\u3001\u7ba1\u7406\u548c\u6aa2\u7d22\u4fe1\u606f\u3002\u6578\u64da\u5eab\u53ef\u4ee5\u5206\u70ba\u591a\u7a2e\u985e\u578b\uff0c\u5305\u62ec\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MySQL\u3001PostgreSQL\uff09\u548c\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MongoDB\u3001Redis\uff09\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u91cd\u9ede\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Python\u8207\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<h2 id=\"python%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5-iDwNxuqrUI\">Python\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5<\/h2>\n<p>\u8981\u5728Python\u4e2d\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd\u76f8\u61c9\u7684\u6578\u64da\u5eab\u9a45\u52d5\u7a0b\u5e8f\u3002\u4ee5MySQL\u70ba\u4f8b\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528`mysql-connector-python`\u9019\u500b\u5eab\u4f86\u9023\u63a5MySQL\u6578\u64da\u5eab\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install mysql-connector-python<\/code><\/pre>\n<h3 id=\"%e5%bb%ba%e7%ab%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-iDwNxuqrUI\">\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u5b89\u88dd\u5b8c\u9a45\u52d5\u7a0b\u5e8f\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\nconn = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"your_username\",\n    password=\"your_password\",\n    database=\"your_database\"\n)\n\n# \u6aa2\u67e5\u9023\u63a5\u662f\u5426\u6210\u529f\nif conn.is_connected():\n    print(\"\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\")\nelse:\n    print(\"\u7121\u6cd5\u9023\u63a5\u5230\u6578\u64da\u5eab\")\n<\/code><\/pre>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-iDwNxuqrUI\">\u57fa\u672c\u7684\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u9032\u884c\u57fa\u672c\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5982\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da\u3002<\/p>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-iDwNxuqrUI\">\u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u4f7f\u7528SQL\u8a9e\u53e5\u67e5\u8a62\u6578\u64da\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>cursor = conn.cursor()\ncursor.execute(\"SELECT * FROM your_table\")\n\n# \u7372\u53d6\u67e5\u8a62\u7d50\u679c\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n\n# \u95dc\u9589\u6e38\u6a19\ncursor.close()\n<\/code><\/pre>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-iDwNxuqrUI\">\u63d2\u5165\u6578\u64da<\/h3>\n<p>\u63d2\u5165\u6578\u64da\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>cursor = conn.cursor()\nsql = \"INSERT INTO your_table (column1, column2) VALUES (%s, %s)\"\nval = (\"value1\", \"value2\")\ncursor.execute(sql, val)\n\n# \u63d0\u4ea4\u5230\u6578\u64da\u5eab\nconn.commit()\nprint(cursor.rowcount, \"\u689d\u8a18\u9304\u63d2\u5165\u6210\u529f\")\n\n# \u95dc\u9589\u6e38\u6a19\ncursor.close()\n<\/code><\/pre>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-iDwNxuqrUI\">\u66f4\u65b0\u6578\u64da<\/h3>\n<p>\u66f4\u65b0\u6578\u64da\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>cursor = conn.cursor()\nsql = \"UPDATE your_table SET column1 = %s WHERE column2 = %s\"\nval = (\"new_value\", \"value2\")\ncursor.execute(sql, val)\n\n# \u63d0\u4ea4\u5230\u6578\u64da\u5eab\nconn.commit()\nprint(cursor.rowcount, \"\u689d\u8a18\u9304\u66f4\u65b0\u6210\u529f\")\n\n# \u95dc\u9589\u6e38\u6a19\ncursor.close()\n<\/code><\/pre>\n<h3 id=\"%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-iDwNxuqrUI\">\u522a\u9664\u6578\u64da<\/h3>\n<p>\u522a\u9664\u6578\u64da\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>cursor = conn.cursor()\nsql = \"DELETE FROM your_table WHERE column1 = %s\"\nval = (\"value1\",)\ncursor.execute(sql, val)\n\n# \u63d0\u4ea4\u5230\u6578\u64da\u5eab\nconn.commit()\nprint(cursor.rowcount, \"\u689d\u8a18\u9304\u522a\u9664\u6210\u529f\")\n\n# \u95dc\u9589\u6e38\u6a19\ncursor.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-iDwNxuqrUI\">\u7d50\u8ad6<\/h2>\n<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u662f\u6bcf\u4f4d\u958b\u767c\u8005\u5fc5\u5099\u7684\u6280\u80fd\u3002\u901a\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u638c\u63e1\u57fa\u672c\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u80fd\u5920\u5728\u81ea\u5df1\u7684\u9805\u76ee\u4e2d\u61c9\u7528\u9019\u4e9b\u77e5\u8b58\u3002\u96a8\u8457\u60a8\u5c0dPython\u548c\u6578\u64da\u5eab\u7684\u6df1\u5165\u4e86\u89e3\uff0c\u60a8\u5c07\u80fd\u5920\u958b\u767c\u51fa\u66f4\u70ba\u8907\u96dc\u548c\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u652f\u6301\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u958b\u767c\u63d0\u4f9b\u66f4\u597d\u7684\u74b0\u5883\u548c\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f15\u9b06\u5b78\u7fd2Python\u6578\u64da\u5eab\u5165\u9580\u6307\u5357\uff0c\u638c\u63e1\u57fa\u672c\u6982\u5ff5\u8207\u5be6\u7528\u6280\u5de7\uff0c\u5feb\u901f\u63d0\u5347\u6578\u64da\u8655\u7406\u8207\u7ba1\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-171899","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171899","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=171899"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171899\/revisions"}],"predecessor-version":[{"id":171900,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171899\/revisions\/171900"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=171899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=171899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=171899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}