{"id":7614,"date":"2024-11-14T08:42:46","date_gmt":"2024-11-14T00:42:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7614\/"},"modified":"2024-11-14T08:42:46","modified_gmt":"2024-11-14T00:42:46","slug":"%e4%bd%bf%e7%94%a8-sqlalchemy-%e6%9f%a5%e8%af%a2%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%8c%e6%98%af%e5%90%a6%e5%bf%85%e9%a1%bb%e6%8c%87%e5%ae%9a%e5%ad%97%e6%ae%b5%e5%90%8d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7614\/","title":{"rendered":"\u4f7f\u7528 SQLAlchemy \u67e5\u8be2\u6570\u636e\u5e93\uff0c\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173105735443251.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 SQLAlchemy \u67e5\u8be2\u6570\u636e\u5e93\uff0c\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 SQLAlchemy \u67e5\u8be2\u6570\u636e\u5e93\uff0c\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 sqlalchemy \u67e5\u8be2\u6570\u636e\u5e93<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 sqlalchemy \u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u65f6\uff0c\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\uff1f<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 sqlalchemy \u67e5\u8be2\u6570\u636e\u5e93\u65f6\uff0c\u65b0\u7248\u4ee3\u7801\u4e2d\u4e0d\u518d\u9700\u8981\u5355\u72ec\u6307\u5b9a\u5b57\u6bb5\u540d\u3002\u60a8\u53ef\u4ee5\u50cf php \u4e00\u6837\u76f4\u63a5\u4e66\u5199 sql \u8bed\u53e5\uff0c\u5982\u4e0b\u6240\u793a\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>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u662f\u65b0\u7248\u4ee3\u7801\uff0c\u4e0e\u8001\u7248\u4ee3\u7801\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u4f7f\u7528\u4e86 text() \u51fd\u6570\u6765\u6267\u884c sql \u8bed\u53e5\uff0c\u5b83\u5141\u8bb8\u60a8\u76f4\u63a5\u7f16\u5199 sql \u800c\u65e0\u9700\u663e\u5f0f\u6307\u5b9a\u5b57\u6bb5\u540d\u3002<\/p>\n<p>\u6709\u5173\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605 sqlalchemy \u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/docs.sqlalchemy.org\/en\/20\/core\/connections.html<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 SQLAlchemy \u67e5\u8be2\u6570\u636e\u5e93\uff0c\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 sqlalchemy \u67e5\u8be2\u6570\u636e\u5e93 \u5728\u4f7f\u7528 sqlalchemy \u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u65f6\uff0c\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\uff1f \u89e3\u7b54\uff1a \u4f7f\u7528 sqlalchemy \u67e5\u8be2\u6570\u636e\u5e93\u65f6\uff0c\u65b0\u7248\u4ee3\u7801\u4e2d\u4e0d\u518d\u9700\u8981\u5355\u72ec\u6307\u5b9a\u5b57\u6bb5\u540d\u3002\u60a8\u53ef\u4ee5\u50cf php \u4e00\u6837\u76f4\u63a5\u4e66\u5199 sql \u8bed\u53e5\uff0c\u5982\u4e0b\u6240\u793a\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) \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a \u4ee5\u4e0a\u4ee3\u7801\u662f\u65b0\u7248\u4ee3\u7801\uff0c\u4e0e\u8001\u7248\u4ee3\u7801\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u4f7f\u7528\u4e86 text() \u51fd\u6570\u6765\u6267\u884c sql \u8bed\u53e5\uff0c\u5b83\u5141\u8bb8\u60a8\u76f4\u63a5\u7f16\u5199 sql \u800c\u65e0\u9700\u663e\u5f0f\u6307\u5b9a\u5b57\u6bb5\u540d\u3002 \u6709\u5173\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605 sqlalchemy \u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/docs.sqlalchemy.org\/en\/20\/core\/connections.html \u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 SQLAlchemy \u67e5\u8be2\u6570\u636e\u5e93\uff0c\u662f\u5426\u5fc5\u987b\u6307\u5b9a\u5b57\u6bb5\u540d\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\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-7614","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7614","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=7614"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7614\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}