{"id":2704,"date":"2025-05-29T17:13:11","date_gmt":"2025-06-04T09:51:10","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2704"},"modified":"2025-06-04T17:13:11","modified_gmt":"2025-06-04T09:51:10","slug":"%e4%bd%bf%e7%94%a8python%e5%bb%ba%e7%ab%8b%e8%b3%87%e6%96%99%e5%ba%ab%e7%ae%a1%e7%90%86%e7%b3%bb%e7%b5%b1","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e5%bb%ba%e7%ab%8b%e8%b3%87%e6%96%99%e5%ba%ab%e7%ae%a1%e7%90%86%e7%b3%bb%e7%b5%b1\/","title":{"rendered":"\u4f7f\u7528Python\u5efa\u7acb\u73fe\u4ee3\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff1a2025\u5e74\u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, \u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71, MySQL, SQLAlchemy, Python \u6559\u5b78\"><\/p>\n<p>\u96a8\u8457\u79d1\u6280\u7684\u9032\u6b65\uff0c\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u5df2\u7d93\u6210\u70ba\u73fe\u4ee3\u793e\u6703\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u53ef\u4ee5\u5e6b\u52a9\u4f01\u696d\u548c\u500b\u4eba\u5132\u5b58\u3001\u7ba1\u7406\u548c\u5206\u6790\u5927\u91cf\u7684\u8cc7\u6599\uff0c\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u6539\u5584\u6c7a\u7b56\u3002Python\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u9069\u5408\u7528\u65bc\u5efa\u7acb\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u56e0\u5176\u7c21\u6f54\u6613\u61c2\u7684\u8a9e\u6cd5\u548c\u5f37\u5927\u7684\u5eab\u652f\u6301\u3002<\/p>\n<h2>Python\u7684\u512a\u9ede<\/h2>\n<p>Python\u662f\u4e00\u7a2e\u9ad8\u968e\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u5176\u8a9e\u6cd5\u7c21\u55ae\u6613\u61c2\uff0c\u4f7f\u5f97\u7a0b\u5f0f\u78bc\u7684\u7dad\u8b77\u548c\u64f4\u5c55\u8b8a\u5f97\u66f4\u52a0\u5bb9\u6613\u3002\u6b64\u5916\uff0cPython\u652f\u63f4\u591a\u7a2e\u8cc7\u6599\u5eab\uff0c\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u7b49\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u8207\u8cc7\u6599\u5eab\u9032\u884c\u4e92\u52d5\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528Python\u5efa\u7acb\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71<\/h2>\n<p>\u8981\u4f7f\u7528Python\u5efa\u7acb\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u9996\u5148\u9700\u8981\u5b89\u88ddPython\u53ca\u6240\u9700\u7684\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u4f8b\u5982MySQL\u3002\u63a5\u8457\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528Python\u7684\u76f8\u95dc\u6a21\u7d44\u4f86\u9023\u63a5\u8cc7\u6599\u5eab\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528`mysql-connector-python`\u6a21\u7d44\u7684\u7bc4\u4f8b\uff0c\u9019\u662f2025\u5e74\u6700\u65b0\u7684\u63a8\u85a6\u65b9\u5f0f\u3002<\/p>\n<p>&#8220;`python<br \/>\nimport mysql.connector<\/p>\n<p># \u9023\u63a5MySQL\u8cc7\u6599\u5eab<br \/>\ndb = mysql.connector.connect(<br \/>\n    host=&#8221;localhost&#8221;,<br \/>\n    user=&#8221;username&#8221;,<br \/>\n    password=&#8221;password&#8221;,<br \/>\n    database=&#8221;database_name&#8221;<br \/>\n)<\/p>\n<p># \u5efa\u7acbcursor\u7269\u4ef6<br \/>\ncursor = db.cursor()<\/p>\n<p># \u57f7\u884cSQL\u6307\u4ee4<br \/>\ncursor.execute(&#8220;SELECT * FROM table_name&#8221;)<\/p>\n<p># \u53d6\u5f97\u6240\u6709\u8cc7\u6599<br \/>\ndata = cursor.fetchall()<\/p>\n<p># \u95dc\u9589\u8cc7\u6599\u5eab\u9023\u63a5<br \/>\ncursor.close()<br \/>\ndb.close()<br \/>\n&#8220;`<\/p>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u793a\u7bc4\u4e86\u5982\u4f55\u9023\u63a5\u5230MySQL\u8cc7\u6599\u5eab\u4e26\u57f7\u884cSQL\u6307\u4ee4\u4ee5\u7372\u53d6\u8cc7\u6599\u3002\u8acb\u78ba\u4fdd\u7528\u6236\u540d\u3001\u5bc6\u78bc\u548c\u8cc7\u6599\u5eab\u540d\u7a31\u6b63\u78ba\u7121\u8aa4\u3002<\/p>\n<h2>\u9032\u968e\u64cd\u4f5c\uff1a\u4f7f\u7528SQLAlchemy<\/h2>\n<p>\u9664\u4e86`mysql-connector-python`\uff0cPython\u9084\u63d0\u4f9b\u4e86SQLAlchemy\u9019\u4e00\u5f37\u5927\u7684ORM\u6846\u67b6\uff0c\u8b93\u8cc7\u6599\u5eab\u64cd\u4f5c\u66f4\u52a0\u7c21\u55ae\u76f4\u89c0\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528SQLAlchemy\u7684\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom sqlalchemy import create_engine<br \/>\nfrom sqlalchemy.orm import sessionmaker<\/p>\n<p># \u5efa\u7acb\u8cc7\u6599\u5eab\u9023\u63a5<br \/>\nengine = create_engine(&#8216;mysql+mysqlconnector:\/\/username:password@localhost\/database_name&#8217;)<br \/>\nSession = sessionmaker(bind=engine)<br \/>\nsession = Session()<\/p>\n<p># \u67e5\u8a62\u8cc7\u6599<br \/>\nresult = session.execute(&#8220;SELECT * FROM table_name&#8221;)<\/p>\n<p>for row in result:<br \/>\n    print(row)<\/p>\n<p># \u95dc\u9589session<br \/>\nsession.close()<br \/>\n&#8220;`<\/p>\n<p>\u4f7f\u7528SQLAlchemy\u53ef\u4ee5\u8b93\u4f60\u4ee5\u7269\u4ef6\u65b9\u5f0f\u64cd\u4f5c\u8cc7\u6599\u5eab\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>1. **\u9023\u63a5\u5931\u6557**\uff1a\u78ba\u4fdd\u4f60\u7684MySQL\u670d\u52d9\u5668\u6b63\u5728\u904b\u884c\uff0c\u4e26\u6aa2\u67e5\u7528\u6236\u540d\u548c\u5bc6\u78bc\u662f\u5426\u6b63\u78ba\u3002<br \/>\n2. **\u8cc7\u6599\u5eab\u4e0d\u5b58\u5728**\uff1a\u78ba\u8a8d\u8cc7\u6599\u5eab\u540d\u7a31\u662f\u5426\u62fc\u5beb\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd\u8a72\u8cc7\u6599\u5eab\u5df2\u7d93\u5efa\u7acb\u3002<br \/>\n3. **\u6b0a\u9650\u4e0d\u8db3**\uff1a\u78ba\u4fdd\u7528\u6236\u6709\u8db3\u5920\u7684\u6b0a\u9650\u4f86\u8a2a\u554f\u6240\u9700\u7684\u8cc7\u6599\u5eab\u548c\u8868\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python\u8207\u8cc7\u6599\u5eab\u4e92\u52d5\u4e0d\u50c5\u9650\u65bc\u67e5\u8a62\u8cc7\u6599\uff0c\u4f60\u9084\u53ef\u4ee5\u9032\u4e00\u6b65\u63a2\u7d22\u5982\u4f55\u9032\u884c\u8cc7\u6599\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u64cd\u4f5c\uff0c\u751a\u81f3\u642d\u5efa\u4e00\u500b\u5b8c\u6574\u7684Web\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u4f7f\u7528Flask\u6216Django\u4f86\u5be6\u73fe\u5f8c\u7aef\u529f\u80fd\u3002\u5982\u679c\u4f60\u5c0d\u8cc7\u6599\u5eab\u7ba1\u7406\u6709\u8208\u8da3\uff0c\u53ef\u4ee5\u53c3\u8003\u9019\u4e9b[Python\u8cc7\u6599\u5eab\u76f8\u95dc\u6559\u5b78](https:\/\/vocus.cc)\u4f86\u62d3\u5c55\u4f60\u7684\u77e5\u8b58\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u9069\u5408\u7528\u65bc\u5efa\u7acb\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002\u96a8\u8457\u65b0\u7684\u6a21\u7d44\u548c\u6280\u8853\u7684\u51fa\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u64cd\u4f5c\u8cc7\u6599\u5eab\uff0c\u4e26\u5feb\u901f\u958b\u767c\u51fa\u9ad8\u6548\u7684\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u6211\u9700\u8981\u5b78\u7fd2\u54ea\u4e9b\u57fa\u672c\u6280\u80fd\u624d\u80fd\u4f7f\u7528Python\u5efa\u7acb\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff1f**<br \/>\nA1: \u4f60\u9700\u8981\u638c\u63e1Python\u7684\u57fa\u672c\u8a9e\u6cd5\uff0c\u4e86\u89e3SQL\u8a9e\u8a00\uff0c\u4e26\u719f\u6089\u81f3\u5c11\u4e00\u7a2e\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff08\u5982MySQL\u6216SQLite\uff09\u3002<\/p>\n<p>**Q2: \u4f7f\u7528SQLAlchemy\u7684\u597d\u8655\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA2: SQLAlchemy\u63d0\u4f9b\u4e86\u7269\u4ef6\u95dc\u806f\u6620\u5c04\uff08ORM\uff09\uff0c\u4f7f\u5f97\u8cc7\u6599\u5eab\u64cd\u4f5c\u66f4\u5177\u53ef\u8b80\u6027\u548c\u9748\u6d3b\u6027\uff0c\u6e1b\u5c11\u4e86\u76f4\u63a5\u7de8\u5bebSQL\u4ee3\u78bc\u7684\u9700\u8981\u3002<\/p>\n<p>**Q3: \u5982\u679c\u6211\u5728\u9023\u63a5\u8cc7\u6599\u5eab\u6642\u9047\u5230\u932f\u8aa4\uff0c\u6211\u8a72\u600e\u9ebc\u8fa6\uff1f**<br \/>\nA3: \u6aa2\u67e5\u4f60\u7684\u8cc7\u6599\u5eab\u670d\u52d9\u5668\u662f\u5426\u6b63\u5728\u904b\u884c\uff0c\u78ba\u4fdd\u7528\u6236\u540d\u3001\u5bc6\u78bc\u548c\u8cc7\u6599\u5eab\u540d\u7a31\u6b63\u78ba\uff0c\u4e26\u6aa2\u67e5\u7528\u6236\u7684\u8a2a\u554f\u6b0a\u9650\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Python\u5efa\u7acb\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Python\u4f86\u7ba1\u7406\u8cc7\u6599\u5eab\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528Python\u4f86\u63d0\u9ad8\u8cc7\u6599\u5eab\u7ba1\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-HC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2704"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2704\/revisions"}],"predecessor-version":[{"id":2705,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2704\/revisions\/2705"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}