{"id":46234,"date":"2024-12-01T16:12:05","date_gmt":"2024-12-01T08:12:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46234\/"},"modified":"2024-12-01T16:12:05","modified_gmt":"2024-12-01T08:12:05","slug":"sqlalchemy-%e6%9f%a5%e8%af%a2%e7%bb%93%e6%9e%9c%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%bf%e9%97%ae%e6%8c%87%e5%ae%9a%e5%ad%97%e6%ae%b5%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46234\/","title":{"rendered":"Sqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Sqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f<\/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\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aSqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f\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\u7c73\u4e91\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><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/17328913696749d2e95d96a.jpg\" class=\"aligncenter\" title=\"Sqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f\u63d2\u56fe\" alt=\"Sqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>sqlalchemy \u5bf9\u67e5\u8be2\u7ed3\u679c\u7684\u6307\u5b9a<\/strong><\/p>\n<p>sqlalchemy \u5728\u67e5\u8be2\u65f6\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\u5462\uff1f<\/p>\n<p>\u5b98\u65b9\u6587\u6863\u8868\u660e\uff0c\u65b0\u7248 sqlalchemy \u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 text \u51fd\u6570\u6765\u6267\u884c sql \u67e5\u8be2\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>from sqlalchemy import text, create_engine\n\nengine = create_engine(\"mysql+pymysql:\/\/\u8d26\u53f7:\u5bc6\u7801@\u5730\u5740\/\u5e93\")\n\nwith engine.connect() as connection:\n    result = connection.execute(text(\"select username from users\"))\n    for row in result:\n        print(\"username:\", row.username)<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0ctext(&#8220;select username from users&#8221;) \u521b\u5efa\u4e86\u4e00\u4e2a\u6587\u672c\u8868\u8fbe\u5f0f\uff0c\u5b83\u8868\u793a\u8981\u6267\u884c\u7684 sql \u67e5\u8be2\u3002\u6211\u4eec\u4e0d\u518d\u9700\u8981\u624b\u52a8\u6307\u5b9a\u5b57\u6bb5\u540d\uff0c\u800c\u662f\u53ef\u4ee5\u901a\u8fc7 row.username \u8bbf\u95ee\u7ed3\u679c\u4e2d\u7684\u503c\u3002<\/p>\n<p>\u4e0e\u65e7\u7248\u4ee3\u7801\u76f8\u6bd4\uff0c\u65b0\u7248\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\uff0c\u540c\u65f6\u5b83\u4e5f\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aSqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Sqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f \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\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aSqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f\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\u7c73\u4e91\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 sqlalchemy \u5bf9\u67e5\u8be2\u7ed3\u679c\u7684\u6307\u5b9a sqlalchemy \u5728\u67e5\u8be2\u65f6\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\u5462\uff1f \u5b98\u65b9\u6587\u6863\u8868\u660e\uff0c\u65b0\u7248 sqlalchemy \u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 text \u51fd\u6570\u6765\u6267\u884c sql \u67e5\u8be2\u3002\u4f8b\u5982\uff1a from sqlalchemy import text, create_engine engine = create_engine(&#8220;mysql+pymysql:\/\/\u8d26\u53f7:\u5bc6\u7801@\u5730\u5740\/\u5e93&#8221;) with engine.connect() as connection: result = connection.execute(text(&#8220;select username from users&#8221;)) for row in result: print(&#8220;username:&#8221;, row.username) \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0ctext(&#8220;select username from users&#8221;) \u521b\u5efa\u4e86\u4e00\u4e2a\u6587\u672c\u8868\u8fbe\u5f0f\uff0c\u5b83\u8868\u793a\u8981\u6267\u884c\u7684 sql \u67e5\u8be2\u3002\u6211\u4eec\u4e0d\u518d\u9700\u8981\u624b\u52a8\u6307\u5b9a\u5b57\u6bb5\u540d\uff0c\u800c\u662f\u53ef\u4ee5\u901a\u8fc7 row.username \u8bbf\u95ee\u7ed3\u679c\u4e2d\u7684\u503c\u3002 \u4e0e\u65e7\u7248\u4ee3\u7801\u76f8\u6bd4\uff0c\u65b0\u7248\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\uff0c\u540c\u65f6\u5b83\u4e5f\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u5b98\u65b9\u6587\u6863\u3002 \u4ee5\u4e0a\u5c31\u662f\u300aSqlalchemy \u67e5\u8be2\u7ed3\u679c\u4e2d\u5982\u4f55\u8bbf\u95ee\u6307\u5b9a\u5b57\u6bb5\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-46234","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=46234"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46234\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}