{"id":97024,"date":"2024-10-20T14:22:18","date_gmt":"2024-10-20T06:22:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97024\/"},"modified":"2024-10-20T14:22:18","modified_gmt":"2024-10-20T06:22:18","slug":"python%e5%af%a6%e7%8f%bemysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b7%a8%e7%a8%8b%e6%b7%b1%e5%ba%a6%e5%88%86%e6%9e%90-python%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b7%a8%e7%a8%8b-mysql","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97024\/","title":{"rendered":"Python\u5be6\u73feMySQL\u6578\u64da\u5eab\u7de8\u7a0b\u6df1\u5ea6\u5206\u6790 (python\u6578\u64da\u5eab\u7de8\u7a0b mysql)"},"content":{"rendered":"<h1 id=\"python%e5%af%a6%e7%8f%bemysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b7%a8%e7%a8%8b%e6%b7%b1%e5%ba%a6%e5%88%86%e6%9e%90-mMCXtkCqfO\">Python\u5be6\u73feMySQL\u6578\u64da\u5eab\u7de8\u7a0b\u6df1\u5ea6\u5206\u6790<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u4e14\u6613\u65bc\u5b78\u7fd2\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u6578\u64da\u5eab\u7de8\u7a0b\u4e2d\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Python\u8207MySQL\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87python%e8%88%87mysql-mMCXtkCqfO\">\u70ba\u4ec0\u9ebc\u9078\u64c7Python\u8207MySQL<\/h2>\n<p>Python\u64c1\u6709\u5f37\u5927\u7684\u5eab\u548c\u6846\u67b6\uff0c\u4f7f\u5f97\u8207MySQL\u7684\u4ea4\u4e92\u8b8a\u5f97\u7c21\u55ae\u3002MySQL\u662f\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u9069\u5408\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u3002\u7d50\u5408Python\u548cMySQL\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\u64cd\u4f5c\uff0c\u4e26\u9032\u884c\u6578\u64da\u5206\u6790\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e8%a8%ad%e7%bd%ae-mMCXtkCqfO\">\u74b0\u5883\u8a2d\u7f6e<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86Python\u548cMySQL\u3002\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5b89\u88dd\u4e00\u500bPython\u7684MySQL\u5eab\uff0c\u4f8b\u5982`mysql-connector-python`\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install mysql-connector-python<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e5%88%b0mysql%e6%95%b8%e6%93%9a%e5%ba%ab-mMCXtkCqfO\">\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u8207MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u9023\u63a5\u7684\u57fa\u672c\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import mysql.connector\n\n# \u5efa\u7acb\u9023\u63a5\nconn = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"your_username\",\n    password=\"your_password\",\n    database=\"your_database\"\n)\n\n# \u5275\u5efa\u6e38\u6a19\ncursor = conn.cursor()\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u60a8\u9700\u8981\u66ff\u63db`your_username`\u3001`your_password`\u548c`your_database`\u70ba\u60a8\u7684MySQL\u8cec\u6236\u4fe1\u606f\u548c\u6578\u64da\u5eab\u540d\u7a31\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e7%9a%84%e6%95%b8%e6%93%9a%e6%93%8d%e4%bd%9c-mMCXtkCqfO\">\u57fa\u672c\u7684\u6578\u64da\u64cd\u4f5c<\/h2>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-mMCXtkCqfO\">\u63d2\u5165\u6578\u64da<\/h3>\n<p>\u63d2\u5165\u6578\u64da\u662f\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u6700\u57fa\u672c\u7684\u529f\u80fd\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>sql_insert = \"INSERT INTO your_table (column1, column2) VALUES (%s, %s)\"\nvalues = (\"value1\", \"value2\")\n\ncursor.execute(sql_insert, values)\nconn.commit()  # \u63d0\u4ea4\u4e8b\u52d9\n<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-mMCXtkCqfO\">\u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u67e5\u8a62\u6578\u64da\u662f\u53e6\u4e00\u500b\u5e38\u898b\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>sql_select = \"SELECT * FROM your_table\"\ncursor.execute(sql_select)\n\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n<\/code><\/pre>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-mMCXtkCqfO\">\u66f4\u65b0\u6578\u64da<\/h3>\n<p>\u66f4\u65b0\u6578\u64da\u7684\u64cd\u4f5c\u4e5f\u975e\u5e38\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u66f4\u65b0\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>sql_update = \"UPDATE your_table SET column1 = %s WHERE column2 = %s\"\nvalues = (\"new_value\", \"value2\")\n\ncursor.execute(sql_update, values)\nconn.commit()  # \u63d0\u4ea4\u4e8b\u52d9\n<\/code><\/pre>\n<h3 id=\"%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-mMCXtkCqfO\">\u522a\u9664\u6578\u64da<\/h3>\n<p>\u522a\u9664\u6578\u64da\u7684\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<pre><code>sql_delete = \"DELETE FROM your_table WHERE column1 = %s\"\nvalue = (\"value1\",)\n\ncursor.execute(sql_delete, value)\nconn.commit()  # \u63d0\u4ea4\u4e8b\u52d9\n<\/code><\/pre>\n<h2 id=\"%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-mMCXtkCqfO\">\u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u932f\u8aa4\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u584a\u4f86\u6355\u7372\u7570\u5e38\uff1a<\/p>\n<pre><code>try:\n    cursor.execute(sql_insert, values)\n    conn.commit()\nexcept mysql.connector.Error as err:\n    print(f\"Error: {err}\")\nfinally:\n    cursor.close()\n    conn.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-mMCXtkCqfO\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u6211\u5011\u6df1\u5165\u5206\u6790\u4e86\u5982\u4f55\u4f7f\u7528Python\u9032\u884cMySQL\u6578\u64da\u5eab\u7de8\u7a0b\u3002\u5f9e\u9023\u63a5\u6578\u64da\u5eab\u5230\u57f7\u884c\u57fa\u672c\u7684\u589e\u522a\u6539\u67e5\u64cd\u4f5c\uff0c\u9019\u4e9b\u90fd\u662f\u958b\u767c\u8005\u5728\u65e5\u5e38\u5de5\u4f5c\u4e2d\u5fc5\u9808\u638c\u63e1\u7684\u6280\u80fd\u3002\u96a8\u8457\u6578\u64da\u9700\u6c42\u7684\u589e\u9577\uff0c\u638c\u63e1\u9019\u4e9b\u6280\u8853\u5c07\u4f7f\u60a8\u5728\u6578\u64da\u8655\u7406\u548c\u5206\u6790\u65b9\u9762\u66f4\u5177\u7af6\u722d\u529b\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\u7684\u6578\u64da\u5eab\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\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\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u5206\u6790Python\u8207MySQL\u6578\u64da\u5eab\u7de8\u7a0b\uff0c\u63a2\u7d22\u6578\u64da\u64cd\u4f5c\u3001\u67e5\u8a62\u512a\u5316\u53ca\u5be6\u8e10\u6280\u5de7\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u8207\u6027\u80fd\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-97024","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97024","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=97024"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97024\/revisions"}],"predecessor-version":[{"id":97025,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97024\/revisions\/97025"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}