{"id":1092377,"date":"2025-01-08T14:20:36","date_gmt":"2025-01-08T06:20:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1092377.html"},"modified":"2025-01-08T14:20:38","modified_gmt":"2025-01-08T06:20:38","slug":"python-%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae%e5%ba%93%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1092377.html","title":{"rendered":"python \u5982\u4f55\u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205443\/eed579f6-0a0d-4c8e-81a0-c76226df41a2.webp\" alt=\"python \u5982\u4f55\u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868\" \/><\/p>\n<p><p> <strong>Python \u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd<\/strong>\uff0c<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 SQLAlchemy\u3001Pandas\u3001\u4ee5\u53ca\u76f4\u63a5\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u5982 psycopg2\u3001sqlite3 \u7b49<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SQLAlchemy \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>SQLAlchemy \u662f\u4e00\u4e2a Python SQL \u5de5\u5177\u5305\u548c ORM \u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u3001\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 SQLAlchemy \u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 SQLAlchemy<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sqlalchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/example.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u6620\u5c04\u7c7b\u5e76\u53cd\u6620\u73b0\u6709\u8868<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<p>Base = declarative_base()<\/p>\n<p>class ExampleTable(Base):<\/p>\n<p>    __tablename__ = &#39;example_table&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    name = Column(String)<\/p>\n<p>    value = Column(Float)<\/p>\n<h2><strong>\u521b\u5efa\u6240\u6709\u8868<\/strong><\/h2>\n<p>Base.metadata.create_all(engine)<\/p>\n<h2><strong>\u521b\u5efa\u4f1a\u8bdd<\/strong><\/h2>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u904d\u5386\u8bfb\u53d6\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = session.query(ExampleTable).all()<\/p>\n<p>for row in results:<\/p>\n<p>    print(row.id, row.name, row.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001Pandas \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Pandas \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Pandas \u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 Pandas<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u8bfb\u53d6\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e\u5e93\u8868<\/strong><\/h2>\n<p>df = pd.read_sql_query(&quot;SELECT * FROM example_table&quot;, conn)<\/p>\n<h2><strong>\u904d\u5386\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for index, row in df.iterrows():<\/p>\n<p>    print(row[&#39;id&#39;], row[&#39;name&#39;], row[&#39;value&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 SQLAlchemy \u548c Pandas\uff0c\u76f4\u63a5\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u8fd9\u91cc\u6211\u4eec\u4ee5 sqlite3 \u4e3a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884c\u67e5\u8be2\u5e76\u904d\u5386\u8bfb\u53d6\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM example_table&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5176\u4ed6\u6570\u636e\u5e93\u8fde\u63a5\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 sqlite3\uff0cPython \u8fd8\u652f\u6301\u5176\u4ed6\u591a\u79cd\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u5982 psycopg2\uff08\u7528\u4e8e PostgreSQL\uff09\u3001pymysql\uff08\u7528\u4e8e MySQL\uff09\u3001cx_Oracle\uff08\u7528\u4e8e Oracle \u6570\u636e\u5e93\uff09\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 psycopg2 \u904d\u5386\u8bfb\u53d6 PostgreSQL \u6570\u636e\u5e93\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 psycopg2<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<h2><strong>\u8fde\u63a5\u5230 PostgreSQL \u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = psycopg2.connect(<\/p>\n<p>    dbname=&quot;your_dbname&quot;,<\/p>\n<p>    user=&quot;your_username&quot;,<\/p>\n<p>    password=&quot;your_password&quot;,<\/p>\n<p>    host=&quot;your_host&quot;,<\/p>\n<p>    port=&quot;your_port&quot;<\/p>\n<p>)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884c\u67e5\u8be2\u5e76\u904d\u5386\u8bfb\u53d6\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT * FROM example_table&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868\u7684\u6570\u636e<\/strong>\uff0c<strong>\u5305\u62ec\u4f7f\u7528 SQLAlchemy\u3001Pandas \u4ee5\u53ca\u76f4\u63a5\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u5982 sqlite3\u3001psycopg2 \u7b49<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u4f7f\u7528 Python \u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5404\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p><strong>SQLAlchemy<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u652f\u6301 ORM\uff0c\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u5b66\u4e60\u66f2\u7ebf\u8f83\u9661\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u76f4\u63a5\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u3002<\/li>\n<\/ul>\n<p><p><strong>Pandas<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u6613\u4e8e\u4f7f\u7528\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u5185\u5b58\u5360\u7528\u8f83\u9ad8\u3002<\/li>\n<\/ul>\n<p><p><strong>\u76f4\u63a5\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u6027\u80fd\u9ad8\uff0c\u64cd\u4f5c\u7075\u6d3b\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u624b\u52a8\u5904\u7406 SQL \u8bed\u53e5\u548c\u8fde\u63a5\u7ba1\u7406\uff0c\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u548c\u9879\u76ee\u7279\u70b9\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u63d0\u9ad8\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6548\u7387\u548c\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fde\u63a5\u5230\u6570\u636e\u5e93\u5e76\u8bfb\u53d6\u6570\u636e\uff1f<\/strong><br \/>\u8981\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>sqlite3<\/code>\u5e93\u6216<code>SQLAlchemy<\/code>\u7b49\u5e93\u3002\u9996\u5148\uff0c\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff0c\u7136\u540e\u4f7f\u7528\u8fde\u63a5\u5b57\u7b26\u4e32\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002\u8fde\u63a5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6e38\u6807\u5bf9\u8c61\u6267\u884cSQL\u67e5\u8be2\u5e76\u83b7\u53d6\u7ed3\u679c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import sqlite3\n\n# \u8fde\u63a5\u5230\u6570\u636e\u5e93\nconnection = sqlite3.connect(&#39;example.db&#39;)\ncursor = connection.cursor()\n\n# \u6267\u884c\u67e5\u8be2\ncursor.execute(&quot;SELECT * FROM your_table_name&quot;)\nrows = cursor.fetchall()\n\n# \u904d\u5386\u7ed3\u679c\nfor row in rows:\n    print(row)\n\n# \u5173\u95ed\u8fde\u63a5\nconnection.close()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u6839\u636e\u60a8\u7684\u6570\u636e\u5e93\u7c7b\u578b\u8c03\u6574\u8fde\u63a5\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7684\u5927\u6570\u636e\u91cf\uff1f<\/strong><br \/>\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u6e38\u6807\u7684<code>fetchmany(size)<\/code>\u65b9\u6cd5\u6216<code>fetchone()<\/code>\u65b9\u6cd5\u9010\u6761\u8bfb\u53d6\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>fetchmany(100)<\/code>\u53ef\u4ee5\u6bcf\u6b21\u53d6\u51fa100\u6761\u8bb0\u5f55\uff0c\u9002\u5408\u5927\u6570\u636e\u96c6\u7684\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6570\u636e\u5e93\u67e5\u8be2\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u5f02\u5e38\u662f\u5f88\u91cd\u8981\u7684\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u51fa\u73b0\u9519\u8bef\u65f6\uff0c\u7a0b\u5e8f\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    cursor.execute(&quot;SELECT * FROM your_table_name&quot;)\nexcept sqlite3.Error as e:\n    print(f&quot;An error occurred: {e}&quot;)\nfinally:\n    connection.close()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u786e\u4fdd\u8d44\u6e90\u7684\u6b63\u786e\u91ca\u653e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u904d\u5386\u8bfb\u53d6\u6570\u636e\u5e93\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528 SQLAlchemy\u3001Pandas\u3001\u4ee5\u53ca\u76f4\u63a5 [&hellip;]","protected":false},"author":3,"featured_media":1092385,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092377"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1092377"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092377\/revisions"}],"predecessor-version":[{"id":1092387,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092377\/revisions\/1092387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1092385"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1092377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1092377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1092377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}