{"id":96974,"date":"2024-10-20T13:56:17","date_gmt":"2024-10-20T05:56:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96974\/"},"modified":"2024-10-20T13:56:18","modified_gmt":"2024-10-20T05:56:18","slug":"python-%e6%93%8d%e4%bd%9c-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e4%b8%89%e5%80%8b%e6%a8%a1%e5%a1%8a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96974\/","title":{"rendered":"Python \u64cd\u4f5c MySQL \u6578\u64da\u5eab\u7684\u4e09\u500b\u6a21\u584a"},"content":{"rendered":"<h1 id=\"python-%e6%93%8d%e4%bd%9c-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e4%b8%89%e5%80%8b%e6%a8%a1%e5%a1%8a-REknKCIVDB\">Python \u64cd\u4f5c MySQL \u6578\u64da\u5eab\u7684\u4e09\u500b\u6a21\u584a<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0cPython \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\u64cd\u4f5c\u3002MySQL \u662f\u4e00\u7a2e\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u8a31\u591a\u958b\u767c\u8005\u9078\u64c7\u4f7f\u7528 Python \u4f86\u8207 MySQL \u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u4e09\u500b\u5e38\u7528\u7684 Python \u6a21\u584a\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u64cd\u4f5c MySQL \u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"1-mysql-connector-python-REknKCIVDB\">1. MySQL Connector\/Python<\/h2>\n<p>MySQL Connector\/Python \u662f\u7531 MySQL \u5b98\u65b9\u63d0\u4f9b\u7684 Python \u9a45\u52d5\u7a0b\u5e8f\uff0c\u652f\u6301 Python 2.7 \u548c 3.x \u7248\u672c\u3002\u9019\u500b\u6a21\u584a\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684 MySQL API\uff0c\u4e26\u4e14\u80fd\u5920\u8f15\u9b06\u5730\u8207 MySQL \u6578\u64da\u5eab\u9032\u884c\u9023\u63a5\u548c\u64cd\u4f5c\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-REknKCIVDB\">\u5b89\u88dd<\/h3>\n<pre><code>pip install mysql-connector-python<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b-REknKCIVDB\">\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230 MySQL \u6578\u64da\u5eab\nconn = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"yourusername\",\n    password=\"yourpassword\",\n    database=\"yourdatabase\"\n)\n\ncursor = conn.cursor()\n\n# \u57f7\u884c\u67e5\u8a62\ncursor.execute(\"SELECT * FROM yourtable\")\n\n# \u7372\u53d6\u7d50\u679c\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\ncursor.close()\nconn.close()\n<\/code><\/pre>\n<h2 id=\"2-pymysql-REknKCIVDB\">2. PyMySQL<\/h2>\n<p>PyMySQL \u662f\u4e00\u500b\u7d14 Python \u5be6\u73fe\u7684 MySQL \u5ba2\u6236\u7aef\uff0c\u652f\u6301 Python 3.x\u3002\u5b83\u7684\u512a\u52e2\u5728\u65bc\u8f15\u91cf\u7d1a\u548c\u6613\u65bc\u4f7f\u7528\uff0c\u7279\u5225\u9069\u5408\u9700\u8981\u5feb\u901f\u958b\u767c\u7684\u9805\u76ee\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-REknKCIVDB\">\u5b89\u88dd<\/h3>\n<pre><code>pip install PyMySQL<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b-REknKCIVDB\">\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code>import pymysql\n\n# \u9023\u63a5\u5230 MySQL \u6578\u64da\u5eab\nconn = pymysql.connect(\n    host='localhost',\n    user='yourusername',\n    password='yourpassword',\n    database='yourdatabase'\n)\n\ncursor = conn.cursor()\n\n# \u57f7\u884c\u67e5\u8a62\ncursor.execute(\"SELECT * FROM yourtable\")\n\n# \u7372\u53d6\u7d50\u679c\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\ncursor.close()\nconn.close()\n<\/code><\/pre>\n<h2 id=\"3-sqlalchemy-REknKCIVDB\">3. SQLAlchemy<\/h2>\n<p>SQLAlchemy \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684 SQL \u5de5\u5177\u5305\u548c ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ec MySQL\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u5c64\u6b21\u7684 API\uff0c\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u66f4\u52a0\u7c21\u55ae\u548c\u76f4\u89c0\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-REknKCIVDB\">\u5b89\u88dd<\/h3>\n<pre><code>pip install SQLAlchemy pymysql<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b-REknKCIVDB\">\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code>from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\n\n# \u5275\u5efa\u6578\u64da\u5eab\u5f15\u64ce\nengine = create_engine('mysql+pymysql:\/\/yourusername:yourpassword@localhost\/yourdatabase')\n\n# \u5275\u5efa\u6703\u8a71\nSession = sessionmaker(bind=engine)\nsession = Session()\n\n# \u67e5\u8a62\u6578\u64da\nresult = session.execute(\"SELECT * FROM yourtable\")\nfor row in result:\n    print(row)\n\n# \u95dc\u9589\u6703\u8a71\nsession.close()\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-REknKCIVDB\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u4e09\u500b\u5e38\u7528\u7684 Python \u6a21\u584a\u4f86\u64cd\u4f5c MySQL \u6578\u64da\u5eab\uff1aMySQL Connector\/Python\u3001PyMySQL \u548c SQLAlchemy\u3002\u9019\u4e9b\u6a21\u584a\u5404\u6709\u7279\u9ede\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9805\u76ee\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u5de5\u5177\u3002\u7121\u8ad6\u662f\u9032\u884c\u7c21\u55ae\u7684\u6578\u64da\u67e5\u8a62\u9084\u662f\u8907\u96dc\u7684\u6578\u64da\u64cd\u4f5c\uff0c\u9019\u4e9b\u6a21\u584a\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\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f\u5176\u4ed6\u5730\u5340\u7684\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u9ad8\u6548\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Python \u64cd\u4f5c MySQL \u6578\u64da\u5eab\u7684\u4e09\u500b\u6a21\u584a\uff0c\u5b78\u7fd2\u5982\u4f55\u9ad8\u6548\u9023\u63a5\u3001\u67e5\u8a62\u548c\u7ba1\u7406\u6578\u64da\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-96974","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96974","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=96974"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96974\/revisions"}],"predecessor-version":[{"id":96975,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96974\/revisions\/96975"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}