{"id":96994,"date":"2024-10-20T14:06:17","date_gmt":"2024-10-20T06:06:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96994\/"},"modified":"2024-10-20T14:06:18","modified_gmt":"2024-10-20T06:06:18","slug":"python%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%b5%b1%ef%bc%8c%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2-python%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96994\/","title":{"rendered":"Python\u64cd\u4f5c\u7cfb\u7d71\uff0c\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u67e5\u8a62 (python\u67e5\u8a62\u6578\u64da\u5eab\u64cd\u4f5c\u7cfb\u7d71)"},"content":{"rendered":"<h1 id=\"python%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%b5%b1%ef%bc%8c%e8%bc%95%e9%ac%86%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2-jPomBFSeuU\">Python\u64cd\u4f5c\u7cfb\u7d71\uff0c\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u67e5\u8a62<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u67e5\u8a62\u6210\u70ba\u4e86\u8a31\u591a\u61c9\u7528\u7a0b\u5e8f\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5eab\u548c\u5de5\u5177\u4f86\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Python\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\uff0c\u4e26\u4ecb\u7d39\u4e00\u4e9b\u5e38\u7528\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u7cfb\u7d71\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-jPomBFSeuU\">Python\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5<\/h2>\n<p>\u5728\u958b\u59cb\u67e5\u8a62\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fddPython\u80fd\u5920\u9023\u63a5\u5230\u6240\u9700\u7684\u6578\u64da\u5eab\u3002Python\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u7b49\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Python\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230\u6578\u64da\u5eab\ndb = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"yourusername\",\n    password=\"yourpassword\",\n    database=\"yourdatabase\"\n)\n\ncursor = db.cursor()\n<\/code><\/pre>\n<h2 id=\"%e5%9f%b7%e8%a1%8c%e5%9f%ba%e6%9c%ac%e6%9f%a5%e8%a9%a2-jPomBFSeuU\">\u57f7\u884c\u57fa\u672c\u67e5\u8a62<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u5c31\u53ef\u4ee5\u958b\u59cb\u57f7\u884c\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u57f7\u884c\u4e00\u500b\u7c21\u55ae\u7684SELECT\u67e5\u8a62\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code># \u57f7\u884c\u67e5\u8a62\ncursor.execute(\"SELECT * FROM yourtable\")\n\n# \u7372\u53d6\u67e5\u8a62\u7d50\u679c\nresults = cursor.fetchall()\n\nfor row in results:\n    print(row)\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86`fetchall()`\u65b9\u6cd5\u4f86\u7372\u53d6\u6240\u6709\u67e5\u8a62\u7d50\u679c\uff0c\u4e26\u901a\u904e\u8ff4\u5708\u5c07\u5176\u6253\u5370\u51fa\u4f86\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e5%8f%83%e6%95%b8%e5%8c%96%e6%9f%a5%e8%a9%a2-jPomBFSeuU\">\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62<\/h2>\n<p>\u70ba\u4e86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\uff0c\u5efa\u8b70\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code># \u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\nsql = \"SELECT * FROM yourtable WHERE column1 = %s\"\nval = (\"value1\",)\ncursor.execute(sql, val)\n\nresults = cursor.fetchall()\n\nfor row in results:\n    print(row)\n<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u4e0d\u50c5\u5b89\u5168\uff0c\u9084\u80fd\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u3002<\/p>\n<h2 id=\"%e6%9b%b4%e6%96%b0%e5%92%8c%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-jPomBFSeuU\">\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da<\/h2>\n<p>\u9664\u4e86\u67e5\u8a62\u6578\u64da\uff0cPython\u9084\u53ef\u4ee5\u7528\u4f86\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u66f4\u65b0\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code># \u66f4\u65b0\u6578\u64da\nsql = \"UPDATE yourtable SET column1 = %s WHERE column2 = %s\"\nval = (\"new_value\", \"value2\")\ncursor.execute(sql, val)\n\ndb.commit()  # \u63d0\u4ea4\u66f4\u6539\n<\/code><\/pre>\n<p>\u522a\u9664\u6578\u64da\u7684\u64cd\u4f5c\u985e\u4f3c\uff1a<\/p>\n<pre><code># \u522a\u9664\u6578\u64da\nsql = \"DELETE FROM yourtable WHERE column1 = %s\"\nval = (\"value1\",)\ncursor.execute(sql, val)\n\ndb.commit()  # \u63d0\u4ea4\u66f4\u6539\n<\/code><\/pre>\n<h2 id=\"%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-jPomBFSeuU\">\u95dc\u9589\u9023\u63a5<\/h2>\n<p>\u5728\u5b8c\u6210\u6240\u6709\u64cd\u4f5c\u5f8c\uff0c\u8a18\u5f97\u95dc\u9589\u6e38\u6a19\u548c\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/p>\n<pre><code>cursor.close()\ndb.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-jPomBFSeuU\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Python\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\u662f\u4e00\u500b\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u904e\u7a0b\u3002\u901a\u904e\u9069\u7576\u7684\u5eab\u548c\u5de5\u5177\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9023\u63a5\u5230\u5404\u7a2e\u6578\u64da\u5eab\uff0c\u57f7\u884c\u67e5\u8a62\u3001\u66f4\u65b0\u548c\u522a\u9664\u64cd\u4f5c\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684Python\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Python\u64cd\u4f5c\u7cfb\u7d71\uff0c\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eab\u67e5\u8a62\uff0c\u63d0\u5347\u6578\u64da\u7ba1\u7406\u6548\u7387\uff0c\u7c21\u5316\u958b\u767c\u6d41\u7a0b\uff0c\u8b93\u7de8\u7a0b\u66f4\u9ad8\u6548\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-96994","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96994","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=96994"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96994\/revisions"}],"predecessor-version":[{"id":96995,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96994\/revisions\/96995"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}