{"id":188150,"date":"2024-11-11T20:13:22","date_gmt":"2024-11-11T12:13:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/188150\/"},"modified":"2024-11-11T20:13:23","modified_gmt":"2024-11-11T12:13:23","slug":"%e5%9b%9b%e7%a8%aepython%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/188150\/","title":{"rendered":"\u56db\u7a2ePython\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u65b9\u6cd5"},"content":{"rendered":"<h1 id=\"%e5%9b%9b%e7%a8%aepython%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%96%b9%e6%b3%95-uwgHZTKAee\">\u56db\u7a2ePython\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u65b9\u6cd5<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0cPython\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u6578\u64da\u5206\u6790\u3001\u7db2\u9801\u958b\u767c\u548c\u81ea\u52d5\u5316\u7b49\u9818\u57df\u3002\u8207MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u662fPython\u958b\u767c\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u7d44\u6210\u90e8\u5206\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u56db\u7a2e\u5e38\u898b\u7684Python\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u9078\u64c7\u6700\u9069\u5408\u7684\u65b9\u6848\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8mysql-connector-python-uwgHZTKAee\">1. \u4f7f\u7528MySQL Connector\/Python<\/h2>\n<p>MySQL Connector\/Python\u662fMySQL\u5b98\u65b9\u63d0\u4f9b\u7684Python\u9a45\u52d5\u7a0b\u5e8f\uff0c\u652f\u6301Python 3.x\u7248\u672c\u3002\u9019\u500b\u9a45\u52d5\u7a0b\u5e8f\u7c21\u55ae\u6613\u7528\uff0c\u9069\u5408\u521d\u5b78\u8005\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-uwgHZTKAee\">\u5b89\u88dd<\/h3>\n<pre><code>pip install mysql-connector-python<\/code><\/pre>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-uwgHZTKAee\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>import mysql.connector\n\n# \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\nconn = mysql.connector.connect(\n    host=\"localhost\",\n    user=\"yourusername\",\n    password=\"yourpassword\",\n    database=\"yourdatabase\"\n)\n\n# \u5275\u5efa\u6e38\u6a19\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-%e4%bd%bf%e7%94%a8pymysql-uwgHZTKAee\">2. \u4f7f\u7528PyMySQL<\/h2>\n<p>PyMySQL\u662f\u4e00\u500b\u7d14Python\u5be6\u73fe\u7684MySQL\u5ba2\u6236\u7aef\uff0c\u652f\u6301Python 3.x\u3002\u5b83\u7684\u512a\u52e2\u5728\u65bc\u4e0d\u9700\u8981\u5b89\u88dd\u4efb\u4f55C\u64f4\u5c55\uff0c\u5b89\u88dd\u548c\u4f7f\u7528\u90fd\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-uwgHZTKAee\">\u5b89\u88dd<\/h3>\n<pre><code>pip install PyMySQL<\/code><\/pre>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-uwgHZTKAee\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>import pymysql\n\n# \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\nconn = pymysql.connect(\n    host='localhost',\n    user='yourusername',\n    password='yourpassword',\n    database='yourdatabase'\n)\n\n# \u5275\u5efa\u6e38\u6a19\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-%e4%bd%bf%e7%94%a8sqlalchemy-uwgHZTKAee\">3. \u4f7f\u7528SQLAlchemy<\/h2>\n<p>SQLAlchemy\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ecMySQL\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u9ad8\u5c64\u6b21\u7684API\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u9069\u5408\u9700\u8981\u9032\u884c\u8907\u96dc\u67e5\u8a62\u7684\u61c9\u7528\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-uwgHZTKAee\">\u5b89\u88dd<\/h3>\n<pre><code>pip install SQLAlchemy pymysql<\/code><\/pre>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-uwgHZTKAee\">\u793a\u4f8b\u4ee3\u78bc<\/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# \u57f7\u884c\u67e5\u8a62\nresults = session.execute(\"SELECT * FROM yourtable\").fetchall()\nfor row in results:\n    print(row)\n\n# \u95dc\u9589\u6703\u8a71\nsession.close()\n<\/code><\/pre>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8django-orm-uwgHZTKAee\">4. \u4f7f\u7528Django ORM<\/h2>\n<p>Django\u662f\u4e00\u500b\u6d41\u884c\u7684Python\u7db2\u9801\u6846\u67b6\uff0c\u5167\u7f6e\u4e86ORM\u529f\u80fd\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u8207MySQL\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u9019\u5c0d\u65bc\u4f7f\u7528Django\u958b\u767c\u7684\u61c9\u7528\u4f86\u8aaa\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d-uwgHZTKAee\">\u5b89\u88dd<\/h3>\n<pre><code>pip install django mysqlclient<\/code><\/pre>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-uwgHZTKAee\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>import os\nimport django\n\n# \u8a2d\u7f6eDjango\u74b0\u5883\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourproject.settings')\ndjango.setup()\n\nfrom yourapp.models import YourModel\n\n# \u67e5\u8a62\u6578\u64da\nresults = YourModel.objects.all()\nfor row in results:\n    print(row)\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-uwgHZTKAee\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u56db\u7a2e\u4f7f\u7528Python\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u65b9\u6cd5\uff0c\u5305\u62ecMySQL Connector\/Python\u3001PyMySQL\u3001SQLAlchemy\u548cDjango ORM\u3002\u6839\u64da\u4e0d\u540c\u7684\u9700\u6c42\u548c\u4f7f\u7528\u5834\u666f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9078\u64c7\u6700\u9069\u5408\u7684\u65b9\u6848\u4f86\u5be6\u73fe\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u56db\u7a2ePython\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u65b9\u6cd5\uff0c\u7c21\u5316\u6578\u64da\u64cd\u4f5c\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\uff0c\u9069\u5408\u5404\u985e\u578b\u7684\u958b\u767c\u8005\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-188150","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188150","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=188150"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188150\/revisions"}],"predecessor-version":[{"id":188151,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188150\/revisions\/188151"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=188150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=188150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=188150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}