{"id":199760,"date":"2025-05-06T10:05:41","date_gmt":"2025-05-06T02:05:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199760\/"},"modified":"2025-05-06T10:05:41","modified_gmt":"2025-05-06T02:05:41","slug":"%e7%a0%94%e7%a9%b6mysql%e5%9c%a8%e5%a4%9a%e5%b9%b3%e5%8f%b0%e5%ba%94%e7%94%a8%e4%b8%ad%e7%9a%84%e8%a1%a8%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199760\/","title":{"rendered":"\u7814\u7a76MySQL\u5728\u591a\u5e73\u53f0\u5e94\u7528\u4e2d\u7684\u8868\u73b0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u7814\u7a76MySQL\u5728\u591a\u5e73\u53f0\u5e94\u7528\u4e2d\u7684\u8868\u73b0<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u4e3b\u673a\u5b9d\u8d1d\u5c31\u6574\u7406\u5206\u4eab\u300a\u7814\u7a76MySQL\u5728\u591a\u5e73\u53f0\u5e94\u7528\u4e2d\u7684\u8868\u73b0\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6570\u636e\u5e93\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u4e3b\u673a\u5b9d\u8d1d\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>MySQL \u5728\u4e0d\u540c\u5e73\u53f0\u95f4\u7684\u5e94\u7528\u63a2\u7a76<\/p>\n<p>MySQL \u662f\u4e00\u6b3e\u5f00\u6e90\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u4e2d\u3002\u968f\u7740\u79d1\u6280\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u4eba\u4eec\u5bf9\u4e8e\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u8981\u6c42\u4e5f\u8d8a\u6765\u8d8a\u9ad8\uff0c\u4f7f\u5f97\u6570\u636e\u5e93\u7cfb\u7edf\u9700\u8981\u5728\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u548c\u5e94\u7528\u3002\u672c\u6587\u5c06\u63a2\u8ba8 MySQL \u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u7684\u5e94\u7528\uff0c\u5305\u62ec Windows\u3001Mac \u548c Linux \u5e73\u53f0\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5728 Windows \u5e73\u53f0\u4e0a\u7684 MySQL \u5e94\u7528<\/p>\n<p>\u5728 Windows \u5e73\u53f0\u4e0a\u4f7f\u7528 MySQL\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u9002\u7528\u4e8e Windows \u7684 MySQL \u5b89\u88c5\u7a0b\u5e8f\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7 MySQL \u81ea\u5e26\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6216\u8005 MySQL Workbench \u6765\u7ba1\u7406\u6570\u636e\u5e93\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5728 Windows \u5e73\u53f0\u4e0a\u8fde\u63a5 MySQL \u6570\u636e\u5e93\u5e76\u67e5\u8be2\u6570\u636e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>import mysql.connector\n\n# \u8fde\u63a5\u5230\u6570\u636e\u5e93\nmydb = mysql.connector.connect(\n  host=\"localhost\",\n  user=\"root\",\n  password=\"password\",\n  database=\"mydatabase\"\n)\n\nmycursor = mydb.cursor()\n\n# \u67e5\u8be2\u6570\u636e\nmycursor.execute(\"SELECT * FROM customers\")\n\nresult = mycursor.fetchall()\n\nfor row in result:\n  print(row)\n<\/pre>\n<p>\u4e8c\u3001\u5728 Mac \u5e73\u53f0\u4e0a\u7684 MySQL \u5e94\u7528<\/p>\n<p>\u5728 Mac \u5e73\u53f0\u4e0a\u5b89\u88c5 MySQL \u7c7b\u4f3c\u4e8e\u5728 Windows \u5e73\u53f0\u4e0a\u7684\u5b89\u88c5\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d Mac \u7248\u7684 MySQL \u5b89\u88c5\u7a0b\u5e8f\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216\u8005 MySQL Workbench \u6765\u7ba1\u7406\u6570\u636e\u5e93\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5728 Mac \u5e73\u53f0\u4e0a\u4f7f\u7528 Python \u8fde\u63a5 MySQL \u6570\u636e\u5e93\u5e76\u63d2\u5165\u6570\u636e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>import mysql.connector\n\n# \u8fde\u63a5\u5230\u6570\u636e\u5e93\nmydb = mysql.connector.connect(\n  host=\"localhost\",\n  user=\"root\",\n  password=\"password\",\n  database=\"mydatabase\"\n)\n\nmycursor = mydb.cursor()\n\n# \u63d2\u5165\u6570\u636e\nsql = \"INSERT INTO customers (name, address) VALUES (%s, %s)\"\nval = (\"John\", \"Highway 21\")\nmycursor.execute(sql, val)\n\nmydb.commit()\n\nprint(\"\u6570\u636e\u63d2\u5165\u6210\u529f\")\n<\/pre>\n<p>\u4e09\u3001\u5728 Linux \u5e73\u53f0\u4e0a\u7684 MySQL \u5e94\u7528<\/p>\n<p>\u5728 Linux \u5e73\u53f0\u4e0a\u5b89\u88c5 MySQL \u4e00\u822c\u901a\u8fc7\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u6bd4\u5982\u5728 Ubuntu \u4e0a\u53ef\u4ee5\u4f7f\u7528 apt-get \u6765\u5b89\u88c5 MySQL\u3002\u5b89\u88c5\u5b8c\u6210\u540e\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216\u8005\u5176\u5b83\u6570\u636e\u5e93\u7ba1\u7406\u5de5\u5177\u6765\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5728 Linux \u5e73\u53f0\u4e0a\u901a\u8fc7 PHP \u8fde\u63a5 MySQL \u6570\u636e\u5e93\u5e76\u66f4\u65b0\u6570\u636e\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"myDB\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u6d4b\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n  die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u66f4\u65b0\u6570\u636e\n$sql = \"UPDATE customers SET address='New Address' WHERE id=1\";\n\nif ($conn-&gt;query($sql) === TRUE) {\n  echo \"\u6570\u636e\u66f4\u65b0\u6210\u529f\";\n} else {\n  echo \"Error updating record: \" . $conn-&gt;error;\n}\n\n$conn-&gt;close();\n?&gt;<\/pre>\n<p>\u603b\u7ed3\uff1aMySQL \u4f5c\u4e3a\u4e00\u6b3e\u8de8\u5e73\u53f0\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u7684\u5e94\u7528\u65b9\u6cd5\u6709\u4e9b\u8bb8\u5dee\u5f02\uff0c\u4f46\u672c\u8d28\u4e0a\u90fd\u662f\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884cSQL\u8bed\u53e5\u7b49\u57fa\u672c\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u6587\u53ef\u4ee5\u5bf9\u8bfb\u8005\u7406\u89e3 MySQL \u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u7684\u5e94\u7528\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u7814\u7a76MySQL\u5728\u591a\u5e73\u53f0\u5e94\u7528\u4e2d\u7684\u8868&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-199760","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199760","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=199760"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199760\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}