{"id":97038,"date":"2024-10-20T14:29:18","date_gmt":"2024-10-20T06:29:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97038\/"},"modified":"2024-10-20T14:29:18","modified_gmt":"2024-10-20T06:29:18","slug":"python%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%ae%a1%e7%90%86mysql%ef%bc%8c%e6%8f%90%e9%ab%98%e6%95%88%e7%8e%87-python%e4%b8%ad%e7%9a%84mysql%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97038\/","title":{"rendered":"Python\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7ba1\u7406MySQL\uff0c\u63d0\u9ad8\u6548\u7387 (python\u4e2d\u7684mysql\u6578\u64da\u5eab\u9023\u63a5\u6c60)"},"content":{"rendered":"<h1 id=\"python%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%ae%a1%e7%90%86mysql%ef%bc%8c%e6%8f%90%e9%ab%98%e6%95%88%e7%8e%87-vvXXRqMyNF\">Python\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7ba1\u7406MySQL\uff0c\u63d0\u9ad8\u6548\u7387<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002\u5c24\u5176\u662f\u5728\u4f7f\u7528MySQL\u6578\u64da\u5eab\u6642\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u6210\u70ba\u4e86\u958b\u767c\u8005\u9700\u8981\u9762\u5c0d\u7684\u4e00\u500b\u6311\u6230\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7684\u6982\u5ff5\u61c9\u904b\u800c\u751f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u6c60\u4f86\u7ba1\u7406MySQL\uff0c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-vvXXRqMyNF\">\u4ec0\u9ebc\u662f\u6578\u64da\u5eab\u9023\u63a5\u6c60\uff1f<\/h2>\n<p>\u6578\u64da\u5eab\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7528\u65bc\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u7684\u6280\u8853\u3002\u5b83\u901a\u904e\u9810\u5148\u5275\u5efa\u4e00\u5b9a\u6578\u91cf\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u4e26\u5c07\u9019\u4e9b\u9023\u63a5\u5b58\u5132\u5728\u6c60\u4e2d\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u6642\u5feb\u901f\u7372\u53d6\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u6bcf\u6b21\u8acb\u6c42\u90fd\u91cd\u65b0\u5efa\u7acb\u9023\u63a5\u7684\u958b\u92b7\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-vvXXRqMyNF\">\u70ba\u4ec0\u9ebc\u4f7f\u7528\u9023\u63a5\u6c60\uff1f<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong>\u9023\u63a5\u6c60\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u9023\u63a5\u5efa\u7acb\u7684\u6642\u9593\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u97ff\u61c9\u901f\u5ea6\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u901a\u904e\u9650\u5236\u540c\u6642\u6253\u958b\u7684\u9023\u63a5\u6578\u91cf\uff0c\u9023\u63a5\u6c60\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u8cc7\u6e90\uff0c\u9632\u6b62\u904e\u8f09\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u4ee3\u78bc\uff0c\u8b93\u958b\u767c\u8005\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%9c%a8python%e4%b8%ad%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0-vvXXRqMyNF\">\u5728Python\u4e2d\u4f7f\u7528\u9023\u63a5\u6c60<\/h2>\n<p>\u5728Python\u4e2d\uff0c\u6709\u591a\u500b\u5eab\u53ef\u4ee5\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fSQLAlchemy\u548cmysql-connector-python\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u9019\u5169\u500b\u5eab\u4f86\u5be6\u73feMySQL\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7684\u793a\u4f8b\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8sqlalchemy-vvXXRqMyNF\">\u4f7f\u7528SQLAlchemy<\/h3>\n<pre><code>from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\n\n# \u5275\u5efa\u6578\u64da\u5eab\u5f15\u64ce\uff0c\u8a2d\u7f6e\u9023\u63a5\u6c60\u5927\u5c0f\nengine = create_engine('mysql+mysqlconnector:\/\/username:password@localhost\/dbname', pool_size=5, max_overflow=10)\n\n# \u5275\u5efa\u6703\u8a71\u5de5\u5ee0\nSession = sessionmaker(bind=engine)\n\n# \u7372\u53d6\u6703\u8a71\nsession = Session()\n\n# \u57f7\u884c\u67e5\u8a62\nresult = session.execute(\"SELECT * FROM your_table\")\nfor row in result:\n    print(row)\n\n# \u95dc\u9589\u6703\u8a71\nsession.close()\n<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8mysql-connector-python-vvXXRqMyNF\">\u4f7f\u7528mysql-connector-python<\/h3>\n<pre><code>import mysql.connector\nfrom mysql.connector import pooling\n\n# \u5275\u5efa\u9023\u63a5\u6c60\ndbconfig = {\n    \"database\": \"dbname\",\n    \"user\": \"username\",\n    \"password\": \"password\",\n    \"host\": \"localhost\"\n}\nconnection_pool = pooling.MySQLConnectionPool(pool_name=\"mypool\", pool_size=5, **dbconfig)\n\n# \u7372\u53d6\u9023\u63a5\nconnection = connection_pool.get_connection()\n\n# \u57f7\u884c\u67e5\u8a62\ncursor = connection.cursor()\ncursor.execute(\"SELECT * FROM your_table\")\nfor row in cursor.fetchall():\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\ncursor.close()\nconnection.close()\n<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-vvXXRqMyNF\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u6c60\u6642\uff0c\u6709\u5e7e\u500b\u6700\u4f73\u5be6\u8e10\u9700\u8981\u9075\u5faa\uff1a<\/p>\n<ul>\n<li><strong>\u5408\u7406\u8a2d\u7f6e\u9023\u63a5\u6c60\u5927\u5c0f\uff1a<\/strong>\u6839\u64da\u61c9\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\u548c\u6578\u64da\u5eab\u7684\u6027\u80fd\uff0c\u5408\u7406\u8a2d\u7f6e\u9023\u63a5\u6c60\u7684\u5927\u5c0f\uff0c\u4ee5\u907f\u514d\u8cc7\u6e90\u6d6a\u8cbb\u6216\u904e\u8f09\u3002<\/li>\n<li><strong>\u5b9a\u671f\u6aa2\u67e5\u9023\u63a5\u72c0\u614b\uff1a<\/strong>\u78ba\u4fdd\u9023\u63a5\u6c60\u4e2d\u7684\u9023\u63a5\u662f\u6709\u6548\u7684\uff0c\u5b9a\u671f\u6aa2\u67e5\u4e26\u6e05\u7406\u7121\u6548\u7684\u9023\u63a5\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff1a<\/strong>\u5728Python\u4e2d\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4f86\u81ea\u52d5\u7ba1\u7406\u9023\u63a5\u7684\u7372\u53d6\u548c\u91cb\u653e\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u4ee3\u78bc\u4e2d\u7684\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-vvXXRqMyNF\">\u7e3d\u7d50<\/h2>\n<p>\u5728Python\u4e2d\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u6c60\u4f86\u7ba1\u7406MySQL\u4e0d\u50c5\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u5730\u7ba1\u7406\u8cc7\u6e90\u3002\u901a\u904e\u5408\u7406\u8a2d\u7f6e\u9023\u63a5\u6c60\u7684\u5927\u5c0f\u548c\u4f7f\u7528\u6700\u4f73\u5be6\u8e10\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728\u9ad8\u4e26\u767c\u7684\u74b0\u5883\u4e2d\u4fdd\u6301\u61c9\u7528\u7a0b\u5e8f\u7684\u7a69\u5b9a\u6027\u548c\u6548\u7387\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u5eab\u548c\u61c9\u7528\u7a0b\u5e8f\u3002\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7ba1\u7406MySQL\uff0c\u63d0\u5347\u61c9\u7528\u6548\u7387\uff0c\u6e1b\u5c11\u9023\u63a5\u5ef6\u9072\uff0c\u512a\u5316\u8cc7\u6e90\u4f7f\u7528\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-97038","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97038","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=97038"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97038\/revisions"}],"predecessor-version":[{"id":97039,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97038\/revisions\/97039"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}